none
How to add page number automatically in all pages using C# RRS feed

  • Question

  • Hi All,

    I faced an issue regarding add page numbers for all pages in one Word Document. I have tried the code below,

    	_document.ActiveWindow.View.Type = Word.WdViewType.wdPrintView;
    
    	object nullobject = Missing.Value;
    
    	_document.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekEvenPagesFooter;
    
    	_document.ActiveWindow.Selection.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
    
    	_document.ActiveWindow.Selection.Font.Name = "Arial";
    
    	_document.ActiveWindow.Selection.Font.Size = 8;
    
    	Object CurrentPage = Word.WdFieldType.wdFieldPage;
    
    	_document.ActiveWindow.Selection.Fields.Add(_document.ActiveWindow.Selection.Range, ref CurrentPage, ref nullobject, ref nullobject);

    It can only insert the page number in even page, such as page 2, 4, 6. But the code cannot add page number in page 1,3,5.

    How to solve this issue?

    Also, we can add the page number, But how to remove the page number if i didn't want to display?

    Thanks,


    The future belongs to those who believe in the beauty of their dreams.

    Tuesday, May 26, 2015 9:00 AM

Answers

  • You are only looking at the footer for the even pages:

    Word.WdSeekView.wdSeekEvenPagesFooter;

    change it to:

    Word.WdSeekView.wdSeekPrimaryFooter;

    Then you should get the primary footer on all pages.

    But that aside, you are also ignoring the fact that your document could contain sections with different footers, you need to iterate each section really and update the footers:

    foreach (Word.Section sec in _document.Sections) {
    Word.Range rng = sec.Footers(Word.WdSeekView.wdSeekPrimaryFooter).Range; 
    rng.Font.Name = "Arial"; 
    rng.Font.Size = 8; 
    rng.Fields.Add(rng, Type: Word.WdFieldType.wdFieldPage);
    }

    • Edited by Nick Dewitt Tuesday, May 26, 2015 1:27 PM added version by section
    • Marked as answer by Yang,Chenfei Thursday, May 28, 2015 12:49 AM
    Tuesday, May 26, 2015 1:12 PM
  • Hi Yang, Chenfei

    Nick's answer is correct, as far as it goes. But you should NOT be using SeekView in order to work with Word's headers and footers, nor should you work with Selection. This is one place where the macro recorder doesn't give you reliable code. Instead, you should work with the object model representations directly. For any document where the options for "Different First Page" and "Odd/Even pages" is NOT activated and the document has only one section:

    Word.Range rng = _document.Sections[1].HeadersFooters(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
    rng.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
    rng.Font.Name = "Arial";
    rng.Font.Size = 8;
    rng.
    Fields.Add(rng, ref CurrentPage, ref nullobject, ref nullobject);

    <<Also, we can add the page number, But how to remove the page number if i didn't want to display?>>

    Under what kind of circumstances would you not want the page number to display? The way Headers and Footers are designed, you can have different content only with the two options mentioned above and/or by using Section Breaks, which allow you to define different headers, footers, margins, newspaper column settings, etc.


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Yang,Chenfei Thursday, May 28, 2015 12:49 AM
    Wednesday, May 27, 2015 4:52 PM
    Moderator

All replies

  • You are only looking at the footer for the even pages:

    Word.WdSeekView.wdSeekEvenPagesFooter;

    change it to:

    Word.WdSeekView.wdSeekPrimaryFooter;

    Then you should get the primary footer on all pages.

    But that aside, you are also ignoring the fact that your document could contain sections with different footers, you need to iterate each section really and update the footers:

    foreach (Word.Section sec in _document.Sections) {
    Word.Range rng = sec.Footers(Word.WdSeekView.wdSeekPrimaryFooter).Range; 
    rng.Font.Name = "Arial"; 
    rng.Font.Size = 8; 
    rng.Fields.Add(rng, Type: Word.WdFieldType.wdFieldPage);
    }

    • Edited by Nick Dewitt Tuesday, May 26, 2015 1:27 PM added version by section
    • Marked as answer by Yang,Chenfei Thursday, May 28, 2015 12:49 AM
    Tuesday, May 26, 2015 1:12 PM
  • Hi Yang, Chenfei

    Nick's answer is correct, as far as it goes. But you should NOT be using SeekView in order to work with Word's headers and footers, nor should you work with Selection. This is one place where the macro recorder doesn't give you reliable code. Instead, you should work with the object model representations directly. For any document where the options for "Different First Page" and "Odd/Even pages" is NOT activated and the document has only one section:

    Word.Range rng = _document.Sections[1].HeadersFooters(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
    rng.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
    rng.Font.Name = "Arial";
    rng.Font.Size = 8;
    rng.
    Fields.Add(rng, ref CurrentPage, ref nullobject, ref nullobject);

    <<Also, we can add the page number, But how to remove the page number if i didn't want to display?>>

    Under what kind of circumstances would you not want the page number to display? The way Headers and Footers are designed, you can have different content only with the two options mentioned above and/or by using Section Breaks, which allow you to define different headers, footers, margins, newspaper column settings, etc.


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Yang,Chenfei Thursday, May 28, 2015 12:49 AM
    Wednesday, May 27, 2015 4:52 PM
    Moderator
  • Hi Cindy and Nick,

    Thank you very much. Currently, I used the solution provided by Cindy and solved this issue.

    Regards,


    The future belongs to those who believe in the beauty of their dreams.

    Thursday, May 28, 2015 12:50 AM
  • @All,

    If we want to set the page number in the center, we should set it in the end, I test it in Word 2007.

            private void Common(Word.Range range)
            {
                object nullobject = System.Reflection.Missing.Value;
    
                range.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
    
                Object CurrentPage = Word.WdFieldType.wdFieldPage;
    
                _document.ActiveWindow.Selection.Fields.Add(range, ref CurrentPage, ref nullobject, ref nullobject);
    
                range.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
            }
    Thanks,


    The future belongs to those who believe in the beauty of their dreams.

    Thursday, May 28, 2015 1:09 AM