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 a Top and Left parameter in pixels. The Application object's Resize method is used to resize the active window when the WindowState is set to wdWindowStateNormal and takes a Width and Height parameter 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 the foreach keyword in C#, as shown in Listing 8-15.

Listing 8-15. A VSTO Customization That Iterates over the Open Windows

private void ThisDocument_Startup(object sender, EventArgs e)
 // Create 20 windows
 for (int i = 0; i < 20; i++)

 foreach (Word.Window w in Application.Windows)


Accessing a Window in the Collection

To access a Window in the Windows collection, you use a method called get_Item, which returns a Window. The get_Item method 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.

Listing 8-16. A VSTO Customization That Uses get_Item to Get a Window

private void ThisDocument_Startup(object sender, EventArgs e)
 Word.Application app = this.Application;

 // Create some windows
 object stringIndex = app.NewWindow().Caption;

 "There are {0} windows.",

 object index = 1;
 Word.Window w = app.Windows.get_Item(ref index);

 Word.Window w2 = app.Windows.get_Item(ref stringIndex);


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 only makes sense 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 to the currently active document. To change the currently 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 further control it by calling the ResetSideBySideWith method, which takes a Document parameter that is the document you want to reset side by side with against the currently active document. The SyncScrollingSideBySide property tells you whether you are in side-by-side mode and lets you temporarily disable the synchronization of scrolling. 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

private void ThisDocument_Startup(object sender, EventArgs e)
 // Create a second document
 Word.Document doc2 = Application.Documents.Add(
 ref missing, ref missing, ref missing, ref missing);
 Word.Range r1 = this.Range(ref missing, ref missing);
 Word.Range r2 = doc2.Range(ref missing, ref missing);

 // Fill both documents with random text
 Random rand = new Random();
 for (int i = 0; i < 1000; i++)
 string randomNumber = rand.NextDouble().ToString();
 r1.InsertAfter(randomNumber + System.Environment.NewLine);
 r2.InsertAfter(randomNumber + System.Environment.NewLine);

 // Arrange windows
 Application.Windows.Arrange(ref missing);
 MessageBox.Show("Windows are tiled.");

 // Activate this document and synchronize with doc2
 object docObject = doc2;
 Application.Windows.CompareSideBySideWith(ref docObject);
 MessageBox.Show("Windows are in side by side mode.");

Part One. An Introduction to VSTO

An Introduction to Office Programming

Introduction to Office Solutions

Part Two. Office Programming in .NET

Programming Excel

Working with Excel Events

Working with Excel Objects

Programming Word

Working with Word Events

Working with Word Objects

Programming Outlook

Working with Outlook Events

Working with Outlook Objects

Introduction to InfoPath

Part Three. Office Programming in VSTO

The VSTO Programming Model

Using Windows Forms in VSTO

Working with Actions Pane

Working with Smart Tags in VSTO

VSTO Data Programming

Server Data Scenarios

.NET Code Security


Part Four. Advanced Office Programming

Working with XML in Excel

Working with XML in Word

Developing COM Add-Ins for Word and Excel

Creating Outlook Add-Ins with VSTO

Visual Studio Tools for Office(c) Using C# with Excel, Word, Outlook, and InfoPath
Visual Studio Tools for Office(c) Using C# with Excel, Word, Outlook, and InfoPath
ISBN: 321334884
Year: N/A
Pages: 214 © 2008-2020.
If you may any questions please contact us: