Tải bản đầy đủ - 0 (trang)
Figure 22-2. A smart tag in Excel XP

Figure 22-2. A smart tag in Excel XP

Tải bản đầy đủ - 0trang

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

Next



produces the output:

FName.SmartTag/True

MOFL.SmartTag/True



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.)













Delete method

Name property

DownloadURL property

XML property

SmartTagActions property



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:

Debug.Print st.Name



prints:



408



urn:schemas-microsoft-com:office:smarttags#stockticker



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,

the code:

Debug.Print st.XML



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

Debug.Print st.SmartTagActions(i).Name

Next



produces the output:

Insert refreshable stock price...

LatestQuoteData

CompanyReportData

RecentNews



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

Debug.Print sta.Name

Next



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:



409



Sub ExecuteASmartTag()

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( _

"urn:schemas-microsoft-com:office:smarttags#stockticker")

st.SmartTagActions(sAction).Execute

End Sub



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:

Enum XlSmartTagDisplayMode

xlIndicatorAndButton = 0

xlDisplayNone = 1

xlButtonOnly = 2

End Enum



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

of terminology).

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:

http://msdn.microsoft.com/downloads/default.asp?URL=/code/sample.asp?url=/MSDNFILES/027/001/652/msdncompositedoc.xml.



410



Part IV: Appendixes

Appendix A

Appendix B

Appendix C

Appendix D

Appendix E

Appendix F

Appendix G



411



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



A.2 Z-Order



412



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:

ShapeObject.ZOrder(ZOrderCmd)



where ZOrderCmd is one of the constants in the following enum (from the Microsoft Office

object model):

Enum MsoZOrderCmd

msoBringToFront

msoSendToBack =

msoBringForward

msoSendBackward

End Enum



= 0

1

= 2

= 3



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

the z-order.



A.3 Creating Shapes



413



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)

msoShapeLineCallout2BorderandAccentBar

(122)

msoShapeLineCallout2NoBorder (118)

msoShapeLineCallout3 (111)

msoShapeLineCallout3AccentBar (115)

msoShapeLineCallout3BorderandAccentBar

(123)

msoShapeLineCallout3NoBorder (119)



msoShape16pointStar (94)



msoShapeFlowchartCard (75)



msoShape24pointStar (95)

msoShape32pointStar (96)

msoShape4pointStar (91)



msoShapeFlowchartCollate (79)

msoShapeFlowchartConnector (73)

msoShapeFlowchartData (64)



msoShape5pointStar (92)



msoShapeFlowchartDecision (63)



msoShape8pointStar (93)

msoShapeActionButtonBackorPrevious

(129)

msoShapeActionButtonBeginning

(131)



msoShapeFlowchartDelay (84)

msoShapeFlowchartDirectAccessStorage

(87)

msoShapeFlowchartDisplay (88)



msoShapeLineCallout4AccentBar (116)



msoShapeActionButtonCustom (125)



msoShapeFlowchartDocument (67)



msoShapeLineCallout4BorderandAccentBar

(124)



msoShapeFlowchartExtract (81)



msoShapeLineCallout4NoBorder (120)



msoShapeFlowchartInternalStorage (66)



msoShapeMixed (-2)



msoShapeFlowchartMagneticDisk (86)



msoShapeMoon (24)



msoShapeFlowchartManualInput (71)

msoShapeFlowchartManualOperation (72)



msoShapeNoSymbol (19)

msoShapeNotchedRightArrow (50)



msoShapeFlowchartMerge (82)



msoShapeNotPrimitive (138)



msoShapeFlowchartMultidocument (68)

msoShapeFlowchartOffpageConnector (74)

msoShapeFlowchartOr (78)

msoShapeFlowchartPredefinedProcess (65)

msoShapeFlowchartPreparation (70)

msoShapeFlowchartProcess (61)

msoShapeFlowchartPunchedTape (76)

msoShapeFlowchartSequentialAccessStorage

(85)

msoShapeFlowchartSort (80)

msoShapeFlowchartStoredData (83)

msoShapeFlowchartSummingJunction (77)

msoShapeFlowchartTerminator (69)

