Consulting

Results 1 to 3 of 3

Thread: Help on Word VBA On Error

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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()
        Debug.Print "...===...beginning of program....===..." & Format(Now(), "dd/mm/yyyy hh:mm:ss.ms")
        On Error GoTo ErrHandler
        fileString = "Doc doesn't exist.doc"
        Debug.Print "fileString is " & fileString
        Set objDoc = Documents.Open(fileName:=fileString)
        ErrHandler:
        On Error GoTo -1  '  clear error
        '  https://www.fmsinc.com/free/newtips/VBA/ErrorHandling/LineNumber.html
        Debug.Print "--- error --- "
        boxTheMsg = "While processing document " & strFile & "   An error occured ." & vbCrLf4
        boxTheMsg = boxTheMsg & "Error Line: " & Erl & vbCrLf
        boxTheMsg = boxTheMsg & "Error: (" & Err.Number & ") " & Err.Description
        Debug.Print "boxTheMsg is " & boxTheMsg
        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
    Last edited by Aussiebear; 12-20-2024 at 04:42 PM.

  2. #2
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,273
    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
  •