1

I am trying to set the print margins in a Word Doc I am printing from my c# application but I am having trouble accessing the methods that I need to call (based on what they are in MS Word VBA)

In VBA the code looks like this:

Options.printBackground = False

With ActiveDocument.PageSetup
    .TopMargin = CentimetersToPoints(0.61)
    .BottomMargin = CentimetersToPoints(0.43)
    .LeftMargin = CentimetersToPoints(1.27)
    .RightMargin = CentimetersToPoints(0.43)
    .Gutter = CentimetersToPoints(0)
End With

Here is my c# code

oWordDoc.PageSetup.TopMargin = Microsoft.Office.Interop.Word.Application.CentimetersToPoints(float.Parse ("0.61"))  ;

The error I am getting is: An object reference is required for the non-static field, method, or property 'Microsoft.Office.Interop.Word._Application.CentimetersToPoints(float)'

I have tried several varaiations after searching the VS 2010 Object Browser for CentimetersToPoints

The available interfaces in the Object Browser are:

  • Microsoft.Office.Interop.Word._Application.CentimetersToPoints(float)
  • Microsoft.Office.Interop.Word.ApplicationClass.CentimetersToPoints(float)
  • Microsoft.Office.Interop.Word._Global.CentimetersToPoints(float)
  • Microsoft.Office.Interop.Word.GlobalClass.CentimetersToPoints(float)

how can I access methods like this?

thanks

Our Man in Bananas
  • 5,809
  • 21
  • 91
  • 148

1 Answers1

2

As the error message indicates, you need a reference to the Word.Application object to access this method, since it is not static.

I guess you have a oWordApp somewhere that you use to create or open the oWordDoc, so you can access the method from this object.

Or you can retreive the instance of the Word.Application from your oWordDoc (Word.Document) object.

oWordDoc.PageSetup.TopMargin = oWordDoc.Application.CentimetersToPoints(float.Parse("0.61"));
Chris
  • 8,527
  • 10
  • 34
  • 51