c# - 如何在我使用 C# 生成 Word2007 文档时将 "Page x of y"页脚添加到它

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

我看了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

我尝试过的所有方法都完全适合我(我有点接近)。 如果问题有任何不清楚之处,请告诉我。

最佳答案

是的,开始工作了。

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

关于c# - 如何在我使用 C# 生成 Word2007 文档时将 "Page x of y"页脚添加到它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7502703/

相关文章:

c# - 如何使用Parallel.ForEach显示IDictionary的值?

c# - 无法转换类型 'UnityEngine.Component'

c# - PrinterSettings.InstalledPrinters 未获取网络打印机

c# - 确保应用程序独立于用户的屏幕分辨率

c# - PreApplicationStartMethod 属性导致异常

python - 从 MS Word 中提取数据

c# - 类属性中的字典有什么问题?

c# - 我的 Windows 服务中的 .psess 文件是什么?

c# - 如何使用C#获取word文档中的段落编号?

c# - 在 Excel 中调用 WCF 方法切换 "context"并让 Word 继续 "work"