Trapping a "Run Time Error 5408” when using a VBA macro

jstockton

New Member
Joined
Jun 23, 2006
Messages
3
Please Help,

I have run across a problem where I am forced to use a VBA macro in bringing up a word document (the word document is setup with a Password). When clicking on the macro, a pop up window will appear asking for a Password! I can view the word document (Read Only, which works great :-D ), but if click on “CANCEL” or the OK Button, A Microsoft Visual Basic Error Debug Pop Up Window will appear giving a error code of “Run Time Error 5408” :o . ( ***Please remember to have the word document setup with a Password.***)

Below is what I have. Is there a way to trap the error code?

Sub PRD77()

Dim appWord As New Word.Application
Dim docWord As Word.Document

Set docWord = appWord.Documents.Open("S:\VB2 Product Development\PRDs\PRD#77-

Instant VB2 User Registration\PRD#77-InstantVB2UserRegistration.doc")

appWord.Visible = True


End Sub

Please help...
:huh:
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Hello

This should work. You just have to tell Excel not to handle errors, but rather you will by adding the "On error resume next" to the beginning of your macro. You can then with the use of Err.number determine what the user pressed with the following code. I assumed you used the createobject function to create the instance of Word.


Code:
If Err.Number = 5408 Then
    wordobj.Quit
    MsgBox "Password supplied is incorrect"
End If
If Err.Number = 4198 Then
    wordobj.Quit
    MsgBox "User cancelled opening file"
End If
If Err.Number <> 0 Then
    wordobj.Quit
    MsgBox "Unhandeled exception, Microsoft Word terminated"
End If

Hope this helps, if not let me know.
 
Upvote 0

Forum statistics

Threads
1,224,938
Messages
6,181,869
Members
453,068
Latest member
DCD1872

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