Getting a Shape's page relative position
-
2012年3月28日 23:13
Paul Foster
My first attempt to post was with IE 8 and it didn't take the content from the editor :(So, reposting in Firefox:
Platfom is VS 2010 / Microsoft Word 2007
General use case is parsing TextBoxes out of a Word document, assembling them into groups for copying/exporting.
I have a document with TextBox Shape objects that all have absolute positions, with most Relative to the Page, but I have a couple of TextBoxes with positions such as "-0.1" to the right of the "Column" and "2.0" below the "Paragraph". I need to compare positions of all the TextBoxes on the page, so I need them all with Page-relative abosolute positions. I can't seem to find an API that tells me:
1. What the Shape.Top & Shape.Left properties are relative to.
2. How to get the page-relative position of the object it's relative to.
If it makes any difference - I'm testing using a stand alone console app, just using the Interop libraries to talk to word. I'm not running as an Add-in.
- 已编辑 pfoster 2012年3月28日 23:20
全部回复
-
2012年4月2日 4:15版主
Hi Paul,
Thanks for posting in the MSDN Forum.
It's based on my experience the properties which your mentioned are measured via point (72 points = 1 inch).
I hope it can help you.
Have a good day,
Tom
Tom Xu [MSFT]
MSDN Community Support | Feedback to us
-
2012年4月2日 16:23
My question wasn't related to the units that the property is in. I am aware they're in points. Let me clarify:
I have the values circled in Green, what I need to know is the property values circled in yellow.
Paul Foster
-
2012年4月3日 20:00
I've partially answered my own question, but raised some more related to the same issue.
I see the following properties:
Microsoft.Office.Interop.Word.Shape.WrapFormat.Type - tells the type of wrap (from the second page of the properties dialog above)
Microsoft.Office.Interop.Word.Shape.RelativeHorizontalPosition - An enum value indicating the value of the "to the right of". Values are Character, Column, Page, others ...
But I'm still confused by the fact that these three different ways to get the position are giving me three different results:
Shape shape; // Approach 1 int topRelative = shape.TopRelative; // Approach 2 int top = shape.Top; // Approach 3 shape.Select(); int topFromSelect = shape.Application.Selection.get_Information(Word.WdInformation.wdHorizontalPositionRelativeToPage);
For example, here's a Shape with properties:
- Type: msoPicture
- RelativeVerticalPosition: wdRelativeVerticalPositionPage
- WrapFormat.Type: wdWrapBehind
- Top (Approach 1): -0.5
- TopRelative (Approach 2): -999999
- (Approach 3): 205.15
- RelativeHorizontalPosition: wdRelativeHorizontalPositionPage
- Left (Approach 1): -0.5
- LeftRelative (Approach 2): -999999
- (Approach 3): 18.15
- Width: 613
- Height: 793
It should be noted also that these shapes were created by automated software, not necessarily a human using Word.
What's the explanation as to why the values are different, even when the Relative*Position properties all say *Page?
Paul Foster
-
2012年4月13日 18:47版主
Hello Paul,
This applies to the duplicate thread you entered in the "Word for Developers" Forum. Thank you for your patience in both Forum threads. The list of content, and the remarks for several address your question for why you see three different values for the position of the shape. The most telling is that the position of a shape is always related to the anchor point - which is briefly described in two of the topics listed.
Shape.TopRelative Property (Microsoft.Office.Interop.Word)
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.shape.toprelative.aspx
Shape.TopRelative Property (Word) - MSDN – Explore Windows, Web ...
http://msdn.microsoft.com/en-us/library/ff196536.aspx
Shape.RelativeHorizontalPosition Property (Microsoft.Office ...
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.shape.relativehorizontalposition.aspx
Shape Properties (Microsoft.Office.Interop.Word)
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.shape_properties.aspx
See this statement in the following page “A shape is always attached to an anchoring range”:
Shape Object - MSDN – Explore Windows, Web, Cloud, and Windows ...
http://msdn.microsoft.com/en-us/library/aa223088(v=office.11).aspx
Shape.Anchor Property (Word) - MSDN – Explore Windows, Web ...
http://msdn.microsoft.com/en-us/library/ff835500.aspx
Please see the code examples in this page:
RelativeHorizontalPosition Property - MSDN – Explore Windows ...
http://msdn.microsoft.com/en-us/library/aa196492(v=office.11).aspx
Once again, thanks for your patience.Regards,
Chris Jensen
Senior Technical Support LeadChris Jensen
- 已标记为答案 pfoster 2012年4月13日 23:53

