Home » excel » C# Excel (2003) interop – Margins don't correspond to the input value?

C# Excel (2003) interop – Margins don't correspond to the input value?

Posted by: admin April 23, 2020 Leave a comment

Questions:

I’m using dotNET C# to communicate with Excel 2003.
I’m trying to change the print margins of the document, but the values that I put in, don’t correspond to the margin values that Excel then uses…

xls.PageSetup setup = ws.PageSetup;
setup.Orientation = xls.XlPageOrientation.xlLandscape;

//Standard margins (Top - Bottom - Left - Right):
//                  2.5 - 2.5 - 1.9 - 1.9


setup.BottomMargin = 1.0;
setup.TopMargin = 1.0;
setup.LeftMargin = 1.0;
setup.RightMargin = 1.0;
//Excel gives me: 0.0 - 0.0 - 0.0 - 0.0

setup.BottomMargin = 20.0;
setup.TopMargin = 20.0;
setup.LeftMargin = 20.0;
setup.RightMargin = 20.0;
//Now Excel gives me: 0.7 - 0.7 - 0.7 - 0.7

setup.BottomMargin = 30.0;
setup.TopMargin = 30.0;
setup.LeftMargin = 30.0;
setup.RightMargin = 30.0;
//Now Excel gives me: 1.1 - 1.1 - 1.1 - 1.1

As you can see, Excel does change its margins, but it doesn’t correspond to the double I’m putting in.
Is there a factor that I’m missing?

Many thanks for your feedback!

How to&Answers:

From http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.pagesetup.rightmargin(v=office.11).aspx

Margins are set or returned in points. Use the InchesToPoints method or the CentimetersToPoints method to convert measurements from inches or centimeters.

Hope this helps