Open XML Text is not replacing after editing word document. RRS feed

  • Question

  • Hi, I am working with OpenXML and stuck in a issue.

    First I am creating the word document from template file.

    Then using following code for mail merge

     Using docGenerated As WordprocessingDocument = WordprocessingDocument.Open(_targetFileName, True)

                            For Each field As FieldCode In docGenerated.MainDocumentPart.RootElement.Descendants(Of FieldCode)()
                                Dim fieldNameStart = field.Text.LastIndexOf(FieldDelimeter, System.StringComparison.Ordinal)
                                Dim fieldname = "FirstName"
                                Dim fieldValue ="Test"
                                For Each run As Run In docGenerated.MainDocumentPart.Document.Descendants(Of Run)()

                                    For Each txtFromRun As Text In run.Descendants(Of Text)().Where(Function(a) a.Text.ToLower() = "«" & fieldname.ToLower() & "»")
                                        txtFromRun.Text = fieldValue
                            Dim settingsPart As DocumentSettingsPart = docGenerated.MainDocumentPart.GetPartsOfType(Of DocumentSettingsPart)().First()
                            Dim oxeSettings = settingsPart.Settings.Where(Function(a) a.LocalName = "mailMerge").FirstOrDefault()

                            If oxeSettings IsNot Nothing Then
                            End If

                        End Using

    After that I am using AltChunk to add text from template file to existing word document and then trying about code for mail merge.

    But after adding text merging is not working for second page.

    Please suggest what I am doing wrong.

    Thursday, June 28, 2018 10:33 AM

All replies

  • Hi Pankaj,

    Did you get any error if you try mail merge manually? I also suggest that you validate the document using Open XML SDK 2.5 Productivity Tool after you adding the text.

    Regards & Fei

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Friday, June 29, 2018 10:08 AM