Check if directory is open before saving PDF

Sketchin

Registered User.
Local time
Yesterday, 23:12
Joined
Dec 20, 2011
Messages
580
Hi All,

I have some code that resides in a button on a report that allows me to save a PDF version of this report. My problem is that if i have the destination directory open on windows explorer, i get a runtime 2501 "The output To action was cancelled" error.

Is there any way to check if the directory is already open, and cancel the output to, or close the window to avoid this error?

Here is my code:
Code:
Private Sub CmdSavePDF_Click()
Dim MyFileName As String
Dim MyPath As String
'Dim CompanyName As String

'CompanyName = Me.CompanyName

 If Dir("S:\Contracts\Geomatics LAB\Templates\Drafts\" & Me.CompanyName, vbDirectory) = "" Then
        MkDir ("S:\Contracts\Geomatics LAB\Templates\Drafts\" & Me.CompanyName)
    Else
        'do nothing for the "C:\Text Files" directory already exists
        'MsgBox "S:\Contracts\Geomatics LAB\Templates\Drafts\ & Me.CompanyName directory already exists"
    End If

MyFileName = Format(Me.DateOutReq, "yyyy") & _
                 "-" & Format(Me.DateOutReq, "mm") & "-" & Format(Me.DateOutReq, "dd") & _
                 "-" & Me.CompanyName & "-ScheduleA" & ".pdf"

MyPath = "S:\Contracts\Geomatics LAB\Templates\Drafts\" & Me.CompanyName & "\"

DoCmd.OutputTo acOutputReport, "rptScheduleA", acFormatPDF, MyPath & MyFileName, False
End Sub
 
You could use simple error handling to check for that particular error.
 

Users who are viewing this thread

Back
Top Bottom