Share via


Adding ActiveX Controls (Visual Basic .NET 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.

  1. 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"
    
  2. 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
    
  3. 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
                SmartDocXmlTypeName = cACTIVEX
    

    In the SmartDocXMLTypeCaption property, insert the following code.

            Case 10
                SmartDocXmlTypeCaption = _
                    "ActiveX Control: Date Control"
    

    In the ControlCount property, insert the following code.

            Case cACTIVEX
                ControlCount = 1
    

    In the ControlID property, insert the following code.

            Case cACTIVEX
                ControlID = ControlIndex + 900
    

    In the ControlTypeFromID property, insert the following code.

            Case 901
                ControlTypeFromID = C_TYPE.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
                ControlCaptionFromID = _
                    "{8E27C92B-1264-101C-8A2F-040224009C02}"
    
  4. In PopulateActiveXProps, insert the following code.

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

  5. 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 Add Reference dialog box rather than add the control to the Toolbox.

    How?

    1. On the Project menu, select Add Reference.
    2. Click Browse.
    3. Navigate to the folder where Microsoft Office 2003 is installed on your computer. On most systems, this is C:\Program Files\Microsoft Office\OFFICE11\.
    4. Select the file named MSCAL.OCX.
    5. Click Open.
    6. Click OK.

    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. In addition, you need a global variable to access the host application, in this case Microsoft Word. Insert the following code in the general declarations section of your code.

    Private WithEvents objCal As MSACAL.Calendar
    Private objApp As Microsoft.Office.Interop.Word.Application
    

    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
                   ControlNameFromID = "Calendar"
                Case Else
                   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 first need to access the activex element and then the smart tag action for the ActiveX control. Insert the following code into the OnPaneUpdateComplete method.

            Dim objDoc As Microsoft.Office.Interop.Word.Document
            Dim objCalendar As Microsoft.Office.Interop.Word.SmartTagAction
            Dim objSel As Microsoft.Office.Interop.Word.Selection
    
           objDoc = Document
           objSel = objDoc.ActiveWindow.Selection
    
    
            If objSel.XMLParentNode.BaseName = "activex" Then
                objCalendar = objSel.XMLParentNode.SmartTag.SmartTagActions("Calendar")
                If objCalendar.PresentInPane Then
                    objCal = objCalendar.ActiveXControl
                End If
            End If
    

    Note  The preceding code uses the Selection object of the Word Visual Basic for Applications (VBA) object model to access the XML element at the insertion point. 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 the preceding objects or methods, see the VBA Help for the host application.

    To provide access to the active application from within the Click event for the ActiveX control, insert the following global object variable assignment at the end of the SmartDocInitialize method.

    objApp = objDoc.Application
    

    Finally, add the Click event for the ActiveX control. You can do this by selecting the object variable from the list of objects. Then select Click from the list of events. Insert the code into the Click event subroutine, so that it looks like the following code.

       Private Sub objCal_Click() Handles objCal.Click
          objApp.ActiveWindow.Selection.Range.Text = objCal.Value
       End Sub
    

    Note  For more information about working with ActiveX controls, see Using ActiveX Controls.

  6. Recompile your SimpleSample smart document DLL, and copy it to the deployment location that you specified earlier.

  7. Because the Microsoft Calendar Control is a COM component and no primary interop assembly exists, Microsoft Visual Studio creates an interop assembly for you. You need to follow these steps to make sure your updated SimpleSample smart document functions properly:

    • Copy that assembly to the deployment location.

    • Add the following to the XML expansion pack manifest file (ManagedManifest.xml) that you created earlier. Within the SD:solution element for the SimpleSampleVB7.clsActions solution action handler, directly under the closing element for the current SD:file element, paste the following text:

      <SD:file>
          <SD:type>other</SD:type>
          <SD:version>1.0</SD:version>
          <SD:filePath><INTEROP FILE NAME AND PATH GOES HERE></SD:filePath>
       </SD:file>
      
    • Replace <INTEROP FILE NAME AND PATH GOES HERE> in the preceding text with the correct path where you are deploying the smart document and the interop assembly file name (for example, C:\SimpleSample\Interop.MSACAL.dll).

  8. When you reopen your SimpleSample smart document, delete the SimpleSample XML expansion pack, and then re-add it to the document.

Next  Adding Labels, Separator Lines, and Hyperlinks