none
Creating Word add in using OpenXml to add MergeField RRS feed

  • Question

  • I'm new to VSTO and OpenXML and I would like to develop a Word add-in. This add-in should use OpenXML, The add in should add a MergeField to the document, I can insert text but I can't figure out how can I insert a MergeField.

    So I have this code in ButtonClick

    // take current file location
                var fileFullName = Globals.ThisAddIn.Application.ActiveDocument.FullName;
    
                Globals.ThisAddIn.Application.ActiveDocument.Close(WdSaveOptions.wdSaveChanges, WdOriginalFormat.wdOriginalDocumentFormat, true);
    
                // function to insert new field here
                OpenAndAddTextToWordDocument(fileFullName, "username");
    
    
    
    
                Globals.ThisAddIn.Application.Documents.Open(fileFullName);

    And I Created the function which should add the new MergeField:

    public static DocumentFormat.OpenXml.Wordprocessing.Paragraph OpenAndAddTextToWordDocument(string filepath, string txt)
            {
                // Open a WordprocessingDocument for editing using the filepath.
                WordprocessingDocument wordprocessingDocument =
                WordprocessingDocument.Open(filepath, true);
    
                // Assign a reference to the existing document body.
                Body body = wordprocessingDocument.MainDocumentPart.Document.Body;
    
    
                // add text
    
                string instructionText = String.Format(" MERGEFIELD  {0}  \\* MERGEFORMAT", txt);
                SimpleField simpleField1 = new SimpleField() { Instruction = instructionText };
    
                Run run1 = new Run();
    
                RunProperties runProperties1 = new RunProperties();
                NoProof noProof1 = new NoProof();
    
                runProperties1.Append(noProof1);
                Text text1 = new Text();
                text1.Text = String.Format("«{0}»", txt);
    
                run1.Append(runProperties1);
                run1.Append(text1);
    
                simpleField1.Append(run1);
    
                DocumentFormat.OpenXml.Wordprocessing.Paragraph paragraph = new DocumentFormat.OpenXml.Wordprocessing.Paragraph();
                paragraph.Append(new OpenXmlElement[] { simpleField1 });
                return paragraph;
    
    
    
    
            // Close the handle explicitly.
            wordprocessingDocument.Close();

    But something is not working here, when I use the add in it doesn't do anything Thanks for the help.

    Monday, December 11, 2017 7:58 AM

Answers

  • Hello Riyad Bandi,

    I think OpenAndAddTextToWordDocument method does not need return a Paragraph object. You need finish editing the file with Open XML in the method directly. 

    In the method you create a new paragraph, however, you did not append it to the document body. 

    Please refer to below code and test again.

     public static void OpenAndAddTextToWordDocument(string filepath, string txt)
            {
                // Open a WordprocessingDocument for editing using the filepath.
                WordprocessingDocument wordprocessingDocument =
                WordprocessingDocument.Open(filepath, true);
    
                // Assign a reference to the existing document body.
                Body body = wordprocessingDocument.MainDocumentPart.Document.Body;
    
    
                // add text
    
                string instructionText = String.Format(" MERGEFIELD  {0}  \\* MERGEFORMAT", txt);
                SimpleField simpleField1 = new SimpleField() { Instruction = instructionText };
    
                Run run1 = new Run();
    
                RunProperties runProperties1 = new RunProperties();
                NoProof noProof1 = new NoProof();
    
                runProperties1.Append(noProof1);
                Text text1 = new Text();
                text1.Text = String.Format("«{0}»", txt);
    
                run1.Append(runProperties1);
                run1.Append(text1);
    
                simpleField1.Append(run1);
    
                DocumentFormat.OpenXml.Wordprocessing.Paragraph paragraph = new DocumentFormat.OpenXml.Wordprocessing.Paragraph();
                paragraph.Append(new OpenXmlElement[] { simpleField1 });
    
                body.Append(paragraph);
                //return paragraph;
    
                // Close the handle explicitly.
                wordprocessingDocument.Close();
            }

    By the way, what's the current state of your previous thread VSTO and Open XML. If it has been resolved, I would suggest you mark helpful replay or share us your solution and mark it to close that thread. If no, please feel free to let us know your current issue.

    Best Regards,

    Terry


    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.


    • Edited by Terry Xu - MSFT Tuesday, December 12, 2017 7:43 AM
    • Marked as answer by Riyad Bandi Thursday, December 14, 2017 7:05 AM
    Tuesday, December 12, 2017 7:39 AM

All replies

  • Hello Riyad Bandi,

    I think OpenAndAddTextToWordDocument method does not need return a Paragraph object. You need finish editing the file with Open XML in the method directly. 

    In the method you create a new paragraph, however, you did not append it to the document body. 

    Please refer to below code and test again.

     public static void OpenAndAddTextToWordDocument(string filepath, string txt)
            {
                // Open a WordprocessingDocument for editing using the filepath.
                WordprocessingDocument wordprocessingDocument =
                WordprocessingDocument.Open(filepath, true);
    
                // Assign a reference to the existing document body.
                Body body = wordprocessingDocument.MainDocumentPart.Document.Body;
    
    
                // add text
    
                string instructionText = String.Format(" MERGEFIELD  {0}  \\* MERGEFORMAT", txt);
                SimpleField simpleField1 = new SimpleField() { Instruction = instructionText };
    
                Run run1 = new Run();
    
                RunProperties runProperties1 = new RunProperties();
                NoProof noProof1 = new NoProof();
    
                runProperties1.Append(noProof1);
                Text text1 = new Text();
                text1.Text = String.Format("«{0}»", txt);
    
                run1.Append(runProperties1);
                run1.Append(text1);
    
                simpleField1.Append(run1);
    
                DocumentFormat.OpenXml.Wordprocessing.Paragraph paragraph = new DocumentFormat.OpenXml.Wordprocessing.Paragraph();
                paragraph.Append(new OpenXmlElement[] { simpleField1 });
    
                body.Append(paragraph);
                //return paragraph;
    
                // Close the handle explicitly.
                wordprocessingDocument.Close();
            }

    By the way, what's the current state of your previous thread VSTO and Open XML. If it has been resolved, I would suggest you mark helpful replay or share us your solution and mark it to close that thread. If no, please feel free to let us know your current issue.

    Best Regards,

    Terry


    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.


    • Edited by Terry Xu - MSFT Tuesday, December 12, 2017 7:43 AM
    • Marked as answer by Riyad Bandi Thursday, December 14, 2017 7:05 AM
    Tuesday, December 12, 2017 7:39 AM
  • Hello Terry Xu 

    Thank you so much your answer solved my problem!

    Just a question, can I insert the new field after my cursor? Currently it is inserted as a new paragraph. 

    Regarding the previous thread I have marked it as answered, Thanks for reminding me :)

    Thursday, December 14, 2017 7:09 AM