If the Page Number option in Headers and Footers is grayed out in Word, it could be due to a few reasons:
- Document Protection: Check if the document is protected. If it is, you may need to unprotect it to make changes to the headers and footers.
- Section Breaks: Ensure that you are not in a section of the document where headers and footers are disabled. Sometimes, certain sections may have different settings.
- Compatibility Mode: If the document is in compatibility mode (for older versions of Word), some features may be limited. Try converting the document to the latest format.
- Editing Restrictions: If the document has editing restrictions, you may need to remove these restrictions to access the Page Number feature.
You can also insert page numbers programmatically using the following code snippet if you are working with VSTO Add-ins:
C#
foreach (Word.Section section in this.Application.ActiveDocument.Sections)
{
Word.Range headerRange = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
headerRange.Fields.Add(headerRange, Word.WdFieldType.wdFieldPage);
headerRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
}
VB
For Each section As Word.Section In Me.Application.ActiveDocument.Sections
Dim headerRange As Word.Range = section.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
headerRange.Fields.Add(headerRange, Word.WdFieldType.wdFieldPage)
headerRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
Next
This code adds page numbers to the primary header of each section in the active document, aligning them to the right.