Way of trap the error code when using 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. I can view the word document (which works great :-P ), but if I “CANCEL” to view the word document, I received a Run Time Error 5408 :o . 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

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
I ran your code and do not have the option to cancel. Please explain in what context you are given an option to cancel opening or viewing the work doc...
 
Upvote 0
Thank you so much for replying,

Have the word document setup for with a Password (Read Only). When you click on your macro, a pop up window will appear asking for a Password, Click on Cancel or the OK button. A Microsoft Visual Basic error debug pop up window will appear giving an Error Code of “Run-Time error 5408”.


Please help.
 
Upvote 0
Hi,

You could use a little bit of deferred error handling. Below is an example I just knocked up:

Code:
Sub PRD77()

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

    On Error Resume Next
    Set docWord = appWord.Documents.Open("C:\temp\mydoc.doc")
    On Error GoTo 0

    If docWord Is Nothing Then
        'Document not opened.  Error code here
        appWord.Quit
        Set appWord = Nothing
        MsgBox "User cancelled"
    Else
        appWord.Visible = True
    End If

End Sub

Hope this helps,
Daniel
 
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