【发布时间】:2018-12-18 16:11:32
【问题描述】:
我正在尝试研究如何更新\更改模板上的字段。到目前为止,我发现的每条好消息都导致当前页眉或页脚被完全覆盖。这很明显。问题是我找不到如何专门修改页脚中的某些字段而不是整个页脚。
// Set headers
foreach (Word.Section section in doc.Sections)
{
Word.Range headerRange = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
headerRange.Fields.Add(headerRange, Word.WdFieldType.wdFieldPage);
headerRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
}
// Set footers
foreach (Word.Section wordSection in doc.Sections)
{
Word.Range footerRange = wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
footerRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
footerRange.Font.ColorIndex = Word.WdColorIndex.wdDarkRed;
footerRange.Font.Size = 20;
footerRange.Text = "Confidential";
}
在这段代码中,我试图让“机密”一词覆盖中间字段。目前中间字段为空白。左右字段分别是页码和日期。一旦我运行代码,它就会被“机密”代码替换。它还完全删除了作为页脚背景颜色强调的形状。
如何修改现有字段?这样会删除形状吗?
更新: 以下代码是在我觉得自己在兜圈子之前得到的。这一切看起来都非常有序,就好像您在物理上键入\编辑文档一样。伊克。不管怎样,这很接近。但是由于某种原因,格式被忽略了,我不能让它只应用于中心文本。另外,如果我取消注释日期字段行,整个页脚都会被删除。
// Set footers
foreach (Word.Section wordSection in doc.Sections)
{
Word.Range footerRange = wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
footerRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
footerRange.Paragraphs.TabStops.Add(wordApp.InchesToPoints(3.25F), Word.WdTabAlignment.wdAlignTabCenter);
footerRange.Paragraphs.TabStops.Add(wordApp.InchesToPoints(6.5F), Word.WdTabAlignment.wdAlignTabRight);
footerRange.Fields.Add(footerRange, Word.WdFieldType.wdFieldPage, "\t", true);
footerRange.Font.ColorIndex = Word.WdColorIndex.wdDarkRed;
footerRange.Font.Size = 20;
footerRange.Text = "\tCONFIDENTIAL\t";
footerRange.InsertBefore("01-DEC-18");
//footerRange.Fields.Add(footerRange, Word.WdFieldType.wdFieldDate);
}
【问题讨论】:
-
请详细说明如何识别“中间字段”?如果这是一个“标准”页脚,它使用制表符和制表位来对齐内容。如果是这种情况,打开非打印字符的显示可以帮助我们在屏幕截图中看到这一点。如果没有识别信息,就只能猜测什么可行……
-
我还提醒您,根据网站政策,一个问题应该只包含一个查询,而不是更多。请为第二个查询发布一个单独的问题。并包括所有详细信息,例如日期/时间字段在文档中的位置...
-
@CindyMeister 我更新了图片。
-
感谢您在半夜提供的最新信息。我的方法贴在下面。
标签: c# .net ms-word office-interop