You're on the right track, but you're using Fields.Add incorrectly. The third parameter has nothing to do with paragraph alignment and you should remove that parameter.
Often, it's easiest to understand how to write your code by performing the task you need in the Word application user interface. The API is closely associated with that.
So if you go to the footer of a Word document and
- Type: "some text"
- Press the TAB key twice - should take you to the right side of the page.
The default Footer style includes tab stops for center and right alignment - Type: Page
- Insert the Page field (as your code does)
- Type: of
- Insert the NumPages field
That should work and also tells you how your code basically needs to be constructed. Where you type text as a user, in your code you need Range.Text. Include ANSI 9 (\t) for the tab presses in your string.
The only real stumbling block is getting the field codes inserted, and in the correct order. For that, you need to create Word.Field objects and assign the field you insert to it: Word.Field fldPage = Range.Fields.Add(//params);
You need to get the "focus" for the " of " after the field you insert. The Field object has two properties that return Range objects: Code and Result. In this case you need the Result property: Word.Range rngField = fldPage.Result; You
can then collapse that Range to its end point in order to continue after the field:
object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
rngField.Collapse(ref oCollapseEnd);
rngField.Text = " of ";
rngField.Collapse(ref oCollapseEnd);
//now insert the next field
Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>