0

我想将合并字段插入到现有的 word 文档中。能够创建合并字段的 xml 元素,但不确定如何将其附加到文档中。下面是我的代码

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

从上面我会得到活动文档

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 });

这里正在创建一个段落。现在我怎样才能将此段落元素附加到 wrdDoc

4

1 回答 1

-1
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);
于 2013-11-14T05:28:36.873 回答