Click to See Complete Forum and Search --> : C# Word automation - footer added but not visible


dannyl
July 31st, 2009, 05:24 PM
I'm using automation to create a word document using C#. I've got it working pretty well but need to add a footer. I've done this with the following code.

foreach (Microsoft.Office.Interop.Word.Section section in wordDoc.Sections)
{
Object CurrentPage = Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage;
section.Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Fields.Add(section.Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range, ref CurrentPage, ref oMissing, ref oMissing);
section.Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.set_Style(ref oBodyStyleCenter);
}

That works fine, but when I open the word doc up after my application closes the headers and footers are not displayed. If I go to View->Header and Footers, they show up with my page number in them.

So my question is, how do I programatically get them to show up? I'm sure I'm missing some stupid little thing.

Thanks

dglienna
July 31st, 2009, 06:06 PM
Start the Macro Recorder, turn on the Header/Footer, type something, then turn them off again.

Stop the recorder, and you should see what you left off. Check if they're OFF, then turn them ON

dannyl
August 3rd, 2009, 08:31 AM
Thanks worked, here's the line of code I'm using

wordApp.ActiveWindow.ActivePane.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdPrintPreview;

However, that seems a bit hacky to me, up until that point in my code I wasn't having to deal with any ActiveWindow stuff, everything was done on the document.

It seems like there should be an option on the document itself to display or not display the headers and footers. Another thing leading me to believe this is that I saved an old version (no headers/footers displayed) as XML and a new version (headers/footers displayed) as XML and did a text compare. The only difference was that in the old document I had this tag defined and in the new one I did not
<w:docPr>
<w:dontDisplayPageBoundaries/>

So if any one has dealt with this before, I'm still hopeful for a more appropriate way to turn on headers and footers.