Results 1 to 4 of 4
  1. #1
    templeowls is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2019
    Posts
    305

    Add error code for export VBA if PDF is already open?

    I have code for a button that exports a report to a PDF. It works flawlessly, however some users have trouble with it. These users export this report a couple times a day and save over the previous pdf under the same name, and unfortunately, they sometimes leave the previous PDF open. This doesn't allow access to overwrite it and access then shows a 2501 output error.

    No matter how many times I tell them, these users think that this error means something is wrong with access and not the fact that they have a pdf open that they are trying to overwrite. Can anyone help modify my code so that it handles this error with a simple "hey the pdf is open" message instead of the 2501 output error message? Thanks!

    Code:
    Private Sub btnExportReport_Click()    
        Dim reportName As String
        Dim fd As Object
        Dim filename As String
       
        reportName = "rptComplaintReview"
    
    
        Set fd = Application.FileDialog(2)
        filename = "Complaint Review" & " " & Format(Date, "mm.dd.yyyy") & ".pdf"
        
        With fd
            .Title = "Save to PDF"
            .InitialFileName = "\Documents\" & filename
            If .Show = -1 Then
                filename = fd.SelectedItems(1)
                If InStr(filename, ".") = 0 Then
                    filename = filename & ".pdf"
                ElseIf Right(filename, 4) <> ".pdf" Then
                    k = InStrRev(filename, ".") - 1
                    filename = Left(filename, k)
                    filename = filename & ".pdf"
                End If
            
            DoCmd.OutputTo acOutputReport, reportName, acFormatPDF, filename
    
    
            MsgBox "Report saved to " & filename
            
            End If
        
        End With
    
    
        Set fd = Nothing
    End Sub


  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    For error handling example, review http://allenbrowne.com/ser-23a.html
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,165
    Code:
    Private Sub btnExportReport_Click()
    On Error GoTo ErrHandler
        Dim reportName As String
        Dim fd As Object
        Dim filename As String
       
        reportName = "rptComplaintReview"
    
    
        Set fd = Application.FileDialog(2)
        filename = "Complaint Review" & " " & Format(Date, "mm.dd.yyyy") & ".pdf"
        
        With fd
            .Title = "Save to PDF"
            .InitialFileName = "\Documents\" & filename
            If .Show = -1 Then
                filename = fd.SelectedItems(1)
                If InStr(filename, ".") = 0 Then
                    filename = filename & ".pdf"
                ElseIf Right(filename, 4) <> ".pdf" Then
                    k = InStrRev(filename, ".") - 1
                    filename = Left(filename, k)
                    filename = filename & ".pdf"
                End If
            
            DoCmd.OutputTo acOutputReport, reportName, acFormatPDF, filename
    
    
            MsgBox "Report saved to " & filename
            
            End If
        
        End With
        
    ExitHandler:
        Set fd = Nothing
        Exit Sub
        
    ErrHandler:
        If Err.Number = 2501 Then
            MsgBox "Hey the pdf is already open, knucklehead."
        Else
            MsgBox "Error #" & Err.Number & " - " & Err.Description, , "Export Error"
        End If
        
        Resume ExitHandler
    End Sub

  4. #4
    templeowls is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2019
    Posts
    305
    Quote Originally Posted by kd2017 View Post
    Code:
    Private Sub btnExportReport_Click()
    On Error GoTo ErrHandler
        Dim reportName As String
        Dim fd As Object
        Dim filename As String
       
        reportName = "rptComplaintReview"
    
    
        Set fd = Application.FileDialog(2)
        filename = "Complaint Review" & " " & Format(Date, "mm.dd.yyyy") & ".pdf"
        
        With fd
            .Title = "Save to PDF"
            .InitialFileName = "\Documents\" & filename
            If .Show = -1 Then
                filename = fd.SelectedItems(1)
                If InStr(filename, ".") = 0 Then
                    filename = filename & ".pdf"
                ElseIf Right(filename, 4) <> ".pdf" Then
                    k = InStrRev(filename, ".") - 1
                    filename = Left(filename, k)
                    filename = filename & ".pdf"
                End If
            
            DoCmd.OutputTo acOutputReport, reportName, acFormatPDF, filename
    
    
            MsgBox "Report saved to " & filename
            
            End If
        
        End With
        
    ExitHandler:
        Set fd = Nothing
        Exit Sub
        
    ErrHandler:
        If Err.Number = 2501 Then
            MsgBox "Hey the pdf is already open, knucklehead."
        Else
            MsgBox "Error #" & Err.Number & " - " & Err.Description, , "Export Error"
        End If
        
        Resume ExitHandler
    End Sub

    THis worked! Thanks so much

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 9
    Last Post: 01-29-2019, 05:28 PM
  2. Replies: 3
    Last Post: 02-06-2015, 03:22 PM
  3. Replies: 6
    Last Post: 10-04-2014, 12:22 PM
  4. Error in Open/Browse folder code in access 2010
    By stigmatized in forum Programming
    Replies: 4
    Last Post: 10-01-2014, 01:55 PM
  5. Replies: 2
    Last Post: 02-26-2010, 08:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums