Reputation: 160
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", cmbType.Text + "__" + cmbField.Text);
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}»", cmbType.Text + "__" + cmbField.Text);
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
Upvotes: 0
Views: 1026
Reputation: 160
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;
wrdSelection.ParagraphFormat.Alignment =
Word.WdParagraphAlignment.wdAlignParagraphJustify;
wrdMergeFields.Add(wrdSelection.Range, fieldname);
Upvotes: -1