Sei sulla pagina 1di 3

How To Call a VB ActiveX Server from a VBA Application

Article ID: 185731 - View products that this article applies to.

Retired KB Content Disclaimer This article was previously published under Q185731 Expand all | Collapse all

On This Page
o o SUMMARY MORE INFORMATION Create a Simple ActiveX Component Creating the Client Application REFERENCES Properties Give Feedback

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. Back to the top | Give Feedback

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. 2. 3. 4. 5. 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.

6. Insert the following code in the General section of MyClass: 7. Option Explicit 8. 9. Public Function SquareIt(lngNumber As Long) 10. SquareIt = lngNumber ^ 2 End Function 11. On the Run menu, click Start with Full Compile.

Creating the Client Application


1. 2. Start a VBA application (such as Word, Access, Excel, and so on). 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): 6. 'begin procedure 7. 8. 'create a object reference to the component 9. Dim obj As MyComponent.MyClass 10. Dim lngArgument As Long 11. Dim lngResult As Long 12. 13. 'create an instance of the object 14. Set obj = New MyClass 15. lngArgument = 2 16. 17. 'call the objects SquareIt method 18. lngResult = obj.SquareIt(lngArgument) 19. 20. MsgBox "The Square of " & lngArgument & _ 21. " is " & lngResult 22. 'end procedure 23. 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. Back to the top | Give Feedback

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). Back to the top | Give Feedback

Properties
Article ID: 185731 - Last Review: June 29, 2004 - Revision: 2.1 APPLIES TO Keywords:
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

kbhowto KB185731

Retired KB Content Disclaimer This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.

Potrebbero piacerti anche