Adding ActiveX Controls (Visual Basic 6.0 Tutorial) [Office 2003 SDK Documentation]
Previous Adding Document Fragments
The following steps show you how to add an ActiveX control to the SimpleSample smart document.
The first thing you need to do is add a constant for the activex element in the SimpleSample schema. Insert the following code into the general declarations section of your code module, below the existing constants.
Const cACTIVEX As String = cNAMESPACE & "#activex"
Next, you need to add 1 to the cTYPES constant. Remove the existing cTYPES constant, and enter the following code or change your code to match.
Const cTYPES As Integer = 10
Now you are ready to modify the existing code to insert the ActiveX control. The subroutines you need to modify are SmartDocXMLTypeName, SmartDocXMLTypeCaption, ControlCount, ControlID, ControlTypeFromID, and ControlCaptionFromID.
In the SmartDocXMLTypeName property, insert the following code.
Case 10 ISmartDocument_SmartDocXmlTypeName = cACTIVEX
In the SmartDocXMLTypeCaption property, insert the following code.
Case 10 ISmartDocument_SmartDocXmlTypeCaption = _ "ActiveX Control: Date Control"
In the ControlCount property, insert the following code.
Case cACTIVEX ISmartDocument_ControlCount = 1
In the ControlID property, insert the following code.
Case cACTIVEX ISmartDocument_ControlID = ControlIndex + 900
In the ControlTypeFromID property, insert the following code.
Case 901 ISmartDocument_ControlTypeFromID = C_TYPE_ACTIVEX
For ActiveX controls, the ControlCaptionFromID property provides the globally unique identifier (GUID) for the ActiveX control. In this case, you add the GUID for the calendar control. (The GUID for an installed ActiveX control is stored in the registry. For help locating the GUID, see Locating a GUID.) In the ControlCaptionFromID property subroutine, insert the following code.
Case 901 ISmartDocument_ControlCaptionFromID = _ "{8E27C92B-1264-101C-8A2F-040224009C02}"
In the PopulateActiveXProps property, insert the following code. This specifies that the control will be 200 pixels high and 250 pixels wide.
Select Case ControlID Case 901 Props.Write Key:="W", value:="250" Props.Write Key:="H", value:="200" End Select
Note Use the Props parameter to define the display properties for the ActiveX control. Use the ActiveXPropBag parameter to define any specific ActiveX properties. These two parameters provide access to the ISmartDocProperties interface. For more information about setting ActiveX control properties, as well as a list of display properties, see the ISmartDocProperties interface.
To allow your smart document to return values from and handle events for the ActiveX control, you need to add a reference to the control. It's important that you add a reference from the References dialog box rather than add the control to the Toolbox.
How?
- On the Project menu, select References.
- Click Browse.
- For the Files of type option, select ActiveX Controls (*.OCX).
- Navigate to the folder where Microsoft Office 2003 is installed on your computer. On most systems, this is C:\Program Files\Microsoft Office\OFFICE11\.
- Select the file named MSCAL.OCX.
- Click Open.
- Click OK.
Important After you add this control and close the References dialog box, the control no longer appears in the list of references in the References dialog box. However, if you attempt to add it again, you get an error message that the name conflicts with an existing module, project, or object library.
To provide access to the ActiveX control, you need a global object variable. You declare this object variable by using the WithEvents keyword so that you can capture events for the control. Insert the following code in the general declarations section of your code.
Private WithEvents objCal As Calendar Private strApp As String
In the SmartDocInitialize method, insert the following code:
strApp = Document.Application.Name
To provide easy access to the control, you can give it a friendly name. Insert the following code into the ControlNameFromID property. You use this friendly name later in the OnPaneUpdateComplete method.
Select Case ControlID Case 901 ISmartDocument_ControlNameFromID = "Calendar" Case Else ISmartDocument_ControlNameFromID = cNAMESPACE & ControlID End Select
Note Although in many cases, the functionality of the controls is built into the ISmartDocument interface, and you may not need to access special functionality for a control, in the case of ActiveX controls, it is a good practice to assign friendly names, so that later you can access them in code without doing any major reworking of the code.
Then, you need to connect the global object variable to the instance of the ActiveX control in the task pane. To do this, you need to access first the activex element and then the smart tag action for the ActiveX control. Insert the following code into the OnPaneUpdateComplete method.
Dim objDoc As Word.Document Dim objSel As Word.Selection Dim objWordCal As Word.SmartTagAction Dim objXlCal As Excel.SmartTagAction If Document.Application.Name = "Microsoft Word" Then Set objDoc = Document Set objSel = objDoc.ActiveWindow.Selection If objSel.XMLParentNode = "activex" Then Set objWordCal = objSel.XMLParentNode.SmartTag.SmartTagActions("Calendar") If objWordCal.PresentInPane Then Set objCal = objWordCal.ActiveXControl End If End If Else Set objXlCal = Document.ActiveSheet.SmartTags(cACTIVEX).SmartTagActions("Calendar") If objXlCal.PresentInPane Then Set objCal = objXlCal.ActiveXControl End If End If
Note The code above uses the Selection object of the Word Visual Basic for Applications (VBA) object model to access the XML element at the insertion point position. However, you could also pass XPATH queries into the SelectNodes and SelectSingleNode methods of the Word object model. For more information about using any of these objects or methods, see the VBA Help for the host application.
Finally, add the Click event for the ActiveX control. You can do this by selecting the object variable from the list of objects. The Click event is the default event, so the Click event subroutine is added to your code. Insert the code into the Click event subroutine so that it looks like the following code.
Dim objWd As Word.Application If InStr(1, strApp, "Word") > 0 Then Set objWd = Word.Application objWd.ActiveWindow.Selection.Range.Text = objCal.value Set objWd = Nothing ElseIf InStr(1, strApp, "Excel") > 0 Then MsgBox objCal.value End If
Note For more information about working with ActiveX controls, see Using ActiveX Controls.
Recompile your SimpleSample smart document DLL, and copy it to the deployment location that you specified earlier. When you reopen your SimpleSample smart document, delete the SimpleSample XML expansion pack, and then re-add it to the document.