如何在命名标签字段内插入段落和表?



我尝试使用c# Word互操作填充MS Word- template。在.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);

谢谢!

你不应该使用"range"因为这是应用程序中的保留字。改成

Microsoft.Office.Interop.Word.Range rng = doc.SelectContentControlsByTag("MyTagName")[1].Range;

,然后跟着

rng.text = "Hello World";

最新更新