vbams-accesspdfms-access-2016

Getting MS ACCESS error when exporting to PDF (can't save file)


I had this issue for a long time, then I had this pretty consistent working but had to make a change on the database and now it's acting up again. It's not always erroring out, but very frequently. Once I hit resume it runs and will complete the process. The error message is "Run-time error '2302': Microsoft Access can't save the output to the file you have selected." I have added two fixes, with no luck. Adding a temp file before creating it and opening the report before saving it. Error code happens at "DoCmd.OutputTo acOutputReport, str_code, acFormatPDF, strFile, False" and all I have to do is to continue (Play button) and it works. What can I do to avoid this and be consistent since I run this automated and need to be able to create PDF's from this report without an error. Thank you for helping. I see some posts online, but nothing really with a 100% fix. Maybe SAVE AS PDF, or anyone know how to fix this?

CODE:

     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

Tried to slow down with SLEEP, added a empty file creation and open report hidden. Nothing really works.

UPDATE 2023-06-08: I have tried this "...copy the file UTILITY.ACCDA from your Program Files\Microsoft Office\Office14\ACCWIZ folder to Program Files\Microsoft Office\Office14
folder." from solution from another stack overflow post and same here from yet another SO post. It's been running only for two days, but I will update here if this works. Thank you for helping everyone. Just a note, there is nothing wrong with the variables and the paths.

Update 2023-06-15: Tried above and not working. When I test with Access open, no errors. When I automate using marco to fire script, I will most likely stop some where in the loop as described. I am testing today what ValNik said in the comments.


Solution

  • changed to C drive for placing the file fixed it. No errors since 06/22.