Microsoft KB Archive/185731

= How To Call a VB ActiveX Server from a VBA Application =

Article ID: 185731

Article Last Modified on 6/29/2004

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic for Applications 5.0

-



This article was previously published under Q185731



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
 Start Microsoft Visual Basic. On the New Project window, select ActiveX DLL or ActiveX EXE. You should see a code window titled Project1-Class1. In the properties window for the Class, change the name property to MyClass. On the Project menu, click Project1 Properties and change the project name to MyComponent.  Insert the following code in the General section of MyClass: Option Explicit

Public Function SquareIt(lngNumber As Long) SquareIt = lngNumber ^ 2 End Function  On the Run menu, click Start with Full Compile.</li></ol>

Creating the Client Application
<ol> Start a VBA application (such as Word, Access, Excel, and so on).</li> Open a module window in the VBA application. This can be an Event Procedure, Function, or Sub.</li> 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 .</li> 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.)</li>  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 </li> Call the Sub or Function, or trigger the event you have chosen to test your component (for example, click the command button).</li></ol>

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.

<div class="references_section">