• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1453
  • Last Modified:

Access: Use of VBA to check if pdf report is open or not

I have a button in my form which creates and open a pdf document of my report, already filtered and sorted,  but it gives an error if you attempt to create another pdf with the previous one still open.  the error message is:

"The OutputTo action was canceled."

If the open pdf is closed, then the command runs fine.  My Question:
How can i modify my code to check if the pdf file is already open and close it, or pops a message box advising the user to close the open pdf before running the command again? here is my code for creating a pdf document from a report in my access Form.

Private Sub CmdPreviewReport_Click()

Dim stDocName As String
stDocName = "Request_Report" ' Name of Report
strLinkCriteria = Me.MainWorkloadQueryForm.form.Filter

'Opening and Previewing filtered Report
DoCmd.OpenReport stDocName, acViewPreview, , strLinkCriteria, acHidden, Me.MainWorkloadQueryForm.form.OrderBy
DoCmd.SelectObject acReport, stDocName

'Obtaining Data from filtered report and converting report to PDF using DoCmd.OutputTo
DoCmd.OutputTo acOutputReport, "Request_Report", acFormatPDF, "Request_Report.pdf", True

'Closes the Report in Access leaving just the opened PDF
DoCmd.Close acReport, stDocName, acSaveNo
End Sub
0
peterslove53
Asked:
peterslove53
  • 2
1 Solution
 
Nick67Commented:
This works. Pass in a full path to a file
From http://support.microsoft.com/kb/291295
Tested in Access 2003 with a pdf file

' This function checks to see if a file is open or not. If the file is
' already open, it returns True. If the file is not open, it returns
' False. Otherwise, a run-time error occurs because there is
' some other problem accessing the file.

Function IsFileOpen(filename As String)
    Dim filenum As Integer, errnum As Integer

    On Error Resume Next   ' Turn error checking off.
    filenum = FreeFile()   ' Get a free file number.
    ' Attempt to open the file and lock it.
    Open filename For Input Lock Read As #filenum
    Close filenum          ' Close the file.
    errnum = Err           ' Save the error number that occurred.
    On Error GoTo 0        ' Turn error checking back on.

    ' Check to see which error occurred.
    Select Case errnum

        ' No error occurred.
        ' File is NOT already open by another user.
        Case 0
         IsFileOpen = False

        ' Error number for "Permission Denied."
        ' File is already opened by another user.
        Case 70
            IsFileOpen = True

        ' Another error occurred.
        Case Else
            Error errnum
    End Select

End Function

Open in new window

0
 
aikimarkCommented:
Change the name of the PDF to include a timestamp
Example: Request_Report_20150219164235.PDF
0
 
peterslove53Author Commented:
Hi aikimark, That is also a good idea!
0
 
peterslove53Author Commented:
I finally decided to add a message box telling the user another report was open and needs to be closed. This seems to suit me:

Private Sub CmdPreviewReport_Click()
On Error GoTo Err_CmdPreviewReport_Click

Dim stDocName As String
stDocName = "Request_Report" ' Name of Report
strLinkCriteria = Me.MainWorkloadQueryForm.form.Filter

'Opening and Previewing filtered Report
DoCmd.OpenReport stDocName, acViewPreview, , strLinkCriteria, acHidden, Me.MainWorkloadQueryForm.form.OrderBy
DoCmd.SelectObject acReport, stDocName



'Obtaining Data from filtered report and converting report to PDF using DoCmd.OutputTo
DoCmd.OutputTo acOutputReport, "Request_Report", acFormatPDF, "Request_Report.pdf", True

'Closes the Report in Access leaving just the opened PDF
DoCmd.Close acReport, stDocName, acSaveNo

Exit_CmdPreviewReport_Click:
    Exit Sub

Err_CmdPreviewReport_Click:
    MsgBox "The Report Is open already!" & vbCrLf & "Close the report and try again", , "Close Open Report"
    'MsgBox Err.Description
    DoCmd.Close acReport, stDocName, acSaveNo
    Resume Exit_CmdPreviewReport_Click
End Sub
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now