Working with Windows


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 Windows

The 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 Windows

The 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

Public Class ThisDocument   Private Sub ThisDocument_Startup(ByVal sender As Object, _     ByVal e As System.EventArgs) Handles Me.Startup     ' Create 20 windows     Dim i As Integer     For i = 0 To 20       Application.NewWindow()     Next     For Each w As Word.Window In Application.Windows       MsgBox(w.Caption)     Next   End Sub End Class 


Accessing a Window in the Collection

To 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

Public Class ThisDocument   Private Sub ThisDocument_Startup(ByVal sender As Object, _     ByVal e As System.EventArgs) Handles Me.Startup     Dim app As Word.Application = Me.Application     ' Create some windows     app.NewWindow()     app.NewWindow()     Dim stringIndex As String = app.NewWindow().Caption     MsgBox(String.Format("There are {0} windows.", _       app.Windows.Count))     Dim w As Word.Window = app.Windows.Item(1)     MsgBox(w.Caption)     Dim w2 As Word.Window = app.Windows(stringIndex)     MsgBox(w2.Caption)   End Sub End Class 


Arranging Windows

Word 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

Public Class ThisDocument   Private Sub ThisDocument_Startup(ByVal sender As Object, _     ByVal e As System.EventArgs) Handles Me.Startup     ' Create a second document     Dim doc2 As Word.Document = Application.Documents.Add()     Dim r1 As Word.Range = Me.Range     Dim r2 As Word.Range = doc2.Range     ' Fill both documents with random text     Dim rand As New Random()     Dim i As Integer     For i = 0 To 1000       Dim randomNumber As String = rand.NextDouble().ToString()       r1.InsertAfter(randomNumber & vbCrLf)       r2.InsertAfter(randomNumber & vbCrLf)     Next     ' Arrange windows     Application.Windows.Arrange()     MsgBox("Windows are tiled.")     ' Activate this document and synchronize with doc2     Me.Activate()     Application.Windows.CompareSideBySideWith(doc2)     MsgBox("Windows are in side by side mode.")   End Sub End Class 





Visual Studio Tools for Office(c) Using Visual Basic 2005 with Excel, Word, Outlook, and InfoPath
Visual Studio Tools for Office: Using Visual Basic 2005 with Excel, Word, Outlook, and InfoPath
ISBN: 0321411757
EAN: 2147483647
Year: N/A
Pages: 221

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net