Automating Mail Merge in Word

R

RalphK

I am trying to automate a mail merge in Word. I am using Office 2003. My
source document is a Microsoft Excel file. My target document is a Word mail
merge document. I have done the steps manually and recorded the following
macro:

Option Explicit

Sub Fennessey1()
'
' Fennessey1 Macro
' Macro recorded 10/31/2006 by Ralph K
'
Documents.Open FileName:="""1 Project Engagement Description.doc""", _
ConfirmConversions:=False, ReadOnly:=False, AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="", Revert:=False, _
WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _
wdOpenFormatAuto, XMLTransform:=""
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=False
End With
ActiveDocument.SaveAs FileName:="Test1.doc",
FileFormat:=wdFormatDocument, _
LockComments:=False, Password:="", AddToRecentFiles:=True,
WritePassword _
:="", ReadOnlyRecommended:=True, EmbedTrueTypeFonts:=False, _
SaveNativePictureFormat:=False, SaveFormsData:=False,
SaveAsAOCELetter:= _
False
End Sub

When I play back the macro, it chokes on the following line:

.Destination = wdSendToNewDocument

with runtime error 5852. Requested object is not available.

Can anyone tell me what is wrong.

RalphK
 
D

Doug Robbins - Word MVP

Are you sure that "1 Project Engagement Description.doc" is a mail merge
main document with a datasource attached to it?

You may need to include:

.MainDocumentType = wdFormLetters
.OpenDataSource "Drive:\Path\Filename"


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
L

Lüko Willms

Am Tue, 31 Oct 2006 17:14:02 UTC, schrieb RalphK
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
When I play back the macro, it chokes on the following line:

.Destination = wdSendToNewDocument

with runtime error 5852. Requested object is not available.

With your document, check out the following:

------------------

Private Function give_Mailmerge_Status_Text(mmStatus As Long) As
String
Select Case mmStatus
Case wdNormalDocument ' 0
gib_Mailmerge_Status_Text = "normal Dokument"
Case wdMainDocumentOnly ' 1
gib_Mailmerge_Status_Text = "Main dokument w/out Daten Source"
Case wdMainAndDataSource ' 2
gib_Mailmerge_Status_Text = "Main dokument with Data source"
Case wdMainAndHeader ' 3
gib_Mailmerge_Status_Text = "Main dokument with Header"
Case wdMainAndSourceAndHeader ' 4
gib_Mailmerge_Status_Text = "Main dokument with Datasource und
Header"
Case wdDataSource ' 5
gib_Mailmerge_Status_Text = "Data source"
Case Else
gib_Mailmerge_Status_Text = "unknown MailMerge-Status"
End Select
End Function

Public Sub zeigen_Mailmerge_Status()

Call MsgBox("MailMerge Status is: " & ActiveDocument.MailMerge.State
& " = " _
& give_Mailmerge_Status_Text(ActiveDocument.MailMerge.State),
vbOKOnly, ActiveDocument.Name)
End Sub

What do you see?


Yours,
 
D

Doug Robbins - Word MVP

As I suspected, "1 Project Engagement Description.doc" is not a mail merge
main document and as a result, it does not have a data source attached to it
and that is the reason why your attempt to execute the merge fails.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top