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
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