Adding a page number to a range using Word.Interop

Ahta 14 1 Reputation point
2021-09-14T14:48:01.223+00:00

I want to add a page number to a page range using Word.Interop. I have added the page number using the code below. This code adds page numbers from page 2 to the end of the document (pageNum = 2).

For example, I want to add page numbers from page 2 to page 8. I tried using the 'count' property with Selection.GoTo() but the situation did not change, I am waiting for your help

Code:

private void HeaderOrFooterAddPageNumber(int pageNum, _Application wdApp, _Document doc, Data data, Section section, HeaderFooter headerfooter)
    {

        CheckHeaderOrFooter(doc, section, headerfooter);
        object missing = Type.Missing;

        wdApp.Selection.GoTo(Word.WdGoToItem.wdGoToPage, Word.WdGoToDirection.wdGoToFirst, ref missing, pageNum.ToString());
        Range rngPageNum = wdApp.Selection.Range;

        rngPageNum.InsertBreak(Word.WdBreakType.wdSectionBreakNextPage);

        Section currSec = doc.Sections[rngPageNum.Sections[1].Index];
        HeaderFooter ftr = currSec.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary];

        ftr.LinkToPrevious = false;
        ftr.PageNumbers.RestartNumberingAtSection = true;
        ftr.PageNumbers.StartingNumber = 1;
        if (!CheckNullOrWhiteSpace(data.PageNumbersNumStyle))
        {
            if (float.Parse(data.PageNumbersNumStyle) == 2) ftr.PageNumbers.NumberStyle = WdPageNumberStyle.wdPageNumberStyleLowercaseRoman;
        }

        object TotalPages = WdFieldType.wdFieldSectionPages;
        object page = WdFieldType.wdFieldPage;
        Range rngCurrSecFooter = ftr.Range;

        rngCurrSecFooter.Font.Name = CheckNullOrWhiteSpace(data.PageNumbersFont) ? "Times New Roman" : data.PageNumbersFont;
        rngCurrSecFooter.Font.Size = CheckNullOrWhiteSpace(data.PageNumbersFontSize) ? float.Parse("12") : float.Parse(data.PageNumbersFontSize);
        rngCurrSecFooter.Font.ColorIndex = CheckNullOrWhiteSpace(data.PageNumbersFontColor) ? WdColorIndex.wdBlack : (WdColorIndex)float.Parse(data.PageNumbersFontColor);
        rngCurrSecFooter.Paragraphs.Alignment = CheckNullOrWhiteSpace(data.PageNumbersFontAlignment) ? WdParagraphAlignment.wdAlignParagraphCenter : (WdParagraphAlignment)float.Parse(data.PageNumbersFontAlignment);
        rngCurrSecFooter.Collapse(Word.WdCollapseDirection.wdCollapseStart);
        rngCurrSecFooter.Fields.Add(rngCurrSecFooter, ref page, ref missing, true);
    }
Microsoft 365 and Office Development Other
Developer technologies C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jack J Jun 25,296 Reputation points
    2021-09-15T03:21:47.077+00:00

    @Ahta 14 , you could try the following code to add a page number to a page range by using word.interop.

    Code:

        string pageNum1 = "2";  
        string pageNum2 = "8";  
        Object missing = System.Reflection.Missing.Value;  
        Word.Application wdApp = new Word.Application();  
        var selection = wdApp.Selection;  
        Word.Document doc = wdApp.Documents.Open("D:\\1.docx");  
        Word.Range startRange = wdApp.Selection.GoTo(Word.WdGoToItem.wdGoToPage, Word.WdGoToDirection.wdGoToAbsolute, ref missing, pageNum1);  
        Word.Range rngPageNum = wdApp.Selection.Range;  
        Word.Range endRange = wdApp.Selection.GoTo(Word.WdGoToItem.wdGoToPage, Word.WdGoToDirection.wdGoToAbsolute, ref missing, pageNum2);  
        rngPageNum.End = wdApp.Selection.Bookmarks["\\Page"].Range.End;  
        rngPageNum.Select();  
        rngPageNum.InsertBreak(Word.WdBreakType.wdSectionBreakNextPage);  
        Word.Section currSec = doc.Sections[rngPageNum.Sections[1].Index];  
        Word.HeaderFooter ftr = currSec.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary];  
        ftr.LinkToPrevious = false;  
        ftr.PageNumbers.RestartNumberingAtSection = true;  
        ftr.PageNumbers.StartingNumber = 1;  
        object TotalPages = Microsoft.Office.Interop.Word.WdFieldType.wdFieldSectionPages;  
        object CurrentPage = Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage;  
        Word.Range rngCurrSecFooter = ftr.Range;  
        rngCurrSecFooter.Fields.Add(rngCurrSecFooter, ref CurrentPage, ref missing, false);  
        rngCurrSecFooter.InsertAfter(" of ");  
        rngCurrSecFooter.Collapse(Word.WdCollapseDirection.wdCollapseEnd);  
        rngCurrSecFooter.Fields.Add(rngCurrSecFooter, ref TotalPages, ref missing, false);  
        doc.Save();  
        doc.Close();  
    

    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.