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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding mail merge fields into word doc from access VBA

Status
Not open for further replies.

RichyT

Technical User
Jun 11, 2002
20
GB
I have set up some VBA coding behind a button in access that opens a new word document and merges in data from a table in the database I am working with. Unfortunately, I also want it to add certain fields onto the document after it is created, ultimately giving you a new letter with the address already at the top. I can do this from a macro in word, but when I try and refer to the new word document I get the error 'Object variable or with block variable not set'. My code is as follows:

Private Sub MailMerge_Click()

Dim objWord As Word.Application
Dim docWord As Word.Document

Set objWord = CreateObject("Word.Application")

Set docWord = objWord.Documents.Add
' Make Word Visible.
objWord.Application.Visible = True
' Set the Mail Merge data Source.
docWord.MailMerge.OpenDataSource Name:="C:\MyData\Client Database\clients2000(Rich).mdb", _
LinkToSource:=True, Connection:="TABLE MergeAddresses", SQLStatement:="Select * from [MergeAddresses]"

docWord.MailMerge.Fields.Add Range:=Selection.Range, Name:= _
"contitle"
Selection.TypeText Text:=" "
docWord.MailMerge.Fields.Add Range:=Selection.Range, Name:= _
"confore" ...........

the line 'docWord.MailMerge.Fields.Add ...' is the problem line. I think that I may be refering to the word document incorrectly.

Could anyone please help.

Cheers

Rich
 
why don't you just run the macro

Open Word, open Doc, then run macro
see this faq on whow to do that
faq181-28 DougP, MCP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top