Consulting

Results 1 to 3 of 3

Thread: Help on Word VBA On Error

  1. #1

    Help on Word VBA On Error

    I create an on Error handler. I am trying to print out what the error was. I think I must have the variable names wrong. I'm not getting anything for Erl, Err.Number and Err.Description

    macOS 10.10.5. Word 2011 14.7.3 (170325) MS Visual Basic Version 14.7.3 (170325)

    Here is my code:
    Sub testErrorCode()
    10       Debug.Print "...===...beginning of program....===..." & Format(Now(), "dd/mm/yyyy hh:mm:ss.ms")
    340     On Error GoTo ErrHandler
    
    350     fileString = "Doc doesn't exist.doc"
    360     Debug.Print "fileString is " & fileString
    370     Set objDoc = Documents.Open(fileName:=fileString)
    
       
    ErrHandler:
            On Error GoTo -1  '  clear error
    
            '  https://www.fmsinc.com/free/newtips/VBA/ErrorHandling/LineNumber.html
    630     Debug.Print "--- error --- "
    640     boxTheMsg = "While processing document " & strFile & "   An error occured ." & vbCrLf4
    650     boxTheMsg = boxTheMsg & "Error Line: " & Erl & vbCrLf
    670     boxTheMsg = boxTheMsg & "Error: (" & Err.Number & ") " & Err.Description
    680     Debug.Print "boxTheMsg is " & boxTheMsg
    690     MsgBox boxTheMsg, vbCritical
            
    End Sub
    Here is what I see on the output.

    ...===...beginning of program....===...18/07/2018 16:42:31.731
    fileString is Doc doesn't exist.doc
    --- error --- 
    boxTheMsg is While processing document    An error occured .Error Line: 0
    Error: (0)
    See the attachment the uncaught message and what I got with the on error statement.

    Robert
    Attached Images Attached Images

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    You haven't supplied the path to the document.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    I'm trying to get the error handler to work, so of course I supplied an invalid document address.

    Robert

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •