CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2002
    Location
    Fort Worth, TX
    Posts
    19

    C# Word automation - footer added but not visible

    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.

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: C# Word automation - footer added but not visible

    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
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    May 2002
    Location
    Fort Worth, TX
    Posts
    19

    Re: C# Word automation - footer added but not visible

    Thanks worked, here's the line of code I'm using

    Code:
    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
    <wocPr>
    <wontDisplayPageBoundaries/>

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured