【发布时间】:2021-03-08 15:53:27
【问题描述】:
我尝试使用 C# Word Interop 填充 MS Word 模板。在 .dotx 文件中,我想填充一个命名标记(或命名字段或您所称的)。我可以在文档末尾附加文本和表格,但不能在命名字段内。有人能指出我正确的方向吗?
这在文档末尾起作用:
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document doc = app.Documents.Open(WORDTEMPLATE);
Microsoft.Office.Interop.Word.Paragraph para1 = doc.Content.Paragraphs.Add();
para1.Range.Text = "Hello World";
para1.Range.InsertParagraphAfter();
Microsoft.Office.Interop.Word.Table firstTable = doc.Tables.Add(para1.Range, 5, 5);
firstTable.Borders.Enable = 1;
foreach (Microsoft.Office.Interop.Word.Row row in firstTable.Rows)
{
foreach (Microsoft.Office.Interop.Word.Cell cell in row.Cells)
{
//Header row
if (cell.RowIndex == 1)
{
cell.Range.Text = "Column " + cell.ColumnIndex.ToString();
cell.Range.Font.Bold = 1;
//other format properties goes here
cell.Range.Font.Name = "verdana";
cell.Range.Font.Size = 10;
//cell.Range.Font.ColorIndex = WdColorIndex.wdGray25;
cell.Shading.BackgroundPatternColor = Microsoft.Office.Interop.Word.WdColor.wdColorGray25;
//Center alignment for the Header cells
cell.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
cell.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
}
//Data row
else
{
cell.Range.Text = (cell.RowIndex - 2 + cell.ColumnIndex).ToString();
}
}
}
doc.SaveAs2(RESULTFILE, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocumentDefault);
doc.Close(false);
app.Quit(false);
而我想要的是这样的:
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document doc = app.Documents.Open(WORDTEMPLATE);
Microsoft.Office.Interop.Word.Range range = doc.SelectContentControlsByTag("MyTagName")[1].Range; // !! this line is working
Microsoft.Office.Interop.Word.Paragraph para1 = doc.Content.Paragraphs.Add(range); // !! this line is not working
para1.Range.Text = "Hello World";
para1.Range.InsertParagraphAfter();
Microsoft.Office.Interop.Word.Table firstTable = doc.Tables.Add(para1.Range, 5, 5);
firstTable.Borders.Enable = 1;
foreach (Microsoft.Office.Interop.Word.Row row in firstTable.Rows)
{
foreach (Microsoft.Office.Interop.Word.Cell cell in row.Cells)
{
//Header row
if (cell.RowIndex == 1)
{
cell.Range.Text = "Column " + cell.ColumnIndex.ToString();
cell.Range.Font.Bold = 1;
//other format properties goes here
cell.Range.Font.Name = "verdana";
cell.Range.Font.Size = 10;
//cell.Range.Font.ColorIndex = WdColorIndex.wdGray25;
cell.Shading.BackgroundPatternColor = Microsoft.Office.Interop.Word.WdColor.wdColorGray25;
//Center alignment for the Header cells
cell.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
cell.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
}
//Data row
else
{
cell.Range.Text = (cell.RowIndex - 2 + cell.ColumnIndex).ToString();
}
}
}
doc.SaveAs2(RESULTFILE, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocumentDefault);
doc.Close(false);
app.Quit(false);
谢谢!
【问题讨论】:
-
我不懂 C#。但是,我在您的代码中看不到任何添加任何字段或内容控件的内容。我认为您想要的是富文本内容控件。查看 John Korchok 关于使用 vba 插入内容控件的页面:brandwares.com/bestpractices/2019/08/…
-
@CharlesKenyon 该字段已在我的 Word 模板中。所以我想将我的内容填写到该字段中。我可以通过
doc.SelectContentControlsByTag("MyTagName")[1].Range选择字段范围然后我可以将文本填充到我的字段中。但我也想填一张桌子,那是行不通的。因为要创建一个表格,我需要提供一个范围,而我不知道如何获得正确的范围。 -
在 Word 中,您使用的是内容控件(这很好),而不是字段。您应该能够在该范围内插入表格。我假设内容控件是富文本内容控件,而不是纯文本 CC。