Codename K
Codename K

Reputation: 744

Word VSTO - Finding the location of a shape in the document?

I am building a Word VSTO (VB.NET) program where I need to find the exact position in Points of a shape from Top, Left, Right and Bottom. I use the following code,

objShape = Globals.ThisAddIn.Application.ActiveDocument.Shapes(intShapesLoop)

objShape.RelativeHorizontalPosition = Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin

objShape.Select()

sngPageWidth = Globals.ThisAddIn.Application.Selection.Range.PageSetup.PageWidth
sngPageHeight = Globals.ThisAddIn.Application.Selection.Range.PageSetup.PageHeight

sngMarginsLeft = objShape.Left + Globals.ThisAddIn.Application.Selection.Range.PageSetup.LeftMargin
sngMarginsRight = sngPageWidth - (objShape.Width + sngMarginsLeft + sngGutterPosistionRight)

This works fine and shows the correct location values from Left and Right. However, I use the below code for the Top and Bottom locations,

sngMarginsTop = objShape.Top + Globals.ThisAddIn.Application.Selection.Range.PageSetup.TopMargin
sngMarginsBottom = sngPageHeight - (objShape.Height + sngMarginsTop)

This shows the wrong position values. What is the issue here? From the Top value it shows about 12 Points less than the correct value

I found that this happens only on few documents. It shows the correct Top value on most documents but on few it shows the wrong Top value.

Upvotes: 0

Views: 997

Answers (1)

Codename K
Codename K

Reputation: 744

This is the reason why the top value is wrong,

In the Advanced Layout dialog (Text Wrapping > More Layout Options...),

enter image description here

The combo boxes marked by the red rectangles have to be set as Margin. The reason the top value was wrong is because the Absolute position ... below is set to Paragraph instead of Margin. When this is set to Margin the top value became correct.

Upvotes: 2

Related Questions