R. Bandi
R. Bandi

Reputation: 113

Creating word add in with OpenXML to insert new MergeField

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 actually add MergeField using ConsoleApp but I want to insert the MergeField from the Word add in to the current opened document.

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.

Upvotes: 1

Views: 898

Answers (1)

Chris
Chris

Reputation: 3529

Add a try/catch and you'll probably find that it can't open the file because it's currently open for editing.

The OpenXML SDK is a library for writing to Office files without going through Office's interfaces. But you're trying to do so while also using Office's interfaces, so you're essentially trying to take two approaches at once. This isn't going to work unless you first close the document.

But what you probably want to do is use VSTO. In VSTO, each document has a Fields collection, that you can use to add fields.

Fields.Add(Range, Type, Text, PreserveFormatting)

Upvotes: 0

Related Questions