Share via

vba Error handling - document.add

Charles Kenyon 167.7K Reputation points Volunteer Moderator
2011-11-17T12:52:04+00:00

I am running a procedure to create a new document based on a template. I want to put in error handling for the case when the template is not found. The following is what I have and it is not working. Word is instead generating a general error message.

This being developed in Word 2003 for use in Word 2003-2010.

Private Sub NewDocFromTemplate(sTemplateName As String)

'

' NewDocFromTemplate procedure

'   Creates new document based on template in folder

'   Calls WorkgroupPath Function to get Workgroup path

'   This is called by individual macros to create a new document based on the named template

'

    Dim sTemplatesPath As String

    On Err GoTo OopsMessage

    sTemplatesPath = WorkGroupPath & "CrmDef11"

'

    Documents.Add Template:= _

        sTemplatesPath & sTemplateName _

        , NewTemplate:=False

'

Exit Sub

OopsMessage:

    MsgBox "The template " & sTemplateName & _

        " cannot be found in the folder " & _

        sTemplatesPath, vbInformation, "Template not found!"

'

End Sub

It runs fine so long as sTemplateName specifies a template in sTemplatesPath. When it does not, I had expected OopsMessage to kick in.

Instead I get a runtime error 5151 telling the user that the document may be corrupt, etc.

The following procedures are being used to test this:

Sub ErrTest()

' Error test for template not in location

' Macro written 11/17/11 by Charles Kyle Kenyon

'

    Dim sTempName As String

    sTempName = "01_063 - Checklist for the Initial Appearance.dot"

    NewDocFromTemplate (sTempName)

End Sub

Sub Doc01_062()

' Doc01_062 - Checklist for the Initial Appearance

' Macro written 11/12/11 by Charles Kyle Kenyon

'

    Dim sTempName As String

    sTempName = "01_062 - Checklist for the Initial Appearance.dot"

    NewDocFromTemplate (sTempName)

End Sub

There is a template with the second name but not the first.

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Jay Freedman 207.7K Reputation points Volunteer Moderator
2011-11-17T16:11:54+00:00

Hi Charles,

The error trap isn't working because you should write "On Error" instead of "On Err". (Hey, that's the kind of mistake I make all the time!)

Because this is a predictable error, though, it's better to program defensively to avoid the situation. In this case, all it takes is the following statement after the assignment of sTemplatesPath.

    If Dir(sTemplatesPath & sTemplateName) = "" Then GoTo OopsMessage

You may then want to direct the On Error trap to a different error message, since it will catch other errors but not a "template not found" condition.

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Charles Kenyon 167.7K Reputation points Volunteer Moderator
    2011-11-17T18:14:24+00:00

    Thank you Jay.

    Was this answer helpful?

    0 comments No comments