none
Insert merge field in word using open xml RRS feed

  • Question

  • Hi team,

    I want to insert a merge field to an existing word doc. Am able to create a xml element of merge field but am not sure on how to append that to the document. Below is my code 

    Microsoft.Office.Interop.Word.Document wrdDoc = Globals.ThisAddIn.Application.ActiveDocument;

    From above I will get the active document

    string instructionText = String.Format(" MERGEFIELD  {0}  \\* MERGEFORMAT", fieldname);
                    SimpleField simpleField1 = new SimpleField() { Instruction = instructionText };
                    DocumentFormat.OpenXml.Wordprocessing.Run run1 = new DocumentFormat.OpenXml.Wordprocessing.Run();
                    RunProperties runProperties1 = new RunProperties();
                    NoProof noProof1 = new NoProof();
                    runProperties1.Append(noProof1);
                    Text text1 = new Text();
                    text1.Text = String.Format("«{0}»", fieldname);
                    run1.Append(runProperties1);
                    run1.Append(text1);
                    simpleField1.Append(run1);
                    DocumentFormat.OpenXml.Wordprocessing.Paragraph paragraph = new DocumentFormat.OpenXml.Wordprocessing.Paragraph();
                    paragraph.Append(new OpenXmlElement[] { simpleField1 });

    Here am creating a paragraph. Now how can i append this paragraph element to the wrdDoc

    Harish



    Tuesday, November 12, 2013 11:37 AM

Answers

  • Hi Harish

    I'm not sure I understand: Do you want to use the Open XML sDK to add a merge field to a document that's open in the Word application, via a VSTO Add-in? Why, when you're already working with the interop via the add-in?

    The Open XML SDK cannot manipulate a document opened in an Add-in. It is possible to "drop" valid Word Open XML into an open document, but not using the Open XML SDK. You need to construct valid Word Open XML in the OPC flat file format in order to use the InsertXML method. But I can't think of any good reason to do this for a merge field...


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, November 13, 2013 1:52 PM
    Moderator

All replies

  • Addendum: By using interop we can add merge field to the active document. But I want to add through xml element

    Word.MailMerge wrdMailMerge;

    Word.Selection wrdSelection; Word.MailMergeFields wrdMergeFields; Microsoft.Office.Interop.Word.Document wrdDoc = Globals.ThisAddIn.Application.ActiveDocument; Microsoft.Office.Interop.Word.Application wrdApp = Globals.ThisAddIn.Application; wrdSelection = wrdApp.Selection; wrdMailMerge = wrdDoc.MailMerge; wrdMergeFields = wrdMailMerge.Fields; wrdMergeFields.Add(wrdSelection.Range, fieldname);


     

    Harish


    Tuesday, November 12, 2013 11:41 AM
  • I would recommend you to add merge fields to a Word document and then open the document as a ZIP archive and check the file document.xml to see how word wrote the merge fields you inserted. This way you can learn the structure of Open XML documents. There is even an Add-In for Visual Studio that allows you to load and analyze Open XML packages inside VS:
    http://visualstudiogallery.msdn.microsoft.com/450a00e3-5a7d-4776-be2c-8aa8cec2a75b

    Since you want to implement a document generation (mail merge) solution I would also like to encourage to take a look at existing (mostly commercial) solution. One example is Docentric Toolkit:
    http://www.docentric.com

    that will allow you to easily insert placeholders and populate them with your data with a very few lines of code.

    Wednesday, November 13, 2013 1:27 PM
  • Hi Harish

    I'm not sure I understand: Do you want to use the Open XML sDK to add a merge field to a document that's open in the Word application, via a VSTO Add-in? Why, when you're already working with the interop via the add-in?

    The Open XML SDK cannot manipulate a document opened in an Add-in. It is possible to "drop" valid Word Open XML into an open document, but not using the Open XML SDK. You need to construct valid Word Open XML in the OPC flat file format in order to use the InsertXML method. But I can't think of any good reason to do this for a merge field...


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, November 13, 2013 1:52 PM
    Moderator