如何添加“第 x 页,共 y 页”页脚到 Word2007 文档,因为我使用 C# 生成它
我尝试了这个:
private void AddFooters()
{
foreach (Word.Section wordSection in this.WordDoc.Sections)
{
object fieldEmpty = Word.WdFieldType.wdFieldEmpty;
object autoText = "AUTOTEXT \"Page X of Y\" ";
object preserveFormatting = true;
wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Fields.Add(
wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range,
ref fieldEmpty, ref autoText, ref preserveFormatting);
}
}
还有这个:
private void AddFooters()
{
foreach (Word.Section section in this.WordDoc.Sections)
{
Word.Range footerRange = section.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
this.WordDoc.ActiveWindow.Selection.TypeText("Page ");
footerRange.Fields.Add(footerRange, Word.WdFieldType.wdFieldPage);
this.WordDoc.ActiveWindow.Selection.TypeText(" of ");
footerRange = section.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
footerRange.Fields.Add(footerRange, Word.WdFieldType.wdFieldNumPages);
footerRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
}
}
我记录了这个VBA宏,但它似乎没有帮助。
Sub Macro1()
'
' Macro1 Macro
'
'
WordBasic.ViewFooterOnly
ActiveDocument.AttachedTemplate.BuildingBlockEntries("Bold Numbers 3"). _
Insert Where:=Selection.Range, RichText:=True
End Sub
我尝试过的所有方法都没有完全对我有用(我已经有点接近了)。 如果问题的某些内容不清楚,请告诉我。
I looked here and here and here
I tried this:
private void AddFooters()
{
foreach (Word.Section wordSection in this.WordDoc.Sections)
{
object fieldEmpty = Word.WdFieldType.wdFieldEmpty;
object autoText = "AUTOTEXT \"Page X of Y\" ";
object preserveFormatting = true;
wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Fields.Add(
wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range,
ref fieldEmpty, ref autoText, ref preserveFormatting);
}
}
And this:
private void AddFooters()
{
foreach (Word.Section section in this.WordDoc.Sections)
{
Word.Range footerRange = section.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
this.WordDoc.ActiveWindow.Selection.TypeText("Page ");
footerRange.Fields.Add(footerRange, Word.WdFieldType.wdFieldPage);
this.WordDoc.ActiveWindow.Selection.TypeText(" of ");
footerRange = section.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
footerRange.Fields.Add(footerRange, Word.WdFieldType.wdFieldNumPages);
footerRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
}
}
I recorded this VBA macro, but it does not seem to be helpful.
Sub Macro1()
'
' Macro1 Macro
'
'
WordBasic.ViewFooterOnly
ActiveDocument.AttachedTemplate.BuildingBlockEntries("Bold Numbers 3"). _
Insert Where:=Selection.Range, RichText:=True
End Sub
Nothing that I tried quite worked for me entirely (I got somewhat close).
Let me know if something about the question is not clear.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,成功了。
最后一行返回到主文档。
它不是最好和最“花哨”的 C#,但它对我有用。 C# .Net 3.5,Office 2007。
Ya, got it working.
That last line returns to the main document.
It't not the best and most "fancy" c#, but it works for me. C# .Net 3.5, Office 2007.
这是用 VB 编写的,但我尝试过,它对我有用,尽管在这里您必须提供当前页码和总计页码。我确信有更好的解决方案:/
This is in VB but I tried this and it worked for me, although here you'd have to supply the current and total for page numbers. I'm sure there is a better solution :/
此链接有助于解决此问题
https://social.msdn.microsoft.com/Forums/vstudio/en-US/a044ff2d-b4a7-4f19-84f4-f3d5c55396a8/insert-current-page-number-quotpage-x-of-nquot-on -a-word-document?forum=vsto
这是我在 VB.NET 中解决它的方法:
This link helped in solving this problem
https://social.msdn.microsoft.com/Forums/vstudio/en-US/a044ff2d-b4a7-4f19-84f4-f3d5c55396a8/insert-current-page-number-quotpage-x-of-nquot-on-a-word-document?forum=vsto
This is how I solved it in VB.NET:
我刚刚解决了我的标题问题。标题由两行组成。第一行是纯文本“SLUŽBENI LIST BiH”,第二行应包含发行号(文本 Broj)、页码(Strana)、- 字幕 - 发行日期 Daywk var、day var、month var、year var 。副标题必须是斜体。
这是我的解决方案。
添加页码字段后,以下文本会将页码一直推到右侧到底。我认为这是因为添加场范围定位器后不会留在场后面而是在场前面。以下行解决了我的问题。
我希望这能让别人的生活更轻松,如果有人有更优雅的解决方案,我想学习。
干杯!
I just solved my header problem. The header is composed of two lines. The first line is the plain text "SLUŽBENI LIST BiH", and the second line should contain the issue number (text Broj), page number (Strana), - subtitle - date of issue Daywk var, day var, month var, year var. The subtitle must be in italics.
Here is my solution.
After adding the page number field, the following text would push the page number all the way to the right to the end. I think it's because after adding the field range positioner doesn't stay behind but in front of the field. The following line solved my problem.
I hope this can make someone's life easier, and if anyone has a more elegant solution, I'd like to learn.
Cheers!