Question

I have the below code which is for modifying a textframe in a word document at runtime.

This is in a .NET Windows Forms app.

var oShapes = oWord.ActiveDocument.Shapes; 
var titleShape = oShapes["Title"];

var myWord = new Microsoft.Office.Interop.Word.Application();

titleShape.Height = myWord.InchesToPoints(1.75F);
titleShape.Width = myWord.InchesToPoints(0.45F);
titleShape.RelativeHorizontalPosition = Microsoft.Office.Interop.Word.WdRelativeHorizontalPosition;
titleShape.RelativeVerticalPosition =  Microsoft.Office.Interop.Word.WdRelativeVerticalPosition();
titleShape.Left = 4.35F;
titleShape.Top = 17.5F;
titleShape.TextFrame.WordWrap = 0;
titleShape.LockAnchor = 1;

var sTitle =string.Empty;

titleShape.TextFrame.TextRange.Text = DocumentType; // sTitle;
titleShape.TextFrame.AutoSize=-1;

Unfortunately I must have got it wrong because Microsoft.Office.Interop.Word.WdRelativeHorizontalPosition; is showing as an error in VS 2010 (and also on the line below.

What is the correct way to use word enumerated constants like that in c# through Office Interop?

Was it helpful?

Solution 3

I found it, I was calling the enumeration name, rather than the enumerated constant.

So Microsoft.Office.Interop.Word.WdRelativeHorizontalPosition

should be

Microsoft.Office.Interop.Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage;

(plus I was using brackets thinking that was the problem)

OTHER TIPS

remove the parentheses:

titleShape.RelativeVerticalPosition =  Microsoft.Office.Interop.Word.WdRelativeVerticalPosition;

you also don't say what the compilation error is.

The problem is that you are calling the property as if it was a method. Remove the paranthesis at the end and you should be done.

titleShape.RelativeVerticalPosition =  Microsoft.Office.Interop.Word.WdRelativeVerticalPosition;

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top