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. Table 5.12. Window Properties That Control the Display of a WindowProperty Name | Type | What It Does |
---|
DisplayGridline | Boolean | If set to False, Excel won't display gridlines around cells. | DisplayHeadings | Boolean | If set to False, Excel won't display the row and column headers. | DisplayHorizontalScrollBar | Boolean | If set to False, Excel won't display the horizontal scroll bar. | DisplayVerticalScrollBar | Boolean | If set to False, Excel won't display the vertical scroll bar. | DisplayWorkbookTabs | Boolean | If set to False, Excel won't display the tabs to allow the user to switch to another worksheet. | EnableResize | Boolean | If set to False, Excel won't let the user resize the window when WindowState is set to xlNormal. | GridlineColor | Integer | 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 | Integer | Sets the left column that the window should scroll to. | ScrollRow | Integer | 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 | Boolean | 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 Public Class ThisWorkbook Private Sub ThisWorkbook_Startup(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Startup Dim win As Excel.Window = Me.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 End Sub End Class | |