Working with the Window Object
The Window object represents an Excel window. You can use the Window object to position a window associated with a workbook. You can also use the Window object to set display settings for a workbook such as whether to display gridlines and headings.
Positioning a Window
The Window object lets you position and change the way Excel displays a workbook within a window. Window has a WindowState property of type XlWindowState that can be used to set the window to xlMaximized, xlMinimized, or xlNormal.
When the WindowState is set to xlNormal, you can position the window using the Left, Top, Width, and Height properties. These properties are double values that represent points, not screen pixels. You can use the Window's PointsToScreenPixelsX and PointsToScreenPixelsY methods to convert points to pixels.
Display Settings Associated with a Window
A number of additional properties allow you to control the display of a window. Table 5-12 lists some of the most commonly used ones.
Property Name |
Type |
What It Does |
---|---|---|
DisplayGridline |
bool |
If set to false, Excel won't display gridlines around cells. |
DisplayHeadings |
bool |
If set to false, Excel won't display the row and column headers. |
Display HorizontalScrollBar |
bool |
If set to false, Excel won't display the horizontal scroll bar. |
Display VerticalScrollBar |
bool |
If set to false, Excel won't display the vertical scroll bar. |
Display WorkbookTabs |
bool |
If set to false, Excel won't display the tabs to allow the user to switch to another worksheet. |
EnableResize |
bool |
If set to false, Excel won't let the user resize the window when WindowState is set to xlNormal. |
GridlineColor |
int |
Set to the color of the gridlines. Add a reference to your project to System.Drawing.dll and use the System.Drawing.ColorTranslator.ToOle method to generate a color Excel understands from a .NET color. |
ScrollColumn |
int |
Sets the left column that the window should scroll to. |
ScrollRow |
int |
Sets the top row that the window should scroll to. |
SplitColumn |
double |
Sets the column number where the window will be split into vertical panes. |
SplitRow |
double |
Sets the row number where the window will be split into horizontal panes. |
Visible |
bool |
Sets whether the window is visible. |
Zoom |
object |
Zooms the window; set to 100 to zoom to 100%, 200 to zoom to 200%, and so on. |
Listing 5-21 shows an example of using many of these properties. Note that we add a reference to System.Drawing.dll so that we can use the ColorTranslator object to set the GridlineColor property. The ColorTranslator object provides a method called ToOle, which takes a System.Drawing color and converts it to an Ole color formatthe kind of color format that Office methods and properties that take colors expect.
Listing 5-21. A VSTO Customization That Controls the Display Options for a Window
private void ThisWorkbook_Startup(object sender, EventArgs e) { Excel.Window win = this.NewWindow(); win.WindowState = Excel.XlWindowState.xlNormal; win.Width = 200; win.Height = 200; win.Top = 8; win.Left = 8; win.DisplayGridlines = true; win.DisplayHeadings = false; win.DisplayHorizontalScrollBar = false; win.DisplayVerticalScrollBar = false; win.DisplayWorkbookTabs = false; win.EnableResize = false; win.GridlineColor = System.Drawing.ColorTranslator. ToOle(System.Drawing.Color.Blue); win.ScrollColumn = 10; win.ScrollRow = 20; win.Visible = true; win.Zoom = 150; }