I know this is kind of specific to Word, but I avoided posting this in the vba forum because I'm using VSTO and I think the methods employed are different for VSTO vs. VBA.
I'm developing a non-commercial research prototype based on Word, and one task I'm stuck on is getting the coordinates of a Range. It can be relative to either the window or document, doesn't matter. This method works great:
range.get_Information(WdInformation.WdHorizontalPositionRelativeToPage)
and
range.get_Information(WdInformation.WdVerticalPositionRelativeToPage)
However, each of these calls is very, very slow. With about 15 ranges on the page, if I call these two methods for each of those ranges, Word will hang for 1/2-1 second answering those calls. Since every other property of Range is very performant, it seems strange that this one is so slow... since I'm frequently calculating the position of ranges as they change and move around the document, this can really get out of hand. Is there is another way to calculate this information, but more quickly
Another strategy I've tried is inserting a custom windows forms control into the document, hiding it, and then tracking its position by retrieving the control's coordinates from its windows handle with a win32 call. Since it's anchored to a range and reflows with it, I can get the coordinates of a range by simply finding the coordinates of the control. Unfortunately, initially adding the control to the document (using ControlCollection.AddControl) is really slow. It touches the hard disk and makes the Word application window flash (lose and regain focus), and takes about a second. That's not too bad, but occasionally I insert a bunch of these controls at once and it really disrupts the workflow.
Here are some other strategies I've thought of, but haven't been able to validate:
insert a Tools.Bookmark control into the document. It's essentially a range, so if I could find the coordinates of this bookmark control, that would work. I think the bookmark is a Windows Forms control in a host item, and as such has a window handle. If that handle was available I could easily obtain its position through win32 calls.
move the keyboard insertion caret or mouse cursor to a range, and then find the coordinate of the insertion point or mouse through win32 calls (yech).
I know VSTO and the exposed parts of the Word object model aren't really designed for tasks like this, and I would never recommend picking apart some of unsupported APIs like I'm currently doing. However, getting this functionality into Word (no matter how circuitous) will save us a lot of work (months) in doing this proof of concept, so any insight from office or VS devs would be very valuable.
Thanks!
-Phil Crosby

Quickly get coordinates of a Range in Word?
EdwMad
sha240
Hi,
I understand that you would like to get the X and Y cordinates for a cell. There is nothing particular in VSTO that would help with this scenario. I suggest that you post this question to the Excel newsgroups, they will be able to find a solution for you.
http://msdn.microsoft.com/newsgroups/default.aspx dg=microsoft.public.excel.programming&lang=en&cr=US
Regards,
Paul Stubbs
Program Manager
http://blogs.MSDN.com/pstubbs/
http://blogs.MSDN.com/VSTA/
Orcabelle
Not sure if this helps but it may put you on the right path:
http://groups.google.com/group/microsoft.public.word.vba.general/browse_thread/thread/a4aab83eca83ddc3/7793412e01a43bc8 tvc=2#7793412e01a43bc8
Alex Dresko (MVP wannabe)
Paul Stubbs: I was asking about Word, not Excel. Also note that I didn't post this in the word VBA forum because one of my solutions wasn't working because of a performance problem with VSTO winforms controls, and so the question (but seemingly not the answer) is directly VSTO related.
To be clear, this is what I ended up using:
Microsoft.Office.Interop.Word.Window.GetPoint(out left, out top, out width, out height, Range r)
Thanks lynnbt!
-Phil Crosby