Visual Studio Tools for Office: Using Visual Basic 2005 with Excel, Word, Outlook, and InfoPath

After the control has been added to the document or spreadsheet, the experience of using the control on the design surface should be very close to that of working with a standard Windows Form. There are some differences, however. The biggest difference appears when you click a Windows Forms control in the document and use the categorized view in the Properties window. If you compare a Windows.Forms.Controls.Button with a Microsoft.Office.Tools.Excel.Controls.Button, you will see the extra properties merged in from the OLEObject. These properties are listed in the Misc category to denote that these properties are coming from OLEObject.

Excel Control Properties That Are Added from OLEObject

The OLEObject merge done for controls in the Microsoft.Office.Tools .Excel.Controls namespace adds several properties to VSTO extended controls that are not in the base Windows.Forms controls. Table 14.1 shows the most important properties that are added for controls in Excel.

Table 14.1. Additional Excel Control Properties

Name

Type

Access

Description

BottomRightCell

Excel.Range

Read-only

The Range object that represents the cell that lies under the bottom-right corner of the control.

Enabled

Boolean

Read-write

Determines whether the control is enabled. If you set this to False, the control will appear grayed-out in Excel. This enables you to control whether the control will accept input at runtime.

Height

Double

Read-write

The height, in points, of the control.

Left

Double

Read-write

The distance, in points, from the left edge of the control to the left edge of column A.

Placement

Object

Read-write

Determines how the control will be placed. This can be one of three values: xlFreeFloating (equivalent to the Do Not Move or Size with Cell setting in the Format Control dialog box), xlMove (equivalent to the Move but Do Not Size with Cell setting in the Format Control dialog box), or xlMoveAndSize (equivalent to the Move and Size with Cell setting in the Format Control dialog box).

PrintObject

Boolean

Read-write

Determines whether the control will print when the worksheet is printed. This can prove very useful if the control you are using is something like a button that should not be part of the final printed document.

Shadow

Boolean

Read-write

Determines whether Excel should provide a drop shadow for the control. When set to true, Excel will provide a simple black drop shadow around the control.

TopLeftCell

Excel.Range

Read-only

The Range object that represents the cell that lies under the top-left corner of the control.

Top

Double

Read-write

The distance in points from the top edge of the control to the top edge of row 1.

Visible

Boolean

Read-write

Determines whether to hide the control at runtime.

Width

Double

Read-write

The width in points of the control.

Word Control Properties Added from OLEControl

The OLEControl merge done for controls in the Microsoft.Office.Tools .Word.Controls namespace adds several properties to VSTO extended controls that are not in the base Windows.Forms controls. Table 14.2 shows the most important properties that are added for controls in Word.

Table 14.2. Additional Word Control Properties

Name

Type

Access

Description

Bottom

Single

Read-only

The distance in points from the top edge of the first paragraph on the page to the bottom of the control

Height

Single

Read-write

The height in points of the control

InlineShape

InlineShape

Read

Returns the InlineShape object in the Word object model corresponding to the control; returns Nothing if the control is not inline

Shape

Shape

Read

Returns the Shape object in the Word object model corresponding to the control; returns Nothing if the control is inline

Left

Single

Read-write

The distance in points from the left edge of the control to the left edge of the first paragraph on the page

Name

String

Read-write

The name of the control

Right

Single

Read-only

The distance in points from the right edge of the control to the left edge of the first paragraph on the page

Top

Single

Read-write

The distance in points from the top edge of the control to the top edge of the first paragraph on the page

Width

Single

Read-write

The width in points of the control

Many of the properties for controls running in Word are dependent on the wrapping style of the control. If the control is inline with text, the Left, Bottom, Right, Top, and Width properties will throw an exception. Why? Word represents ActiveX controls as either Shapes or InlineShapes, depending on how the control is positioned on the document, and the positioning properties are available only on Shapes that are controls whose wrapping style is Behind Text or In Front of Text.

Word controls also have an InlineShape and Shape property that provide you access to the InlineShape or Shape object in the Word object model corresponding to the control.

Категории