导出为 PDF 时出现 MS ACCESS 错误(无法保存文件)

问题描述 投票:0回答:2

这个问题我已经困扰很长时间了,然后我的工作相当一致,但必须对数据库进行更改,现在它又出现了。 并不总是出错,但非常频繁。一旦我点击恢复,它就会运行并完成该过程。错误消息为“运行时错误‘2302’:Microsoft Access 无法将输出保存到您选择的文件中。” 我添加了两个修复程序,但没有运气。在创建临时文件之前添加临时文件,并在保存报告之前打开报告。错误代码发生在“DoCmd.OutputTo acOutputReport,str_code,acFormatPDF,strFile,False”处,我所要做的就是继续(播放按钮)并且它可以工作。我该怎么做才能避免这种情况并保持一致,因为我自动运行此报告,并且需要能够从该报告创建 PDF,而不会出现错误。 感谢您的帮助。我在网上看到了一些帖子,但没有真正 100% 修复的。也许另存为 PDF,或者有人知道如何解决这个问题吗?

代码:

     Function output(str_code, str_XYZ, str_loc)
            Dim fs As Object
            Dim TextFile As Object
            Dim strFile As String
            strFile = "\\E:\APPS\Dev_accdb\PDF\" & str_XYZ & "\" & str_XYZ & "- 
        " & str_a & "-rpt.pdf"
    
            Sleep 2000
            DoEvents
            'Added this from a post - add a empty file first - did not help
            Set fs = CreateObject("Scripting.FileSystemObject")
            Set TextFile = fs.CreateTextFile("\\E:\APPS\Dev_accdb\PDF\" & str_XYZ & "\" & str_XYZ & "-" & str_TC & "-rpt.pdf", True)
            DoEvents
            Sleep 2000
            TextFile.Close
            DoEvents
        Sleep 1000
    'Added this from a post - OpenReport hidden view and then close later -- also did not help
        DoCmd.OpenReport str_code, acViewPreview, , , acHidden
        Sleep 4000
        'NEXT step is where this stops sometimes with a "can't save the output to the file you have selected"
        DoCmd.OutputTo acOutputReport, str_code, acFormatPDF, strFile, False
        Sleep 1000
        DoCmd.Close acReport, str_code, acSaveNo
        Sleep 100
        
    On Error Resume Next
        DoCmd.Close acReport, str_code, acSaveNo
        On Error GoTo 0
        
        DoEvents
        Sleep 2000
   End Function

尝试使用“睡眠”来减慢速度,添加了一个空文件创建和隐藏的打开报告。没有什么真正有效的。

2023年6月8日更新: 我已经尝试过“...从您的系统中复制文件 UTILITY.ACCDA 程序文件\Microsoft Office\Office14\ACCWIZ 文件夹到 程序文件\Microsoft Office\Office14
文件夹。” 来自另一个堆栈溢出帖子的解决方案和这里相同 来自另一篇 SO 帖子。它只运行了两天,但如果有效的话我会在这里更新。 谢谢大家的帮助。请注意,变量和路径没有任何问题。

2023年6月15日更新: 上面试过了,没用。当我打开 Access 进行测试时,没有错误。当我自动使用 marco 来触发脚本时,我很可能会按照描述停止循环中的某些位置。我今天正在测试 ValNik 在评论中所说的内容。

vba ms-access pdf ms-access-2016
2个回答
0
投票

尝试使用映射驱动器语法来简化代码 - 类似的代码对我们来说效果很好:

Function output(str_code, str_XYZ, str_loc)

    Dim strFile As String

    strFile = "E:\APPS\Dev_accdb\PDF\" & str_XYZ & "\" & str_XYZ & "-" & str_a & "-rpt.pdf"
    
    DoCmd.OutputTo acOutputReport, str_code, acFormatPDF, strFile, False, , , acExportQualityPrint

End Function

0
投票

更改为C盘放置文件修复它。自 06/22 起没有错误。谢谢你

© www.soinside.com 2019 - 2024. All rights reserved.