Friday, January 13, 2006
Linking AutoCAD VBA to a VB DLL
Platform: AutoCAD 2006
Platform: Required VB 6
Visual Basic for Applications (VBA) is a great tool for quick routines or even large projects that have no budget. However, if the project requires any controls that VBA does not include, like a listview, a full Visual Basic application might be required. Linking a VB application to AutoCAD is not that easy and is usually left to C++ or .Net programmers with ObjectARX.
Fortunately, we can reference a Visual Basic Dynamic Link Library (DLL) within AutoCAD VBA.
Start a Visual Basic project with the ActiveX Dll template and copy the code below into the class module.
Public Function HelloWorld() As Boolean
On Error GoTo ErrorHandler
MsgBox "Hello World!", vbOKOnly + vbExclamation, "Hello"
HelloWorld = True
ErrorHandler:
End Function
Rename the class module to "Sample" and rename the project to "vb2vba". Save the project as "vb2vba" as well. Compile the application as "vb2vba.dll".
In AutoCAD 2006, open the VBA Interface and add the code block below to the "ThisDrawing" module.
Public Sub ShowDialog()
' Show the Hello World message box
On Error GoTo ErrorHandler
Dim objSample As vb2vba.Sample
Set objSample = New vb2vba.Sample
If Not objSample.HelloWorld = True Then Err.Raise vbObjectError + 1, , "Error calling HelloWorld from ShowDialog"
Set objSample = Nothing
Exit Sub
ErrorHandler:
Set objSample = Nothing
End Sub
Click on the "Tools" menu in the VBA IDE and select "References...". Find "vb2vba" and click the check mark on.
You should now be able to run the "ShowDialog" subroutine in AutoCAD VBA. It will call the VB Dll and display a message box with the famous "Hello World!" message.
Platform: Required VB 6
Visual Basic for Applications (VBA) is a great tool for quick routines or even large projects that have no budget. However, if the project requires any controls that VBA does not include, like a listview, a full Visual Basic application might be required. Linking a VB application to AutoCAD is not that easy and is usually left to C++ or .Net programmers with ObjectARX.
Fortunately, we can reference a Visual Basic Dynamic Link Library (DLL) within AutoCAD VBA.
Start a Visual Basic project with the ActiveX Dll template and copy the code below into the class module.
Public Function HelloWorld() As Boolean
On Error GoTo ErrorHandler
MsgBox "Hello World!", vbOKOnly + vbExclamation, "Hello"
HelloWorld = True
ErrorHandler:
End Function
Rename the class module to "Sample" and rename the project to "vb2vba". Save the project as "vb2vba" as well. Compile the application as "vb2vba.dll".
In AutoCAD 2006, open the VBA Interface and add the code block below to the "ThisDrawing" module.
Public Sub ShowDialog()
' Show the Hello World message box
On Error GoTo ErrorHandler
Dim objSample As vb2vba.Sample
Set objSample = New vb2vba.Sample
If Not objSample.HelloWorld = True Then Err.Raise vbObjectError + 1, , "Error calling HelloWorld from ShowDialog"
Set objSample = Nothing
Exit Sub
ErrorHandler:
Set objSample = Nothing
End Sub
Click on the "Tools" menu in the VBA IDE and select "References...". Find "vb2vba" and click the check mark on.
You should now be able to run the "ShowDialog" subroutine in AutoCAD VBA. It will call the VB Dll and display a message box with the famous "Hello World!" message.