我使用vb.net命名空间“Imports Microsoft.Office.Interop.Word”。我有word文档,我想在这些中创建一个页眉和/或页脚。例如,像“Date:[CurrentDate] Page:[CurrentPage]”这样的页脚。目前我只写了一个成功的领域。如果我写/添加下一个字段,将删除第一个字段。像“替换”而不是“添加”。代码有什么问题?
For Each section As Section In _wordDoc.Application.ActiveDocument.Sections
Dim footerRange As Range = section.Footers(WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
footerRange.Fields.Add(footerRange, WdFieldType.wdFieldFileName)
footerRange.Fields.Add(footerRange, WdFieldType.wdFieldDate)
' footerRange.Fields.Add(footerRange, "Page")
footerRange.Fields.Add(footerRange, WdFieldType.wdFieldPage)
'footerRange.Fields.Add(footerRange, "-")
footerRange.Fields.Add(footerRange, WdFieldType.wdFieldNumPages)
footerRange.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight
Next
第二个问题是,如何向页眉/页脚添加简单的文本?
答案 0 :(得分:2)
我找到了解决方案:
footerRange = section.Footers(WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
footerRange.InsertAfter("Page ")
footerRange.SetRange(footerRange.End + 1, footerRange.End + 1)
footerRange.Fields.Add(footerRange, WdFieldType.wdFieldPage)
footerRange = section.Footers(WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
footerRange.InsertAfter(" of ")
footerRange.SetRange(footerRange.End + 1, footerRange.End + 1)
footerRange.Fields.Add(footerRange, WdFieldType.wdFieldNumPages)