I am currently using the following method to find the coordinates of a Range
within a document:
private Rectangle GetRangeCoordinates(Window w, Range r)
{
int left = 0;
int top = 0;
int width = 0;
int height = 0;
w.GetPoint(out left, out top, out width, out height, r);
return new Rectangle(left, top, width, height);
}
This works really well unless the Range
is off the screen by a fairly large margin (quite a few pages), in which case I get the following exception:
System.Runtime.InteropServices.COMException (0x800A1066): Command failed at Microsoft.Office.Interop.Word.Window.GetPoint(Int32& ScreenPixelsLeft, Int32& ScreenPixelsTop, Int32& ScreenPixelsWidth, Int32& ScreenPixelsHeight, Object obj) at [ProjectName].[TaskpaneName].GetRangeCoordinates(Window w, Range r) in [...somePath...][TaskpaneName].cs:line 66
Is there a way to figure out if a Range
is on screen or not, so that I can only call this method when it is?