none
Querying a HeaderFooter.Range object in VSTO causes side effects RRS feed

  • Question

  • If I make a Word 2010 addin in Visual Studio and add the code below, opening this document

    http://sdrv.ms/14Nixnu

    will cause the content in the document to move and the document will appear to be edited, causing a Save Changes Dialog when closing the document.

    Can you suggest a fix or a workaround?

    Regards Søren

    private void ThisAddIn_Startup(object senderSystem.EventArgs e)         {             Globals.ThisAddIn.Application.DocumentOpen += new Word.ApplicationEvents4_DocumentOpenEventHandler(Application_DocumentOpen);         }         void Application_DocumentOpen(Word.Document doc)         {             foreach (Word.Section section in doc.Sections)             {                 foreach (Word.HeaderFooter header in section.Headers)                 {                     var rng = header.Range;                 }             }         }

                  

    Wednesday, April 3, 2013 12:54 PM

Answers

  • Hi Søren

    Since accessing the headers/footers means a field update will be triggered that explains why doc.saved = false.

    I've looked at the document and the reason for the behavior is fairly clear: the bottom margin is set to 0 cm but the region provided for the Footer is set at 1.27 cm. So as soon as the footer contains anything there's no longer any room for the Table cell and its text that's positioned at the bottom of the page.

    If you reduce the area provided for the Footer to 0 then the problem doesn't occur - at least, not on my machine. (That's no guarantee for other machine configurations, however, as how the page is actually laid out depends on the versions of Word, windows and the individual printer driver.)

    The only other suggestion I can make is to try accessing this information via the Document.Content.WordOpenXML property. That will return the Word Open XML of the document in the flat-file package format. You should be able to parse that in order to extract the number of content controls in each Header part.


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, April 4, 2013 5:03 PM
    Moderator

All replies

  • Hi Soeren

    What you're seeing is "expected behavior": addressing the headers/footers in a document also activates them. Nothing in your code or question tells us why you're doing this...

    But my recommendation would be to be sure the document is set up such that, when the headers and footers are activated - meaning they have a paragraph mark and will take up the space alotted for them in Page Setup - that this makes no difference to the document layout. To put it another way, make sure the margin settings allow for the headers and footers so that they don't conflict. Or reduce the space alotted for the headers and footers so that they don't push into the margin.


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, April 3, 2013 5:16 PM
    Moderator
  • Hi Cindy

    Thank you for your answer.

    In my actual add in I’m looking for content controls of a certain kind in the headers and footers of the document. This was just a stripped down example, pinpointing the place where I (still) think Word behaves unexpectedly.  A statement like this

    var count = header.Range.ContentControls.Count;

    will have the same effect on the document:  the third text block moves to page 2, and doc.Saved is now false – just by touching the Range property.

    The document is from one of my customers, so I can’t control the layout.

    Activating headers from the Word UI also moves the third block to page 2, but deactivating headers will cause the block to move back into place on page 1, and the document has not changed during the process.

    So my issue here is that merely asking for the number of content controls in a header acts like an edit of the document.

    Regards Søren

    Thursday, April 4, 2013 8:29 AM
  • Hi Søren

    Since accessing the headers/footers means a field update will be triggered that explains why doc.saved = false.

    I've looked at the document and the reason for the behavior is fairly clear: the bottom margin is set to 0 cm but the region provided for the Footer is set at 1.27 cm. So as soon as the footer contains anything there's no longer any room for the Table cell and its text that's positioned at the bottom of the page.

    If you reduce the area provided for the Footer to 0 then the problem doesn't occur - at least, not on my machine. (That's no guarantee for other machine configurations, however, as how the page is actually laid out depends on the versions of Word, windows and the individual printer driver.)

    The only other suggestion I can make is to try accessing this information via the Document.Content.WordOpenXML property. That will return the Word Open XML of the document in the flat-file package format. You should be able to parse that in order to extract the number of content controls in each Header part.


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, April 4, 2013 5:03 PM
    Moderator
  • Hi Cindy

    Since I can open headers from the Ribbon, insert a content control, and close it again while keeping all text on the first page, it still seems like a bug that merely addressing a range property causes the document to change permanently (with no undo option).

    However: your suggestion to use Document.Content.WordOpenXML is a great workaround. All the content controls I need to access are tagged, so through the xml I can get a list of ContentControl.Tag. And having these tags I can get a direct reference to all the controls by using Microsoft.Office.Tools.Word.Document. SelectContentControlsByTag. Which means that I can avoid the section-header-footer iterations altogether, so I’m very happy.

    Thanks

    Søren

    Sunday, April 7, 2013 1:48 PM