Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2006

Tip: Looking for answers? Try searching our database.

Automating Mail Merge in Word

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
RalphK - 31 Oct 2006 18:14 GMT
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,

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.