M
Mike Iacovou
hi there
i used the word macro recorder to get the basic code to mail merge a letter
with excel data - its my first day fiddling with word vba.
the macro links to the data source ok. If i toggle the "merge data" button,
it correctly replaces fields on screen with data from the excel worksheet.
what is odd is that if i .printpreview or .printout the page, even when it
displays the correct info, i get {MERGEFIELD SURNAME} etc etc. If i merge to
new document, then it replaces most of the information (except embedded date
field) - but seems a bit odd that it doesn't work directly...
Code snippet:
ActiveDocument.MailMerge.OpenDataSource Name:=thefile _
, ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=True, _
AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _
WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False, _
Format:=wdOpenFormatAuto, Connection:= _
"Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data
Source="" & thefile & "";Mode=Read;Extended Properties=""HDR=NO;IMEX=1;"";Jet
OLEDB:System database="""";Jet OLED" _
, SQLStatement:="SELECT * FROM `Mailmerge$`", SQLStatement1:="",
SubType _
:=wdMergeSubTypeAccess
With ActiveDocument.MailMerge
.Destination = wdSendToPrinter
.SuppressBlankLines = True
With .DataSource
.FirstRecord = 1
.LastRecord = 10
End With
.Execute Pause:=False
End With
// end
"thefile" is the full path of the excel workbook.
comments / code etc greatly appreciated.
cheers
mike
i used the word macro recorder to get the basic code to mail merge a letter
with excel data - its my first day fiddling with word vba.
the macro links to the data source ok. If i toggle the "merge data" button,
it correctly replaces fields on screen with data from the excel worksheet.
what is odd is that if i .printpreview or .printout the page, even when it
displays the correct info, i get {MERGEFIELD SURNAME} etc etc. If i merge to
new document, then it replaces most of the information (except embedded date
field) - but seems a bit odd that it doesn't work directly...
Code snippet:
ActiveDocument.MailMerge.OpenDataSource Name:=thefile _
, ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=True, _
AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _
WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False, _
Format:=wdOpenFormatAuto, Connection:= _
"Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data
Source="" & thefile & "";Mode=Read;Extended Properties=""HDR=NO;IMEX=1;"";Jet
OLEDB:System database="""";Jet OLED" _
, SQLStatement:="SELECT * FROM `Mailmerge$`", SQLStatement1:="",
SubType _
:=wdMergeSubTypeAccess
With ActiveDocument.MailMerge
.Destination = wdSendToPrinter
.SuppressBlankLines = True
With .DataSource
.FirstRecord = 1
.LastRecord = 10
End With
.Execute Pause:=False
End With
// end
"thefile" is the full path of the excel workbook.
comments / code etc greatly appreciated.
cheers
mike