Konrad
Konrad

Reputation: 1014

Get headers and footers of slides using PowerPoint Interop

I have a problem - each time I try to get a header or footer that does not exist, the code raises an exception "HeadersFooters (unknown member) : Invalid request. Slides don't have headers."

This is weird since the exception is raised by line:

if (slide.HeadersFooters.Header != null)

Is there any way to get rid of this problem without using the ugly try-catch solution?

Here's my code sample for relevance:

if (slide.HeadersFooters != null)
{
    try
    {
        if (slide.HeadersFooters.Header != null)
        {
            var slideHeaderText = slide.HeadersFooters.Header.Text.Trim();
            if (!string.IsNullOrEmpty(slideHeaderText)) _headersFootersOfDocument.Add(slideHeaderText);

        }
        if (slide.HeadersFooters.Footer != null)
        {
            var slideFooterText = slide.HeadersFooters.Footer.Text.Trim();
            if (!string.IsNullOrEmpty(slideFooterText)) _headersFootersOfDocument.Add(slideFooterText);
        }
    }
    catch (Exception ex)
    {
        Console.Writeline(ex);
    }
}

Upvotes: 0

Views: 685

Answers (1)

Steve Rindsberg
Steve Rindsberg

Reputation: 14809

As the error message points out, slides don't have headers. Can't have headers ... there's no such object in the object model. To avoid throwing errors when accessing objects that don't exist, don't access them!

Upvotes: 1

Related Questions