none
Open XML Text is not replacing after editing word document.

    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)
                            docGenerated.ChangeDocumentType(WordprocessingDocumentType.Document)

                            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
                                    Next
                                Next
                            Next
                            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
                                settingsPart.Settings.RemoveChild(oxeSettings)
                                settingsPart.Settings.Save()
                            End If

                            docGenerated.MainDocumentPart.Document.Save()
                            docGenerated.Close()
                        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.

    jeudi 28 juin 2018 10:33

Toutes les réponses

  • 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 MSDNFSF@microsoft.com.

    vendredi 29 juin 2018 10:08
    Modérateur