locked
Add Page # of n to footer RRS feed

  • Question

  • Hi All,

    I am looking something like this in my footer "Page 1 of 4" om right alignment;

    and some text in my footer "Some text" on left alignment 

    I can current page number on all page but I have no idea how i can set "Page 1 of 4 "

    Here is my code:

     //Add the footers into the document
                    foreach (Microsoft.Office.Interop.Word.Section wordSection in document.Sections)
                    {
                        
                        //Get the footer range and add the footer details.
                        Range footerRange = wordSection.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
                        footerRange.Font.ColorIndex = WdColorIndex.wdGray50;
                        footerRange.Font.Size = 10;
                        footerRange.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
                        footerRange.Text = "some text";
                        object currpage = WdFieldType.wdFieldPage;
                        object totalpage = WdFieldType.wdFieldNumPages;                                
                        footerRange.Fields.Add(footerRange, currpage, WdParagraphAlignment.wdAlignParagraphRight);  
                    }


    Split off from https://social.msdn.microsoft.com/Forums/office/en-US/4cebf475-687d-451f-a80a-08284fd0ce13/how-to-add-page-number-automatically-in-all-pages-using-c?forum=worddev
    • Split by Cindy Meister MVP Tuesday, November 24, 2015 4:34 PM new question on old answered thread split for visibility
    • Edited by Cindy Meister MVP Tuesday, November 24, 2015 4:40 PM provide link to possible context
    Tuesday, November 24, 2015 3:23 PM

Answers

  • You're on the right track, but you're using Fields.Add incorrectly. The third parameter has nothing to do with paragraph alignment and you should remove that parameter.

    Often, it's easiest to understand how to write your code by performing the task you need in the Word application user interface. The API is closely associated with that.

    So if you go to the footer of a Word document and

    1. Type: "some text"
    2. Press the TAB key twice - should take you to the right side of the page.
      The default Footer style includes tab stops for center and right alignment
    3. Type: Page
    4. Insert the Page field (as your code does)
    5. Type: of
    6. Insert the NumPages field

    That should work and also tells you how your code basically needs to be constructed. Where you type text as a user, in your code you need Range.Text. Include ANSI 9 (\t) for the tab presses in your string.

    The only real stumbling block is getting the field codes inserted, and in the correct order. For that, you need to create Word.Field objects and assign the field you insert to it: Word.Field fldPage = Range.Fields.Add(//params);

    You need to get the "focus" for the " of " after the field you insert. The Field object has two properties that return Range objects: Code and Result. In this case you need the Result property: Word.Range rngField = fldPage.Result; You can then collapse that Range to its end point in order to continue after the field:

        object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd; 
        rngField.Collapse(ref oCollapseEnd);
        rngField.Text = " of ";
        rngField.Collapse(ref oCollapseEnd);
        //now insert the next field


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    Tuesday, November 24, 2015 4:51 PM