Search code examples
c#.netms-wordinteropoffice-interop

How to expand Word header and footer via C# word interop


When bulk editing images in the header and footer of word documents via word interop it works perfectly well for most documents. If however the header and footer of a document are collapsed, word crashes.

Now to my question: Can you expand the header/footer section automatically via word interop or do you know of another way to work around this problem?

Additional info:

When expanding the header/footer section manually and saving the document it works again, but that's no reasonable option because there are many documents to edit.

Collapsed header (doesn't work): screenshot of the collapsed header (doesn't work)

Expanded header (works): screenshot of the expanded header (works)

The Word error information: screenshot of the word error information

The code I'm using for editing header images:

foreach (Section section in currentDocument.Sections)
{
    HeadersFooters headerFooters = section.Headers;
    foreach (HeaderFooter headerFooter in headerFooters)
    {
        InlineShapes inlineShapes = headerFooter.Range.InlineShapes;
        foreach (InlineShape shape in inlineShapes)
        {
            if (shape.Type != WdInlineShapeType.wdInlineShapePicture)
                continue;
            //[...]
        }
    }
}

Solution

  • It is possible to turn the full page display on/off:

    Word.View vw = currentDocument.ActiveWindow.View;
    if (vw.DisplayPageBoundaries == false) 
    {
       vw.DisplayPageBoundaries = true;
    }