ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Real problems automating a mail merge (https://www.excelbanter.com/excel-programming/301019-real-problems-automating-mail-merge.html)

nath

Real problems automating a mail merge
 
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.xl s",
ConfirmConversions:= _
False, ReadOnly:=False, LinkToSource:=True,
AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="",
WritePasswordDocument:="", _
WritePasswordTemplate:="", Revert:=False,
Format:=wdOpenFormatAuto, _
Connection:= _
"Provider=Microsoft.Jet.OLEDB.4.0;Password="""";Us e
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
OLEDB:Database 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


All times are GMT +1. The time now is 05:54 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com