mario_h
02-27-2005, 10:47 AM
I am running this code in Access and I get Error 462 when I run the code more then once.
I only get the error when I add fields to the word document.
Adding properties does not cause any errors.
It only happens on the second or subsequent times
Quitting Access releases whatever does not get released in code.
I use Office 2000 pro and XP SP2
Please help.
Mario
Public Sub CreateTemplate()
On Error GoTo HandleError
Dim oWord As Word.Application
Dim oDocs As Word.Documents
Dim oFields As Word.Fields
Dim oPrps As Object
Dim strWordTemplate As String
Dim strTemplatePath As String
Set oWord = GetObject(, "Word.Application")
strTemplatePath = oWord.Options.DefaultFilePath(wdUserTemplatesPath) & "\"
strWordTemplate = strTemplatePath & "\Normal.dot"
Set oDocs = oWord.Documents
oDocs.add Template:=strWordTemplate, _
NewTemplate:=True, _
DocumentType:=0
Set oPrps = oWord.ActiveDocument.CustomDocumentProperties
oPrps.add Name:="fldTest", _
LinkToContent:=False, _
type:=msoPropertyTypeString, _
value:="valTest"
Set oFields = oWord.ActiveDocument.Fields
'This causes the error: 462 The remote server machine does not exist or is unavailable
'Quitting Access solves the problem
'Something is not released. What?
oFields.add Range:=Selection.Range, _
type:=wdFieldEmpty, text:= _
"DOCPROPERTY ""fldTest"" ", _
PreserveFormatting:=True
With oWord
.ActiveDocument.SaveAs filename:="Test.dot", _
FileFormat:=wdFormatTemplate, _
LockComments:=False, _
Password:="", _
AddToRecentFiles:=True, _
WritePassword:="", _
ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:=False, _
SaveNativePictureFormat:=False, _
SaveFormsData:=False, _
SaveAsAOCELetter:=False
End With
ExitHere:
Set oFields = Nothing
Set oPrps = Nothing
Set oDocs = Nothing
oWord.Quit
Set oWord = Nothing
Exit Sub
HandleError:
If Err.Number = 429 Then
Set oWord = CreateObject("Word.Application")
Resume Next
Else
MsgBox Err.Number & " " & Error(Err.Number)
Resume ExitHere:
End If
End Sub
I only get the error when I add fields to the word document.
Adding properties does not cause any errors.
It only happens on the second or subsequent times
Quitting Access releases whatever does not get released in code.
I use Office 2000 pro and XP SP2
Please help.
Mario
Public Sub CreateTemplate()
On Error GoTo HandleError
Dim oWord As Word.Application
Dim oDocs As Word.Documents
Dim oFields As Word.Fields
Dim oPrps As Object
Dim strWordTemplate As String
Dim strTemplatePath As String
Set oWord = GetObject(, "Word.Application")
strTemplatePath = oWord.Options.DefaultFilePath(wdUserTemplatesPath) & "\"
strWordTemplate = strTemplatePath & "\Normal.dot"
Set oDocs = oWord.Documents
oDocs.add Template:=strWordTemplate, _
NewTemplate:=True, _
DocumentType:=0
Set oPrps = oWord.ActiveDocument.CustomDocumentProperties
oPrps.add Name:="fldTest", _
LinkToContent:=False, _
type:=msoPropertyTypeString, _
value:="valTest"
Set oFields = oWord.ActiveDocument.Fields
'This causes the error: 462 The remote server machine does not exist or is unavailable
'Quitting Access solves the problem
'Something is not released. What?
oFields.add Range:=Selection.Range, _
type:=wdFieldEmpty, text:= _
"DOCPROPERTY ""fldTest"" ", _
PreserveFormatting:=True
With oWord
.ActiveDocument.SaveAs filename:="Test.dot", _
FileFormat:=wdFormatTemplate, _
LockComments:=False, _
Password:="", _
AddToRecentFiles:=True, _
WritePassword:="", _
ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:=False, _
SaveNativePictureFormat:=False, _
SaveFormsData:=False, _
SaveAsAOCELetter:=False
End With
ExitHere:
Set oFields = Nothing
Set oPrps = Nothing
Set oDocs = Nothing
oWord.Quit
Set oWord = Nothing
Exit Sub
HandleError:
If Err.Number = 429 Then
Set oWord = CreateObject("Word.Application")
Resume Next
Else
MsgBox Err.Number & " " & Error(Err.Number)
Resume ExitHere:
End If
End Sub