question

Ahta14-8622 avatar image
0 Votes"
Ahta14-8622 asked Ahta14-8622 answered

Adding a page number to a range using Word.Interop

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);
     }
dotnet-csharpoffice-vsto-com-dev
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JackJJun-MSFT avatar image
0 Votes"
JackJJun-MSFT answered

@Ahta14-8622, 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.




5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Ahta14-8622 avatar image
0 Votes"
Ahta14-8622 answered

@JackJJun-MSFT this code added number to all pages.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.