505.369.1650 [email protected]

I got a “Folder In Use” error after using a loop that included the DoCmd.OutputTo macro action to export a Microsoft Access report to PDF files.  The error occurred when I tried to rename the folder that the PDF files were exported to.

OutputTo Folder In Use Error

Using the DoCmd.OutputTo created a handle on the folder itself that was not released at the end of the function, preventing me from altering the folder.  I could delete the files in the folder, but not rename the folder.

To fix the problem, I used the DoEvents method after the call to DoCmd.OutputTo in the loop to ensure that all outstanding MS Access or Windows-related events were processed before starting through the loop again.

Code:

Dim intCounter As Integer
Dim strReportName As String
Dim strPath As String

strPath = "C:\ExportFolder\"

For intCounter = 1 To 10
    strReportName = "Report" & intCounter & ".PDF"
    DoCmd.OutputTo acOutputReport, "Report", acFormatPDF, _
        strPath & strReportName, False
    DoEvents
Next intCounter