N
nath
Hi All,
I am trying to automate a mail merge from excel to word.
I have tried the following code which works in word, but
doesnt work using my word object (wrd)
Can anyone help.
Nath
Dim wrd As Word.Application
Dim doc As Word.Document
Set wrd = CreateObject("word.application")
Set doc = wrd.Documents.Add
doc.Select
With wrd
.Visible = True
.ActiveDocument.MailMerge.MainDocumentType =
wdMailingLabels
.ActiveDocument.MailMerge.OpenDataSource Name:= _
"Z:\Development\Engine\ver2.2\Data_For_Mege.xls",
ConfirmConversions:= _
False, ReadOnly:=False, LinkToSource:=True,
AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="",
WritePasswordDocument:="", _
WritePasswordTemplate:="", Revert:=False,
Format:=wdOpenFormatAuto, _
Connection:= _
"Provider=Microsoft.Jet.OLEDB.4.0;Password="""";Use
r ID=Admin;Data Source=Z:\Development\Engine\ver2.2
\Data_For_Mege.xls;Mode=Read;Extended
Properties=""HDR=YES;IMEX=1;"";Jet OLEDB:System
database="""";Jet OLEDB:Registry Path="""";Jet
OLEDBatabase Password="""";Je" _
, SQLStatement:="SELECT * FROM `Sheet1$`",
SQLStatement1:="", SubType:= _
wdMergeSubTypeAccess
.ActiveDocument.Fields.Add Range:=Selection.Range,
Type:=wdFieldMergeField _
, Text:="""Title"""
.Selection.TypeText Text:=" "
.ActiveDocument.Fields.Add Range:=Selection.Range,
Type:=wdFieldMergeField _
, Text:="""Forename"""
.Selection.TypeText Text:=" "
.ActiveDocument.Fields.Add Range:=Selection.Range,
Type:=wdFieldMergeField _
, Text:="""Surname"""
.Selection.TypeParagraph
.ActiveDocument.Fields.Add Range:=Selection.Range,
Type:=wdFieldMergeField _
, Text:="""Add_1"""
.Selection.TypeParagraph
.ActiveDocument.Fields.Add Range:=Selection.Range,
Type:=wdFieldMergeField _
, Text:="""Add_2"""
.Selection.TypeParagraph
.ActiveDocument.Fields.Add Range:=Selection.Range,
Type:=wdFieldMergeField _
, Text:="""Add3"""
.Selection.TypeParagraph
.ActiveDocument.Fields.Add Range:=Selection.Range,
Type:=wdFieldMergeField _
, Text:="""Add_4"""
.Selection.TypeParagraph
.ActiveDocument.Fields.Add Range:=Selection.Range,
Type:=wdFieldMergeField _
, Text:="""PostCode"""
.WordBasic.MailMergePropagateLabel
End With
I am trying to automate a mail merge from excel to word.
I have tried the following code which works in word, but
doesnt work using my word object (wrd)
Can anyone help.
Nath
Dim wrd As Word.Application
Dim doc As Word.Document
Set wrd = CreateObject("word.application")
Set doc = wrd.Documents.Add
doc.Select
With wrd
.Visible = True
.ActiveDocument.MailMerge.MainDocumentType =
wdMailingLabels
.ActiveDocument.MailMerge.OpenDataSource Name:= _
"Z:\Development\Engine\ver2.2\Data_For_Mege.xls",
ConfirmConversions:= _
False, ReadOnly:=False, LinkToSource:=True,
AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="",
WritePasswordDocument:="", _
WritePasswordTemplate:="", Revert:=False,
Format:=wdOpenFormatAuto, _
Connection:= _
"Provider=Microsoft.Jet.OLEDB.4.0;Password="""";Use
r ID=Admin;Data Source=Z:\Development\Engine\ver2.2
\Data_For_Mege.xls;Mode=Read;Extended
Properties=""HDR=YES;IMEX=1;"";Jet OLEDB:System
database="""";Jet OLEDB:Registry Path="""";Jet
OLEDBatabase Password="""";Je" _
, SQLStatement:="SELECT * FROM `Sheet1$`",
SQLStatement1:="", SubType:= _
wdMergeSubTypeAccess
.ActiveDocument.Fields.Add Range:=Selection.Range,
Type:=wdFieldMergeField _
, Text:="""Title"""
.Selection.TypeText Text:=" "
.ActiveDocument.Fields.Add Range:=Selection.Range,
Type:=wdFieldMergeField _
, Text:="""Forename"""
.Selection.TypeText Text:=" "
.ActiveDocument.Fields.Add Range:=Selection.Range,
Type:=wdFieldMergeField _
, Text:="""Surname"""
.Selection.TypeParagraph
.ActiveDocument.Fields.Add Range:=Selection.Range,
Type:=wdFieldMergeField _
, Text:="""Add_1"""
.Selection.TypeParagraph
.ActiveDocument.Fields.Add Range:=Selection.Range,
Type:=wdFieldMergeField _
, Text:="""Add_2"""
.Selection.TypeParagraph
.ActiveDocument.Fields.Add Range:=Selection.Range,
Type:=wdFieldMergeField _
, Text:="""Add3"""
.Selection.TypeParagraph
.ActiveDocument.Fields.Add Range:=Selection.Range,
Type:=wdFieldMergeField _
, Text:="""Add_4"""
.Selection.TypeParagraph
.ActiveDocument.Fields.Add Range:=Selection.Range,
Type:=wdFieldMergeField _
, Text:="""PostCode"""
.WordBasic.MailMergePropagateLabel
End With