Tải bản đầy đủ - 0trang
Figure 22-2. A smart tag in Excel XP
Thus, smart tags are tools that recognize certain types of data within an Office document and
present the user with a list of predefined actions. (At the user-interface level, the smart tag options
dialog can be reached through the AutoCorrect Options menu item under the Tools menu.)
22.2 SmartTagRecognizer Object
The object that is responsible for recognizing a particular type of data as a smart tag is called a
smart tag recognizer. These recognizers take the form of code in a Dynamic Link Library (DLL)
that is loaded into Excel's address space when Excel is first loaded. To illustrate, on my system,
the following code:
Dim str As SmartTagRecognizer
For Each str In Application.SmartTagRecognizers
Debug.Print str.progID & "/" & str.Enabled
produces the output:
indicating that two smart tag recognizer DLLs are loaded in Excel's address space. (A single DLL
may be used to recognize more than one type of smart tag.) We also see from this code that the
SmartTagRecognizers collection holds a SmartTagRecognizer object for each of the currently
available smart tag recognizers.
The SmartTagRecognizer object has a read-write Enabled property used to enable or disable the
recognizer, as well as read-only properties called progID and FullName, which return the name of
the recognizer (the FullName property includes the path).
22.3 SmartTag Object
The Range object has a SmartTags property that returns the collection of all SmartTag objects. Of
course, a SmartTag object represents a smart tag. Let us discuss the more important members of
the SmartTag object, listed next. (The Delete method is self-explanatory.)
The Name property of the SmartTag object returns a fully qualified name for the smart tag. A fully
qualified name consists of a XML namespace URI followed by a number sign (#) and then the tag
name. For instance, referring to the smart tag in Figure 22-2, the code:
Do not confuse the name of the smart tag with the text, such as IBM, of the smart tag. There does
not appear to be a smart tag member that returns the smart tag's text.
The DownloadURL property is an optional URL that is specified by the smart tag's creator. It
provides an address where additional smart tag actions may be available for download.
According to the Microsoft documentation, the read-only XML property of the smart tag object
"Returns a String representing a sample of the XML that would be passed to the action handler."
The term "sample" could certainly use clarification. In any case, for the smart tag in Figure 22-2,
produces the string:IBM
which contains the smart tag's fully qualified name as well as the tag's text (IBM).
22.4 SmartTagAction Object
The SmartTagActions property returns the SmartTagActions collection of SmartTagAction
objects. A SmartTagAction object represents an action that can be taken for a smart tag. As an
example, referring to Figure 22-2, the code:
Dim st As SmartTag
Set st = Application.Range("A1").SmartTags(1)
For i = 1 To st.SmartTagActions.Count
produces the output:
Insert refreshable stock price...
which corresponds to the four actions in Figure 22-2. Incidentally, the code:
Dim st As SmartTag
Dim sta As SmartTagAction
For Each sta In st.SmartTagActions
does not print anything, nor does it produce an error message!
The SmartTagAction object has an Execute method that executes an action. Here is an example
that executes the first action in Figure 22-2:
Dim st As SmartTag
Dim sAction As String
Dim ws As Worksheet
Set ws = Application.ActiveSheet
sAction = "Insert refreshable stock price..."
' Invoke a smart tag for the Microsoft ticker symbol.
Set st = ws.Range("A1").SmartTags( _
22.5 SmartTagOptions Object
The SmartTagOptions object, returned by the SmartTagOptions property of the Workbook object,
has two useful properties. The DisplaySmartTags property takes a value from the following enum:
xlIndicatorAndButton = 0
xlDisplayNone = 1
xlButtonOnly = 2
Thus, we can choose to display nothing, a button only, or a button and indicator for each smart tag.
The EmbedSmartTags property is a Boolean property that determines whether or not smart tags
are saved along with the workbook (although the term "embed" seems to be a rather poor choice
For more information on smart tags and how to create custom smart tags (which as we mentioned
earlier cannot be done from within Excel XP itself), you can check out the smart tag SDK on
Microsoft's web site at:
Part IV: Appendixes
Appendix A. The Shape Object
Now we want to take a brief look at the issue of drawing pictures using VBA code. Since this
subject is not fundamental to Excel VBA programming, we will be very brief, but hopefully this
introduction will give you the necessary background for further study using the VBA help files.
A.1 What Is the Shape Object?
Each Excel sheet (chartsheet or worksheet) and each Excel chart has a drawing layer upon which
we can place drawing objects. A drawing object is represented by a Shape object.
As usual, the Shape objects for a sheet are stored in a Shapes collection. The Chart object and the
Worksheet object both have a Shapes property that returns the collection of all Shape objects
drawn on the chart or worksheet.
There is also a ShapeRange object that holds a collection of selected Shape objects, much as a
Range object can contain a collection of selected cells. The ShapeRange object allows us to set the
properties of a subcollection of all Shape objects.
The Shape-related objects are shown in Figure A-1.
Figure A-1. The Shape-related objects
Every Shape object has an order, called its z-order , that indicates the object's relative position
with respect to an imaginary z-axis that comes directly out of the monitor at right angles, towards
the user, as pictured in Figure A-2.
Figure A-2. Illustrating z-order
The read-only ZOrderPosition property of a Shape object reports the current z-order of the object
which, incidentally, is the same as the object's index within the Shapes collection. Shape objects
with a larger z-order appear on top of objects with a smaller z-order. Hence, the Shape object with
z-order equal to 1 is Shapes(1) and lies at the bottom of the pile!
The ZOrder method sets the z-order of a Shape object relative to other objects. Note that the
method does not set the absolute z-order. The syntax is:
where ZOrderCmd is one of the constants in the following enum (from the Microsoft Office
Thus, the z-order can only be set in the following ways:
Move the object to the front of the z-order.
Move the object to the back of the z-order.
Move the object one forward in the z-order; that is, increase its index by 1.
Move the object one backward in the z-order; that is, decrease its index by 1.
Incidentally, as we have seen, the ChartObject object has a read-only ZOrder property that returns
the z-order of the ChartObject. It also has BringToFront and SendToBack methods for changing
A.3 Creating Shapes
An AutoShape is a Shape object that represents a built-in drawing. To add a new AutoShape
object, we use the AddShape method, whose syntax is:
ShapesObject.AddShape(Type, Left, Top, Width, Height)
The parameter Type is the type of AutoShape to create. It can be any one of the
MsoAutoShapeType constants in Table A-1.
The required parameters Left and Top specify the position (in points as a Single) of the upperleft corner of the bounding box for the AutoShape object, measured relative to the upper-left
corner of the container object (chart, chart sheet, or worksheet).
The Width and Height parameters specify the width and height (in points as a Single) of the
bounding box for the AutoShape. Note that the type of a Shape object can be changed by setting
the AutoShapeType property.
Table A-1. MsoAutoShapeType Constants (and Values)
The short program in Example A-1 will display each AutoShape, along with its AutoShapeType,
for 0.5 seconds. (It should be run on a blank worksheet. You can interrupt this program at any
time by striking Ctrl-Break.) The Delay subroutine that it calls is shown in Example A-2.
Example A-1. Displaying Each AutoShape
Dim sh As Shape
Dim i As Integer
Set sh = ActiveSheet.Shapes.AddShape(1, 100, 100, 72, 72)
For i = 1 To 138
sh.AutoShapeType = i
sh.Visible = True
ActiveSheet.Cells(1, 1).Value = sh.AutoShapeType
Example A-2. The Delay Procedure
Public Sub Delay(rTime As Single)
'Delay rTime seconds (min=.01, max=300)
Dim OldTime As Variant
If rTime < 0.01 Or rTime > 300 Then rTime = 1
OldTime = Timer
Loop Until Timer - OldTime >= rTime