简体   繁体   中英

MS Word Interop - access CentimetersToPoints method

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

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"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM