PageBreaks in Excel with C#

K

kurisutofu

Hello everyone,

With a C# made software, I create an Excel file with many sheets. Everything
goes well until I tried to remove PageBreaks.

I can add them but I can't delete them so I still have the automatic one
appearing there ...

I tried a lot of things such as:
Range AutoVPageBreaks=ExcelWorksheet1.VPagesBreaks[1].Location;
AutoVPageBreaks.PageBreaks=XlPageBreaks.XlPageBreakNone;

or

VPageBreak VPB1= ExcelWorksheet1.VPagesBreaks[1];
VPB1.Delete();
--> This one raised an error

I also tried to add before RestPageBreaks() or DisplayPageBreaks=false but
nothing worked ... I still have the automatic pagebreaks ...

Does someone know howw to remove those pagebreaks? Do I need to set the
sheet to printpreview? If so, how? (I tried but that raised an error)

I hope you can help me.

Thank you.
 
M

Martin Müller

I'm not sure whether I understood your question correctly.

You can use the Delete() method to delete the page breaks.

BUT: VPageBreaks contains only those page breaks which have influence on
what is actually printed. I.e. if a page break is behind the last cell, it
will not be in VPageBreaks. And if the worksheet contains no data, there will
be no breaks in VPageBreaks.

Thus, if you have to delete all page breaks, you might want to write
something in a cell below and to the right of all page breaks, delete them,
and then clear the cell.
 
K

kurisutofu

Thank you for trying to help me.

That didn't work. The delete function raises a COM exception ...
I can delete manual vpagebreaks but I can't delete the automatic one (I
tried to convert the automated one to manual and delete but that just make
the automatic to appear again ...).
 
M

Martin Müller

Just an idea: worksheet.PageSetup.Zoom = false; ....FitToPagesWide = 1;
.....FitToPagesTall = 1; This will print the sheet on one single page, i.e.
without any (automatic) pagebreaks.
 
K

kurisutofu

Martin Müller said:
Just an idea: worksheet.PageSetup.Zoom = false; ....FitToPagesWide = 1;
....FitToPagesTall = 1; This will print the sheet on one single page, i.e.
without any (automatic) pagebreaks.

It worked! Thank you!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top