HOWTO: Call a VB ActiveX Server from a VBA Application

ID: Q185731


The information in this article applies to:
  • Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, version 5.0
  • Microsoft Visual Basic for Applications version 5.0


SUMMARY

ActiveX Components (formerly known as ActiveX or Automation "Servers") created in Visual Basic can be used with applications that support the Visual Basic for Applications (VBA) programming language. This article shows you how to create a simple Visual Basic ActiveX Component, and how to use that component in a VBA-based application.


MORE INFORMATION

The first step is to create the ActiveX Component. If you are not familiar with this process, here are several tips that you may find useful.

  • It is considered good practice to test your ActiveX Component in Visual Basic before you try it in your VBA application. If it does not work in Visual Basic, it probably won't work in VBA.


  • Additionally, it is not required that you compile (make .exe or .dll) your component. You can reference a Visual Basic component that is running in the Visual Basic design environment. This approach allows you to test both the server and client sides of your application before compiling the finished product.


Create a Simple ActiveX Component

  1. Start Microsoft Visual Basic.


  2. On the New Project window, select ActiveX DLL or ActiveX EXE.


  3. You should see a code window titled Project1-Class1.


  4. In the properties window for the Class, change the name property to MyClass.


  5. On the Project menu, click Project1 Properties and change the project name to MyComponent.


  6. Insert the following code in the General section of MyClass:
    
          Option Explicit
    
          Public Function SquareIt(lngNumber As Long)
             SquareIt = lngNumber ^ 2
          End Function 


  7. On the Run menu, click Start with Full Compile.


Creating the Client Application

  1. Start a VBA application (such as Word, Access, Excel, and so on).


  2. Open a module window in the VBA application. This can be an Event Procedure, Function, or Sub.


  3. On the Tools menu, click References. If the references selection in the application you are using is not on this menu, search online help for the keyword references.


  4. Select MyComponent from the list. In this case, the file name in the bottom portion of the references window resembles c:\temp\vb#.tmp. (When using a compiled component, the path would reference the compiled .exe or .dll file name.)


  5. Insert the following code into the Sub, Function, or Event Procedure you are going to use (a button click event procedure would be a good choice here):
    
          'begin procedure
    
          'create a object reference to the component
          Dim obj As MyComponent.MyClass
          Dim lngArgument As Long
          Dim lngResult As Long
    
          'create an instance of the object
          Set obj = New MyClass
          lngArgument = 2
    
          'call the objects SquareIt method
          lngResult = obj.SquareIt(lngArgument)
    
          MsgBox "The Square of " & lngArgument & _
                 " is " & lngResult
    
          'end procedure 


  6. Call the Sub or Function, or trigger the event you have chosen to test your component (for example, click the command button).


If you find it necessary to make changes to your component and/or you receive the error "Error 429. ActiveX component can't create object", the reference created in step 3 must be reestablished. This is also the case if you are using a compiled component, because a new ClassID is created each time the component project is run or compiled, invalidating the previous reference. Components compiled with "Binary Compatibility" are an exception. That subject is beyond the scope of this article--please see the Visual Basic documentation for more information.


REFERENCES

For more information about creating ActiveX components with Visual Basic, refer to the chapter "Creating ActiveX Components" in the Visual Basic "Component Tools Guide" (Professional and Enterprise Editions only).

Additional query words: createobject kbAutomation kbInterop kbDSupport kbDSD

Keywords :
Version : WINDOWS:5.0
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: November 9, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.