Share via


Adding ActiveX Controls (C# 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 String cACTIVEX = 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 Int32 cTYPES = 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 subroutine, insert the following code.

                case 10:
                   strTypeName = cACTIVEX;
                   break;
    

    In the SmartDocXMLTypeCaption property subroutine, insert the following code.

                case 10:
                   strTypeCaption = "ActiveX Control: Date Control";
                   break;
    

    In the ControlCount property subroutine, insert the following code.

                case cACTIVEX:
                   intNumberOfControls = 1;
                   break;
    

    In the ControlID property subroutine, insert the following code.

                case cACTIVEX:
                   intControlID = ControlIndex + 900;
                   break;
    

    In the ControlTypeFromID property subroutine, insert the following code.

                case 901:
                   type = C_TYPE.C_TYPE_ACTIVEX;
                   break;
    

    For ActiveX controls, the ControlCaptionFromID property provides the globally unique identifier (GUID) for the ActiveX control. In this case, you will 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.) Therefore, in the ControlCaptionFromID property subroutine, insert the following code.

                case 901:
                   strControlCaption = 
                      "{8E27C92B-1264-101C-8A2F-040224009C02}";
                   break;
    
  4. In the PopulateActiveXProps subroutine, insert the following code.

             switch (ControlID)
             {
                case 901:
                   Props.Write("W", "250");
                   Props.Write("H", "200");
                   break;
    
                default:
                   break;
             }
    

    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 the ActiveX control property 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. Select Add Reference from the Project menu.
    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 MSACAL.Calendar objCal;
    private Microsoft.Office.Interop.Word.Application objApp;
    

    To provide easy access to the control, you can give it a friendly name. Replace the existing code in the ControlNameFromID property with the following code. You will use this friendly name later in the OnPaneUpdateComplete method.

             String strControlName = "";
    
             switch (ControlID)
             {
                case 901:
                   strControlName = "Calendar";
                   break;
    
                default:
                   strControlName = cNAMESPACE + ControlID;
                   break;
             }
    
             return strControlName;
    

    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 active element and then the smart tag action for the ActiveX control. Insert the following code into the OnPaneUpdateComplete method.

             Microsoft.Office.Interop.Word.Document objDoc;
             Microsoft.Office.Interop.Word.SmartTagAction objCalendar;
             Microsoft.Office.Interop.Word.Selection objSel;
    
             objDoc = (Microsoft.Office.Interop.Word.Document)Document;
             objSel = objDoc.ActiveWindow.Selection;
    
             foreach (Microsoft.Office.Interop.Word.SmartTagAction objAction 
                         in objSel.XMLParentNode.SmartTag.SmartTagActions)
             {
                if (objAction.Name=="Calendar")
                {
                   objCalendar = objAction;
    
                   if (objCalendar.PresentInPane == true)
                   {
                      objCal = (MSACAL.Calendar)objCalendar.ActiveXControl;
                      objCal.Click += new MSACAL.DCalendarEvents_ClickEventHandler(CalendarClick);
                      break;
                   }
                }
             }
    

    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. 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 above 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 subroutine for the ActiveX control. In the ClickEventHandler that you created in the OnPaneUpdateComplete method, you named this subroutine "CalendarClick." Insert the following code into your code module to create a subroutine for the Click event to call.

          private void CalendarClick()
          {
             objApp.ActiveWindow.Selection.Range.Text = objCal.Value.ToString();
          }
    

    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 do the following 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 SimpleSampleCS.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 above 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