msoShapeFoldedCorner (16)



msoShapeOctagon (6)

msoShapeOval (9)

msoShapeOvalCallout (107)

msoShapeParallelogram (2)

msoShapePentagon (51)

msoShapePlaque (28)

msoShapeQuadArrow (39)



msoShapeActionButtonDocument

(134)

msoShapeActionButtonEnd (132)

msoShapeActionButtonForwardorNext

(130)

msoShapeActionButtonHelp (127)

msoShapeActionButtonHome (126)

msoShapeActionButtonInformation

(128)

msoShapeActionButtonMovie (136)

msoShapeActionButtonReturn (133)

msoShapeActionButtonSound (135)

msoShapeArc (25)

msoShapeBalloon (137)

msoShapeBentArrow (41)

msoShapeBentUpArrow (44)

msoShapeBevel (15)

msoShapeBlockArc (20)

msoShapeCan (13)

msoShapeChevron (52)

msoShapeCircularArrow (60)

msoShapeCloudCallout (108)



414



msoShapeLineCallout4 (112)



msoShapeQuadArrowCallout (59)

msoShapeRectangle (1)

msoShapeRectangularCallout (105)

msoShapeRegularPentagon (12)

msoShapeRightArrow (33)

msoShapeRightArrowCallout (53)



msoShapeCross (11)

msoShapeCube (14)

msoShapeCurvedDownArrow (48)

msoShapeCurvedDownRibbon (100)



msoShapeHeart (21)

msoShapeHexagon (10)

msoShapeHorizontalScroll (102)

msoShapeIsoscelesTriangle (7)



msoShapeCurvedLeftArrow (46)



msoShapeLeftArrow (34)



msoShapeCurvedRightArrow (45)

msoShapeCurvedUpArrow (47)

msoShapeCurvedUpRibbon (99)

msoShapeDiamond (4)

msoShapeDonut (18)

msoShapeDoubleBrace (27)

msoShapeDoubleBracket (26)

msoShapeDoubleWave (104)

msoShapeDownArrow (36)

msoShapeDownArrowCallout (56)



msoShapeLeftArrowCallout (54)

msoShapeLeftBrace (31)

msoShapeLeftBracket (29)

msoShapeLeftRightArrow (37)

msoShapeLeftRightArrowCallout (57)

msoShapeLeftRightUpArrow (40)

msoShapeLeftUpArrow (43)

msoShapeLightningBolt (22)

msoShapeLineCallout1 (109)

msoShapeLineCallout1AccentBar (113)

msoShapeLineCallout1BorderandAccentBar

msoShapeVerticalScroll (101)

(121)

msoShapeLineCallout1NoBorder (117)

msoShapeWave (103)

msoShapeLineCallout2 (110)



msoShapeDownRibbon (98)

msoShapeExplosion1 (89)

msoShapeExplosion2 (90)

msoShapeFlowchartAlternateProcess

(62)



msoShapeRightBrace (32)

msoShapeRightBracket (30)

msoShapeRightTriangle (8)

msoShapeRoundedRectangle (5)

msoShapeRoundedRectangularCallout

(106)

msoShapeSmileyFace (17)

msoShapeStripedRightArrow (49)

msoShapeSun (23)

msoShapeTrapezoid (3)

msoShapeUpArrow (35)

msoShapeUpArrowCallout (55)

msoShapeUpDownArrow (38)

msoShapeUpDownArrowCallout (58)

msoShapeUpRibbon (97)

msoShapeUTurnArrow (42)



msoShapeLineCallout2AccentBar (114)



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

Sub DisplayAutoShapes()

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

Delay 0.5

Next i

End Sub



Example A-2. The Delay Procedure

Public Sub Delay(rTime As Single)

'Delay rTime seconds (min=.01, max=300)

Dim OldTime As Variant

'Safty net

If rTime < 0.01 Or rTime > 300 Then rTime = 1

OldTime = Timer

Do

DoEvents

Loop Until Timer - OldTime >= rTime

End Sub



415



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Figure 22-2. A smart tag in Excel XP

Tải bản đầy đủ ngay(0 tr)

×
x