开发者

How to add a "Page x of y" footer to Word2007 doc as I am generating it using C#

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(footerR开发者_如何学Goange, 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.


Ya, got it working.

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

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.


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

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


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:

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


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.

Imports Microsoft.Office.Interop.Word
Imports Word = Microsoft.Office.Interop.Word

    Dim headerRange As Word.Range = Section.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterEvenPages).Range
    headerRange.Font.Size = 9
    headerRange.Text = "SLUŽBENI GLASNIK BiH" & vbCrLf
    headerRange.Font.Italic = False
    headerRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
    headerRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
    headerRange.Text = "Broj " & Br_Gl & " - Strana "
    headerRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphJustify
    headerRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
    headerRange.Fields.Add(headerRange, CurrentPage)

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.

    headerRange.MoveEnd(Word.WdUnits.wdCharacter, 1)
    headerRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
    headerRange.Text = vbTab & " - O g l a s n i k   j a v n e   n a b a v k e - "
    headerRange.Font.Italic = True
    headerRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
    headerRange.Text = vbTab & DanObjL & ", " & DanObj & ". " & Mjesec & ". " & Godina & "."
    headerRange.Font.Italic = False
    headerRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd)

I hope this can make someone's life easier, and if anyone has a more elegant solution, I'd like to learn. Cheers!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