ms-wordvstooffice-interopoffice-addinsword-interop

Word VSTO Add-In: Get Working Area Coordinates / Rectangle on Screen


Using the VSTO Word Interop libraries, how can you get the screen coordinates / rectangle of the main "Working Area"? That is Left, Top, Width and Height.

This image quite nicely shows the area I am looking for highlighted as "DISPLAY" - that is the panel/scroll-viewer containing the document.

Working Area Visualisation

I came across this answer which shows a nice approach pertaining to Ranges and the Window itself, but having dug into Window / ActiveWindow, View and ActivePane, I was not able to find any properties that get me closer to the "Working Area" I am looking for.

A solution / approach in either C# or VBA would be great.


Solution

  • Cindy's kind pointer to the Windows API got me on the right track.

    Using the System.Windows.Automation namespace and the excellent inspect.exe tool, I was able to isolate the ControlType containing the document/working area.

    In practice, the Rect can be obtained as follows:

            var window = AutomationElement.FromHandle(new IntPtr(Globals.ThisAddIn.Application.ActiveWindow.Hwnd));
            var panel = window.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document));
            var docRect = (Rect) panel.GetCurrentPropertyValue(AutomationElement.BoundingRectangleProperty, true);