【问题标题】:How to add a "Page x of y" footer to Word2007 doc as I am generating it using C#当我使用 C# 生成“Page x of y”页脚时,如何将页脚添加到 Word2007 文档
【发布时间】:2011-09-21 15:45:14
【问题描述】:

我看了hereherehere

我试过了:

    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

我尝试过的任何事情都没有完全对我有用(我有点接近)。 如果问题不清楚,请告诉我。

【问题讨论】:

  • 你有没有得到这个工作?我也想做同样的事情。
  • @Christopher Mahan,对不起,我在某个时候停止了尝试,并决定发出 LaTeX 代码并编译它。这对我来说已经足够好了。如果你解决了这个问题,那么我很乐意看看答案。我可以建议在赏金上烧掉你的一些声誉。设置得高一些,但一定要说明你需要什么。

标签: c# .net-4.0 ms-word office-interop page-numbering


【解决方案1】:

嗯,搞定了。

// 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。

【讨论】:

    【解决方案2】:

    这是在 VB 中,但我试过了,它对我有用,尽管在这里你必须提供当前页码和总页码。我确信有更好的解决方案:/

    WordBasic.viewfooteronly
    Selection.EndKey Unit:=wdStory
    Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
    Selection.TypeText Text:="Page " & current & " of " & total
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    

    【讨论】:

    • 另外,您可以尝试在希望页脚所在的位置添加书签,然后设置书签 range.text 属性
    【解决方案3】:

    这个链接帮助解决了这个问题

    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 中解决它的方法:

    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
    

    【讨论】:

      猜你喜欢
      • 2014-07-26
      • 2016-11-30
      • 2017-07-21
      • 2019-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 2019-09-21
      相关资源
      最近更新 更多