Hello
Does anyone know why I am getting the error message?
Thanks in advance for your help
------------------
Private Sub cmdWordLetter_Click()
On Error GoTo ErrorHandler
Dim appWord As Word.Application
Dim docs As Word.Documents
Dim strLetter As String
Dim prps As Object
Dim strDate As String
Dim fso As New Scripting.FileSystemObject
Dim fil As Scripting.File
Dim strTemplate As String
Dim strTemplatePath As String
Dim strTemplateNameAndPath As String
Dim doc As Word.Document
Dim strTitle As String
Dim strPrompt As String
strDate = CStr(Date)
'Check whether template is found in the folder
'Get User Templates path from Word Options dialog
'(or replace with hard-coded path for your computer)
Set appWord = GetObject(, "Word.Application")
'strTemplatePath = appWord.Options.DefaultFilePath(wdUserTemplatesPath)
'Debug.Print "Template path: " & strTemplatePath
strTemplatePath = strTemplatePath & "S:\NonCon\Business_Analytics\Andrea_Palermo\"
strLetter = "DocDE.dot"
strTemplateNameAndPath = strTemplatePath & strLetter
Debug.Print "Template and path: " & strTemplateNameAndPath
On Error Resume Next
Set fil = fso.GetFile(strTemplateNameAndPath)
If fil Is Nothing Then
strPrompt = "Can't find " & strLetter & " in " _
& strTemplatePath & "; canceling"
MsgBox strPrompt, vbCritical + vbOKOnly
GoTo ErrorHandlerExit
End If
On Error GoTo ErrorHandler
Set docs = appWord.Documents
Set doc = docs.Add(strTemplateNameAndPath)
Set prps = doc.CustomDocumentProperties
With prps
.Item("DEC").Value = Nz(Me![strDEC])
.Item("DATAELEMENT").Value = Nz(Me![strDEs])
.Item("DATAELEMENTSTATUS").Value = Nz(Me![strDEP])
.Item("SIGNOFF").Value = Nz(Me![strSignoff])
.Item("DESCRIPTION").Value = Nz(Me![strDesc])
End With
With appWord
.Visible = True
.Activate
.Selection.WholeStory
.Selection.Fields.Update
.Selection.MoveDown Unit:=wdLine, Count:=1
End With
ErrorHandlerExit:
Exit Sub
ErrorHandler:
If Err = 429 Then
'Word is not running; open Word with CreateObject
Set appWord = CreateObject("Word.Application")
Resume Next
Else
MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
Resume ErrorHandlerExit
End If
Does anyone know why I am getting the error message?
Thanks in advance for your help
------------------
Private Sub cmdWordLetter_Click()
On Error GoTo ErrorHandler
Dim appWord As Word.Application
Dim docs As Word.Documents
Dim strLetter As String
Dim prps As Object
Dim strDate As String
Dim fso As New Scripting.FileSystemObject
Dim fil As Scripting.File
Dim strTemplate As String
Dim strTemplatePath As String
Dim strTemplateNameAndPath As String
Dim doc As Word.Document
Dim strTitle As String
Dim strPrompt As String
strDate = CStr(Date)
'Check whether template is found in the folder
'Get User Templates path from Word Options dialog
'(or replace with hard-coded path for your computer)
Set appWord = GetObject(, "Word.Application")
'strTemplatePath = appWord.Options.DefaultFilePath(wdUserTemplatesPath)
'Debug.Print "Template path: " & strTemplatePath
strTemplatePath = strTemplatePath & "S:\NonCon\Business_Analytics\Andrea_Palermo\"
strLetter = "DocDE.dot"
strTemplateNameAndPath = strTemplatePath & strLetter
Debug.Print "Template and path: " & strTemplateNameAndPath
On Error Resume Next
Set fil = fso.GetFile(strTemplateNameAndPath)
If fil Is Nothing Then
strPrompt = "Can't find " & strLetter & " in " _
& strTemplatePath & "; canceling"
MsgBox strPrompt, vbCritical + vbOKOnly
GoTo ErrorHandlerExit
End If
On Error GoTo ErrorHandler
Set docs = appWord.Documents
Set doc = docs.Add(strTemplateNameAndPath)
Set prps = doc.CustomDocumentProperties
With prps
.Item("DEC").Value = Nz(Me![strDEC])
.Item("DATAELEMENT").Value = Nz(Me![strDEs])
.Item("DATAELEMENTSTATUS").Value = Nz(Me![strDEP])
.Item("SIGNOFF").Value = Nz(Me![strSignoff])
.Item("DESCRIPTION").Value = Nz(Me![strDesc])
End With
With appWord
.Visible = True
.Activate
.Selection.WholeStory
.Selection.Fields.Update
.Selection.MoveDown Unit:=wdLine, Count:=1
End With
ErrorHandlerExit:
Exit Sub
ErrorHandler:
If Err = 429 Then
'Word is not running; open Word with CreateObject
Set appWord = CreateObject("Word.Application")
Resume Next
Else
MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
Resume ErrorHandlerExit
End If