The Application object has several properties that are used to control Word's windows. We have already considered several properties, including Width, Height, WindowState, Top, Left, Windows, ActiveWindow, and ShowWindowsInTaskBar. Word provides some additional methods on the Application object that prove useful for managing windows. The Application object's Activate method is used to make Word the active application when another application has focus. The Application object's Move method is used to move the active window when the WindowState is set to wdWindowStateNormal and takes Top and Left parameters in pixels. The Application object's Resize method is used to resize the active window when the WindowState is set to wdWindowStateNormal and takes Width and Height parameters in pixels. Creating New WindowsThe Application object's NewWindow method creates a new window for the active document and returns the newly created Window. This is the equivalent of choosing New Window from the Window menu. You can also create a new window using the Windows collection's Add method. This method takes an optional Window parameter by reference, which tells Word which document to create a new Window for. If you omit the Window parameter, Word will create a new window for the active document. Iterating over the Open WindowsThe Windows collection returned by the Windows property of the Application object has a GetEnumerator method that allows it to be iterated over using a For Each loop in Visual Basic 2005, as shown in Listing 8.15. Listing 8.15. A VSTO Customization That Iterates over the Open Windows
Accessing a Window in the CollectionTo access a Window in the Windows collection, you use a parameterized property called Item, which returns a Window. The Item property takes an Index parameter by reference that is of type Object. You can pass a String representing the caption of the Window, or you can pass a 1-based index into the Windows collection. You can check how many items are in a given collection by using the Count property. Listing 8.16 shows both getting a window using a 1-based index and using the caption of a window. Because Item is also the default property for a collection, you can omit Item and instead pass the parameter, as shown in line of code app.Windows(stringIndex). Listing 8.16. A VSTO Customization That Uses Item to Get a Window
Arranging WindowsWord has various ways of arranging windows and synchronizing those windows so that when one window scrolls, other windows scroll as well. The Arrange method enables you to arrange a collection of windows and is the equivalent of selecting Arrange All from the Windows menu. This method takes an optional Object parameter by reference that can be passed a member of the WdArrangeStyle enumeration: wdIcons or wdTiled. Passing wdTiled makes sense only when you have put Word into MDI mode by setting the Application object's ShowWindowsInTaskbar to False. You also have to set the WindowState of each Window object to wdWindowStateMinimize if Arrange is to do anything when passed wdTiled. The CompareSideBySideWith method enables you to synchronize the scrolling of two windows showing two different documents. This method is the equivalent of choosing Compare Side by Side With from the Window menu when you have multiple documents open in Word. The CompareSideBySideWith method takes a Document parameter that is the document you want to compare with the active document. To change the active document before you call this method, you can use the Document object's Activate method. After you have established side-by-side mode, you can control it further by calling the ResetSideBySideWith method, which takes a Document parameter that is the document you want to reset side by side with against the active document. The SyncScrollingSideBySide property tells you whether you are in side-by-side mode and lets you disable the synchronization of scrolling temporarily. The BreakSideBySide method turns side-by-side mode off. Listing 8.17 shows an example of first arranging two document windows and then establishing side-by-side mode. Listing 8.17. A VSTO Customization That Uses the Arrange and CompareSideBySideWith Methods
|