I'm trying to merge the records within a table into a Word template containing two bookmarks using the following code:
Dim objWord As Word.Document
Set objWord = GetObject("C:\DBTemp\Doc1.dot", "Word.Document")
' Make Word visible.
objWord.Application.Visible = True
objWord.MailMerge.OpenDataSource _
Name:="C:\DBTemp\MyDatabase.mdb", _
LinkToSource:=True, _
Connection:="Table Regions", _
SQLStatement:="Select * from [Regions]"
objWord.ActiveDocument.Bookmarks("Test1").Select
objWord.Selection.Text = [Regions]![RegionID]
objWord.ActiveDocument.Bookmarks("Test2").Select
objWord.Selection.Text = [Regions]![RegionName]
objWord.MailMerge.Destination = wdSendToNewDocument
'Execute the mail merge.
objWord.MailMerge.Execute
objWord.Close
Set objWord = Nothing
However I must be doing something wrong as it doesn't work. Word opens initially but then the document disappears by the end of the procedure.
Also if I step through the code my bookmarks aren't populated with actual data from the table.
Can someone please help!
Dim objWord As Word.Document
Set objWord = GetObject("C:\DBTemp\Doc1.dot", "Word.Document")
' Make Word visible.
objWord.Application.Visible = True
objWord.MailMerge.OpenDataSource _
Name:="C:\DBTemp\MyDatabase.mdb", _
LinkToSource:=True, _
Connection:="Table Regions", _
SQLStatement:="Select * from [Regions]"
objWord.ActiveDocument.Bookmarks("Test1").Select
objWord.Selection.Text = [Regions]![RegionID]
objWord.ActiveDocument.Bookmarks("Test2").Select
objWord.Selection.Text = [Regions]![RegionName]
objWord.MailMerge.Destination = wdSendToNewDocument
'Execute the mail merge.
objWord.MailMerge.Execute
objWord.Close
Set objWord = Nothing
However I must be doing something wrong as it doesn't work. Word opens initially but then the document disappears by the end of the procedure.
Also if I step through the code my bookmarks aren't populated with actual data from the table.
Can someone please help!