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.