none
OpenXML mailmerge header problem when printing

    Question

  • Hello,

    I have a problem with the openXML when I replace all my mailmerges by the value.

    After I do a mailmerge, At a first look all is fine : The values are correct on the screen...
    When I want to print, in the main document, all is fine, but on the header, all the parameters fields are put back to a mergefield instead of the value.

    Here is how I do the replace of the value

    For example if I have a variable with PrjName.

    In the maindocument, PrjName is replaced by TestPrj.

    In the header, prjName is also replaced by TestPrj (that is great, exactly what I need.

    The problem is that as soon as I do a Print in Office 2010, the document is modified on the preview and on the maindocument, PrjName still has the correct value (TestPrj), but on the header (and footer too), the variavle PrjName is put back to PrjName instead of the value.

    Can you please help me?

    Thank you

    Marc

    Private Shared Sub ReplaceMergeFieldield(ByVal RootPart As OpenXmlPartRootElement, ByVal fieldname As String, ByVal fieldValue As String)
            For Each run As Run In RootPart.Descendants(Of Run)()
                For Each txtFromRun As Text In run.Descendants(Of Text)().Where(Function(a) a.Text = "«" & Convert.ToString(fieldname) & "»")

                    txtFromRun.Text = fieldValue

                Next
            Next
        End Sub

    Friday, March 01, 2013 8:58 AM

Answers

  • Hi Marc

    Just to finish off this discussion with what the underlying problem is with your original approach...

    If you write text to a mailmerge field, rather than the field populating from a data source (or not, if no data source is attached), this text will only be available until the point where the mail merge field is updated. When you switch views, or a document is updated, fields in the document header/footer will be updated automatically; this is not the case for all fields in the document body. Since the text you assigned to the merge fields was not coming from a linked data source, when the fields in the header/footer updated at printing, they reset. The fields in the document body did not update, so the text was retained.


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, March 15, 2013 1:48 PM

All replies