Results 1 to 4 of 4
  1. #1
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Sydney, Australia, New South Wales, Australia
    Posts
    251
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Reference to MailMerge document (Word 2000 SR 1)

    Similar to my other post, I want to get a reference to a mail merged document. Using ActiveDocument is causing me some dramas. Is there any way to get a reference to the new mail-merged document apart from ActiveDocument?
    Thanks for your help.
    Peter

    <pre>Function CreateMailMergeDocument() As Word.Document
    Dim doc As Word.Document

    'Open the mail merge letter.
    Set doc = wd.Documents.Open(FileName:="filename", ReadOnly:=True)

    'Do the mail merge
    With doc.MailMerge
    .OpenDataSource Name:=sFrontEndDBToUse, _
    SQLStatement:="SELECT * from [qFull] " & _
    "WHERE [ID]=" & sID

    .Destination = wdSendToNewDocument
    .SuppressBlankLines = True
    .Execute Pause:=True 'Pauses if an error is found
    End With

    'Cleanup
    'Close the main document (i.e. the source document).
    'Merge document should still be open as the now ActiveDocument. Hopefully.
    doc.Close SaveChanges:=False
    Set doc = Nothing
    Set doc = wd.ActiveDocument

    'Process the document
    doc.Repaginate 'So we can get the true number of pages

    '...other stuff

    Set ReferenceToMailMergeDocument = doc
    End Function
    </pre>


  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Reference to MailMerge document (Word 2000 SR

    > Is there any way to get a reference to the new mail-merged document apart from ActiveDocument?

    It does not appear that Document.MailMerge.Execute returns an object reference to the new document.

    You could examine the Documents collection and set a reference to the most likely suspect. For example, the one that is .Saved = False and has the highest integer following the word Document in its .Name. (That's a concept I haven't actually tested, and doing almost no merges, I'm not sure of the naming convention for new documents generated thereby.)

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Reference to MailMerge document (Word 2000 SR 1)

    If you use a second variable of type document and set it to ActiveDocument immediately after the .Execute line, you should always have the correct one. Relying on ActiveDocument after closing the main merge document is less dependable.

  4. #4
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Sydney, Australia, New South Wales, Australia
    Posts
    251
    Thanks
    0
    Thanked 4 Times in 4 Posts

    THANKS: Reference to MailMerge document

    Thanks for the ideas.
    It seems a little odd that Execute doesn't return the new document. Maybe in the next version ...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •