Results 1 to 5 of 5
  1. #1
    Star Lounger
    Join Date
    May 2009
    Posts
    84
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need help with simple error handler code - (VBA included)

    Word 2002
    This simple macro opens a pre-made Word document, copies a text box, and pastes it into a new document. This is used for stamping documents. I want to add a simple error handling statement that displays a custom message if a file to be copied is not available. My problem is that the code executes every time the macro runs. I've stepped through the code and the error handler executes everytime and the error message box displays. I want it to display only if an error occurs. I'm sure it's something simple but I can not determine what. What do you advise?

    Thanks for the help!
    Charlie
    charlie6067

    Sub CurrentDate()

    On Error GoTo Errhandler

    Documents.Open FileName:="""H:\E-Stamp Project - DO NOT DELETE\Filed.doc""", ConfirmConversions:= _
    False, ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:="", _
    PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
    WritePasswordTemplate:="", Format:=wdOpenFormatAuto
    ActiveDocument.Shapes.SelectAll

    Selection.Copy

    ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges


    'Pastes clipboard in document.
    Selection.Paste

    End If

    Errhandler:
    MsgBox "This stamp is not be working correctly. Please report this error to the Help Desk."


    End Sub

  2. #2
    3 Star Lounger
    Join Date
    Apr 2012
    Posts
    240
    Thanks
    3
    Thanked 24 Times in 24 Posts
    Your Errormessage directly follows an if statement, there's nothing to route the code around it.
    You should have a goto statement just after your end if to say goto end.

  3. #3
    Star Lounger
    Join Date
    May 2009
    Posts
    84
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Big Mac! That worked.

  4. #4
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts
    Add Exit Sub above Errhandler:

    If the routine runs normally, it will exit before getting to Errhandler:.

  5. #5
    Star Lounger
    Join Date
    May 2009
    Posts
    84
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, Richard. That worked.

Posting Permissions

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