@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.