none
Mail merging into Microsoft Word text boxes with C# RRS feed

  • Question

  • I have a word document, that uses several textboxes, which contain mail merge fields. 

    I want to mail merge programmatically into a word document, by using a system that I have made, that searches for any mail merge fields in a document and then inserts the appropriate value into the mail merge fields and then saves the document as a new file.

    By using 

    Document.StoryRanges

    I am able to do the above process for 1 text box.

    But if I make several text boxes, it only seems to insert the value into one of the textboxes consistently. The texbox that receives the value does not change. If I try to delete that textbox, the mail merging process does not work and then I have to fiddle with some of the other textboxes to get it to work. For example I have to bring the textbox backwards and then forwards for the system to mail merge into only of the textboxes. 

    I have tried creating a foreach loop to go into each textbox, without much success. So I did some debugging and found that the system is only reading the entire document and one of the textboxes as "StoryRanges". 

    Help would be appreciated.

    Saturday, August 27, 2016 6:00 AM

Answers

All replies

  • >>>I have tried creating a foreach loop to go into each textbox, without much success. So I did some debugging and found that the system is only reading the entire document and one of the textboxes as "StoryRanges". 

    According to your description, if you want a macro to update the fields in any of your text boxes, the following macro steps through each of the shapes in your document (this includes text boxes) and, if the shape contains text, update any fields it contains.
    Sub UpdateTBFields()
    
        Dim shp As Shape
    
        For Each shp In ActiveDocument.Shapes
          If shp.Type = MsoShapeType.msoTextBox Then
            With shp.TextFrame
                If .HasText Then
                    .TextRange.Fields.Update
                End If
            End With
          End If
        Next
    
    End Sub

    Otherwise could you provide more information about your issue, for example sample code, screenshot etc., that will help us reproduce and resolve it.

    Thanks for your understanding.
    Monday, August 29, 2016 5:57 AM
  • I have a word document, that uses several textboxes, which contain mail merge fields. 

    I want to mail merge programmatically into a word document, by using a system that I have made, that searches for any mail merge fields in a document and then inserts the appropriate value into the mail merge fields and then saves the document as a new file.

    Help would be appreciated.

    That is not the correct way to do a mailmerge. For an explanation, see: https://social.msdn.microsoft.com/Forums/office/en-US/15b367d8-b3d5-484a-bfe0-3e6adbf404b6/mail-merging-to-multiple-text-boxes-word?forum=worddev

    For a VBA macro that automates a mailmerge to generate a separate document for each record, see Send Mailmerge Output to Individual Files in the Mailmerge Tips & Tricks thread at: http://www.msofficeforums.com/mail-merge/21803-mailmerge-tips-tricks.html . Amongst other things, the code there demonstrates the methods you need to use. There is no need to address individual mailmerge fields.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Proposed as answer by David_JunFeng Friday, September 2, 2016 8:27 AM
    • Marked as answer by David_JunFeng Monday, September 5, 2016 2:22 PM
    Monday, August 29, 2016 11:14 AM