Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mail Merge Assistance

Status
Not open for further replies.

Aidy680

Technical User
Nov 1, 2006
138
GB
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!
 
Crikey you can tell I didnt get much sleep last night!

OK obviously the:

objWord.Close

command is the reason why the document closes. Stupid of me I know.....

Also I didnt have the DB in the correct folder. I've sorted both of these and now what's happening is that another Word doc - "Form Letters" - opens but still with none of the bookmarks populated.

I imagine the other doc opening is fine as it's a copy of the original but why no populated bookmarks? Are my references to the table/fields incorrect?
 
Right as an update the proc is producing the right number of pages (14 pages 14 records in table) so it really is then just populating the bookmarks.

Also ultimately I want to do this using a query; I believe I can do this by substituting the table info. Should I then leave out the "Connection" detail?
 
I'm wondering if bookmarks are the way to go here....?

The right number of pages are being displayed ie. one per record in the table/query.

I suppose the question is how do I display the data in the new document?

Someone pleeeaasseee put me out of my misery!
 
I think you should play with MailMergeFields instead of Bookmarks.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Uncanny. This is exactly what I tried this morning and it works. Amazing what sleeping on a problem can do. Feel stupid now....

Thanks for your help PHV.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top