如何在Word2007 doc中添加“Page x of y”页脚,因为我使用C#生成它
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在Word2007 doc中添加“Page x of y”页脚,因为我使用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
我尝试的任何东西都完全不适合我(我有点接近)。如果问题不明确,请告诉我。
答案
呀,搞定了。
// objects I use in the code below
// instanciate wordapp as the Word application object
Microsoft.Office.Interop.Word.Application wordapp = new Microsoft.Office.Interop.Word.Application();
// create missing object for compatibility with C# .NET 3.5
Object oMissing = System.Reflection.Missing.Value;
// define worddoc as the word document object
Microsoft.Office.Interop.Word.Document worddoc = wordapp.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
// define s as the selection object
Microsoft.Office.Interop.Word.Selection s = wordapp.Selection;
// code for the page numbers
// move selection to page footer (Use wdSeekCurrentPageHeader for header)
worddoc.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageFooter;
// Align right
s.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
// start typing
worddoc.ActiveWindow.Selection.TypeText("Page ");
// create the field for current page number
object CurrentPage = Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage;
// insert that field into the selection
worddoc.ActiveWindow.Selection.Fields.Add(s.Range, ref CurrentPage, ref oMissing, ref oMissing);
// write the "of"
worddoc.ActiveWindow.Selection.TypeText(" of ");
// create the field for total page number.
object TotalPages = Microsoft.Office.Interop.Word.WdFieldType.wdFieldNumPages;
// insert total pages field in the selection.
worddoc.ActiveWindow.Selection.Fields.Add(s.Range, ref TotalPages, ref oMissing, ref oMissing);
// return to the document main body.
worddoc.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;
最后一行返回主文档。
它不是最好和最“花哨”的c#,但它对我有用。 C#.Net 3.5,Office 2007。
另一答案
这是在VB,但我试过这个,它对我有用,虽然在这里你必须提供页码的当前和总数。我确信有更好的解决方案:/
WordBasic.viewfooteronly
Selection.EndKey Unit:=wdStory
Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
Selection.TypeText Text:="Page " & current & " of " & total
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
另一答案
这个链接有助于解决这个问题
这就是我在VB.NET中解决它的方法:
Dim aDoc As Word.Document
aDoc.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageFooter
aDoc.ActiveWindow.ActivePane.Selection.Paragraphs.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight
aDoc.ActiveWindow.Selection.TypeText("Page ")
Dim CurrentPage = Word.WdFieldType.wdFieldPage
aDoc.ActiveWindow.Selection.Fields.Add(aDoc.ActiveWindow.Selection.Range, CurrentPage, , )
aDoc.ActiveWindow.Selection.TypeText(" of ")
Dim TotalPageCount = Word.WdFieldType.wdFieldNumPages
aDoc.ActiveWindow.Selection.Fields.Add(aDoc.ActiveWindow.Selection.Range, TotalPageCount, , )
aDoc.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument
以上是关于如何在Word2007 doc中添加“Page x of y”页脚,因为我使用C#生成它的主要内容,如果未能解决你的问题,请参考以下文章
以编程方式从 Word 2007 文档中提取宏 (VBA) 代码
Word 2007 转 Word2003格式 docx2doc