Error Handling

mtagliaferri

Board Regular
Joined
Oct 27, 2004
Messages
156
The below code is working, however once it has run and the pdf files are open, if the code is run again will display error '-2147018887 (80071779)'
I am trying to solve this by handling the error and display the message to close the documents.
All sheets are hidden with the exclusion of the main one named Dashboard, for some reason when the handle errorhandle runs, when clicking on OK on the error message the first sheet from the code (AcceptanceOfResignation) becomes visible and selected.

How can I handle this error in a way that when acknowledging the error message by the user it returns to the Dashboard sheet and all other sheets remain invisible?
VBA Code:
Sub CreateOpenPDF()

    Application.ScreenUpdating = False
    
On Error GoTo ErrorHandle

        ShAcceptanceOfResignation.Visible = True
        
            With ShAcceptanceOfResignation
            .ExportAsFixedFormat xlTypePDF, Environ("Userprofile") & "\Documents\" & .Range("B16") & " " & .Range("B17") & " " & _
             Format(.Range("M26"), "YYYYMMDD") & " " & .Name & ".pdf", OpenAfterPublish:=True
    
       ShAcceptanceOfResignation.Visible = False
       
        End With
 
        ShLeaversForm.Visible = True
        
            With ShLeaversForm
            .ExportAsFixedFormat xlTypePDF, Environ("Userprofile") & "\Documents\" & .Range("D12") & " " & .Range("D13") & " " & _
             Format(.Range("E19"), "YYYYMMDD") & " " & .Name & ".pdf", OpenAfterPublish:=True
            
        ShLeaversForm.Visible = False
        
        End With
 
        ShCompanyPropertyChecklist.Visible = True
           
            With ShCompanyPropertyChecklist
            .ExportAsFixedFormat xlTypePDF, Environ("Userprofile") & "\Documents\" & .Range("D11") & " " & .Range("D12") & " " & _
             Format(.Range("J13"), "YYYYMMDD") & " " & .Name & ".pdf", OpenAfterPublish:=True
             
        ShCompanyPropertyChecklist.Visible = False
    
    End With

ErrorHandle:
     Select Case Err.Number
        Case -2147018887
            MsgBox "Please close all created documents that are open before creating new ones"
        Case Else
    Exit Sub
        End Select
    Application.ScreenUpdating = True
    
  End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Do you want the PDFs to be open after the code runs?
 
Upvote 0
Do you want the PDFs to be open after the code runs?
No, the preference is that any PDF created via the vba must be closed before creating again a set of PDFs therefore acknoledging the message it returns to the Dashboard view and all other sheets hidden, the user will close the PDFs and then return to Excel to run the VBA
 
Upvote 0
If you set OpenAfterPublish to False then the PDFs won't open after they've been created.
 
Upvote 0
If you set OpenAfterPublish to False then the PDFs won't open after they've been created.
The preference is to open the PDFs that have been created.
I would like to handle the error message to remind to close them.
The message works up to the point the OK option is pressed then it selects the hidden sheet setting it to visible.
I want to avoid this by exiting the sub and stay on the Dashboard sheet.

How can I achieve this?
 
Upvote 0

Forum statistics

Threads
1,215,227
Messages
6,123,743
Members
449,116
Latest member
alexlomt

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top