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
Doug Robbins - Word MVP - 31 Oct 2006 20:01 GMT
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"

Signature
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
>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
[quoted text clipped - 43 lines]
>
> RalphK
Lüko Willms - 31 Oct 2006 20:40 GMT
Am Tue, 31 Oct 2006 17:14:02 UTC, schrieb RalphK
<RalphK@discussions.microsoft.com> auf
microsoft.public.word.vba.general :
> 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,
RalphK - 31 Oct 2006 21:13 GMT
The message is:
MailMerge Status is: 0 = normal Dokument.
RalphK
> Am Tue, 31 Oct 2006 17:14:02 UTC, schrieb RalphK
> <RalphK@discussions.microsoft.com> auf
[quoted text clipped - 45 lines]
>
> Yours,
Doug Robbins - Word MVP - 31 Oct 2006 22:32 GMT
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.

Signature
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
> The message is:
>
[quoted text clipped - 51 lines]
>>
>> Yours,