Word MAil Merge Automation (MailMerge.execute)

In the following code If I use WordDoc.MailMerge.Execute() it opens up another documet named
FormLetters,

Question is what's use of WordDoc.MailMerge.execute

Private Sub DoMailMerge()

Dim WordApp As New Word.Application

Dim WordDoc As New Word.Document


Try

WordApp.Visible = True

WordDoc = WordApp.Documents.Open("c:\mydoc.dot") ' Template no merge inside this document only fields

WordDoc.MailMerge.OpenDataSource(Name:= "c:\sqlserver.odc")

'Perform Merge
'WordDoc.MailMerge.Execute() 'Do I Need this statement. Mail merge still works without this

WordDoc.SaveAs(strFileName)


Catch


Finally
WordDoc.Close(False)
WordApp.Quit(False)

WordDoc = Nothing
WordApp = Nothing

End Sub



Answer this question

Word MAil Merge Automation (MailMerge.execute)

  • jzfredricks

    Correct, bookmarks are deleted when the merge executes. The reason is that a bookmark must be unique in a document. Since mail merge (when used as designed) will result in multiple copies of the original document, the bookmarks would be duplicated. Since this isn't allowed, bookmarks are removed.

    Why not insert the signature BEFORE executing the merge. Then remove it from the main merge document (or simply close the main merge document without saving changes).



  • Kaiser28

    Our posts crossed...

    You should avoid using the Selection object, whenever possible. "Bad form" (and it's slower, less reliable ("subject to change") and makes the screen flicker). Better to work with the RANGE object (think of a Range as being a selection, but not visible on the screen). Then:

    Dim shp as Word.Shape
    shp = WordDoc.Shapes.AddPicture(sign_file, LinkToFile:=False, SaveWithDocument:=True, Anchor:=WordDoc.Bookmarks("Sign").Range)

    Now you work with the variable shp to manipulate the picture you inserted.



  • faith2006

    I realize after posting here (I just feel stupid)

    Before doing merge I need to do this

    If (check some condition) Then

    Dim sign_file As String
    sign_file = "c:\signature.tif"

    WordApp.Selection.GoTo(What:=Word.WdGoToItem.wdGoToBookmark, Name:="SIGN")
    WordDoc.Shapes.AddPicture(sign_file, LinkToFile:=False, SaveWithDocument:=True, Anchor:=WordApp.Selection.Range)

    End If

    and Then

    Call Merge

    WordDoc.MailMerge.Destination = Word.WdMailMergeDestination.wdSendToNewDocument
    WordDoc.MailMerge.Execute()

    Hopefully this will be my last question, I need to do one more thing

    How can I programmatically find the Picture placed at the bookmark "Sign"


  • sridhar kumar.j

    You made it clear to me. You were right merge was dynamic. I changed the data in the SQL table and it changed it in the document. I must use Execute then.

    I am a seasoned developer but very new to word and its object model, and have another question

    After merging I need to programmatically add signature to this document (not a merge data), there is a bookmark name SIGN in which I need to put a picure

    This is what I do after the merge

    WordDoc.MailMerge.Destination = Word.WdMailMergeDestination.wdSendToNewDocument
    WordDoc.MailMerge.Execute()

    but bookmark does not exists in the new document

    I want to do this for the bookmark
    If (check some condition) Then

    Dim sign_file As String
    sign_file = "c:\signature.tif"

    WordApp.Selection.GoTo(What:=Word.WdGoToItem.wdGoToBookmark, Name:="SIGN")
    WordDoc.Shapes.AddPicture(sign_file, LinkToFile:=False, SaveWithDocument:=True, Anchor:=WordApp.Selection.Range)

    End If


  • mario.muja

    The concept of mail merge is that you can "personalize" the same letter for many recipients. (Or you can create envelopes or labels for many records in your database)

    Execute generates a result for each record in the database. Execute will either send each to the printer, or generate a new document containing all the results. If executing to a new document, the merge fields are resolved to plain text, and are thus static.

    Mail merge was not designed for using the preview result, which is what you appear to be doing. Whether you'll be walking into any unexpected problems by doing this depends on how you're using the result if you do not .Execute the merge... One common problem people run into who "save themselves" this additional step is that the next time they open the document the data has changed - because the merge fields are dynamic.



  • Word MAil Merge Automation (MailMerge.execute)