Free Support Forum -

Pagesetup.getheader problem


I am looping through all the worksheets in a excel document. The document in question has headers on every sheet but half of the worksheets are of made from a chart template.

The outer loop, loops through all worksheets (chart sheets as well), and the pagesetup.getheader returns a value for the normal worksheets, but the pagesetup.getheader only returns a null on the chart type worksheets.

Am I doing somthing wrong? Or does Pagesetup.getheader not work on worksheets created from a graph template.


The code in question:

for (j = 0; j < wbook.Worksheets.Count; j++)


for (i = 0; i < 3; i++)


if (wbook.Worksheets[j].PageSetup.GetHeader(i) != null)

stringHeader = wbook.Worksheets[j].PageSetup.GetHeader(i).ToString();

do somthing....


Please change your codes. See following codes:

for (int j = 0; j < wbook.Worksheets.Count; j++)
Worksheet sheet = wbook.Worksheets[j];
PageSetup pageSetup = sheet.Type == SheetType.Chart ? sheet.Charts[0].PageSetup : sheet.PageSetup;
for (int i = 0; i < 3; i++)

if (pageSetup.GetHeader(i) != null)

stringHeader = pageSetup.GetHeader(i).ToString();

If you still could not get any header ,please post your template file.

That worked a treat.