Как добавить нижний колонтитул "Page x of 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

Ничто из того, что я пробовал, не сработало для меня полностью (я подошел немного ближе). Дайте мне знать, если что-то в этом вопросе неясно.

5
задан Eonasdan 21 September 2011 в 16:11
поделиться