I have a word document that I am merging data into from access. The first time I can open and merge data fine, but if the user closes the word doc, I get an automation error when I try to reopen it the second time for another merge. If I restart access, no problem.
Here is my code.
Public Function OpenCatalog()
'Start Word and get template info
On Error Resume Next
Set objWord = GetObject(, "Word.Application"
If Err.Number = 429 Then
Set objWord = CreateObject("Word.Application"
objWord.Application.Visible = True
Err.Clear
'this is a template I copy bookmarks from
objWord.Documents.Add Template:=strTemplPath & "Merge1.dot"
'the automation error occurs on the next line the second time although word is opened and the template appears
ActiveDocument.Range.Copy
ActiveDocument.Close
'open my current catalog.doc
objWord.Documents.Open (strTemplPath & "catalog.doc"
AppActivate "Microsoft Word", 1
'go to the end and paste bookmarks
Set myrange = ActiveDocument.Range(Start:=ActiveDocument.Content.End - 1, End:=ActiveDocument.Content.End - 1)
myrange.Paste
ElseIf Err.Number <> 0 Then
MsgBox Err.Number & Err.Description & "An unexpected error has occurred."
Exit Function
Else 'doc is still open
Set myrange = ActiveDocument.Range(Start:=ActiveDocument.Content.End - 1, End:=ActiveDocument.Content.End - 1)
myrange.Paste
End If
End Function
after I do the merge I open the catalog to review the additions as follows:
With objWord.ActiveDocument
.Fields.Update
End With
ActiveDocument.Save
AppActivate "Microsoft Word", 1
Set objWord = Nothing
end sub
Any ideas would be greatly appreciated!
Beth
Here is my code.
Public Function OpenCatalog()
'Start Word and get template info
On Error Resume Next
Set objWord = GetObject(, "Word.Application"
If Err.Number = 429 Then
Set objWord = CreateObject("Word.Application"
objWord.Application.Visible = True
Err.Clear
'this is a template I copy bookmarks from
objWord.Documents.Add Template:=strTemplPath & "Merge1.dot"
'the automation error occurs on the next line the second time although word is opened and the template appears
ActiveDocument.Range.Copy
ActiveDocument.Close
'open my current catalog.doc
objWord.Documents.Open (strTemplPath & "catalog.doc"
AppActivate "Microsoft Word", 1
'go to the end and paste bookmarks
Set myrange = ActiveDocument.Range(Start:=ActiveDocument.Content.End - 1, End:=ActiveDocument.Content.End - 1)
myrange.Paste
ElseIf Err.Number <> 0 Then
MsgBox Err.Number & Err.Description & "An unexpected error has occurred."
Exit Function
Else 'doc is still open
Set myrange = ActiveDocument.Range(Start:=ActiveDocument.Content.End - 1, End:=ActiveDocument.Content.End - 1)
myrange.Paste
End If
End Function
after I do the merge I open the catalog to review the additions as follows:
With objWord.ActiveDocument
.Fields.Update
End With
ActiveDocument.Save
AppActivate "Microsoft Word", 1
Set objWord = Nothing
end sub
Any ideas would be greatly appreciated!
Beth