Dim section As Word.Section
For Each section In Me.Sections
section.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Fields.Add( _
section.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range, _
Word.WdFieldType.wdFieldEmpty, "AUTOTEXT ""Page X of Y"" ", True)
foreach (Word.Section section in this.Sections)
{
object fieldEmpty = Word.WdFieldType.wdFieldEmpty;
object autoText = "AUTOTEXT \"Page X of Y\" ";
object preserveFormatting = true;
section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Fields.Add(
section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range,
ref fieldEmpty, ref autoText, ref preserveFormatting);