How To: Pass an object from an ActiveX DLL to VBA
When creating an ActiveX DLL there may be a desire to pass an object from the DLL to ArcMap. Instructions provided demonstrate how to pass a Geometry object.
The following steps outline how to pass an object, in this case a geometry object, from a DLL to ArcMap. This article assumes previous experience creating custom Tools and Commands.
- Create a Visual Basic ActiveX DLL project that implements ICommand. To do this, click on the article in Related Information below called "Implement ICommand in Visual Basic". When following the procedure, include the following:
- Name the class 'clsGeom'.
- Name the project 'AoDemo'.
- In Project Properties, enter 'ArcObjects Geometry' as the Project Description.
- Save the class as clsGeom.cls
- Save the project as AoDemo_clsGeom.vbp.
- Compile the DLL as AoDemo_clsGeom.dll
- In the Visual Basic ActiveX DLL project, declare a module-level object variable to hold a reference to IGeometry.
Private m_pGeom As IGeometry
- Create a geometry object in OnClick and assign that geometry object to m_pGeom.
Private Sub ICommand_OnClick()
Dim pPt As IPoint
Set pPt = New Point
pPt.X = 15
pPt.Y = 25
Set m_pGeom = pPt
- Create a Public Property that returns a Geometry.
Public Property Get Geometry() As IGeometry
Set Geometry = m_pGeom
- Compile the DLL as AoDemo_clsGeom.dll. A. Click the File Menu and select Make DLL.
B. From the Project Menu, select Properties.
C. Select the Component Tab.
D. Click Binary Compatibility.
E. Click the File Menu and select Make DLL.
- Add the custom command to ArcMap. A. If it is not already running, start the ArcGIS application (ArcMap, ArcCatalog, or ArcScene) to which you want to add your custom command.
B. Click the Tools menu and select Customize.
C. Select the Commands Tab.
D. In the lower left corner of the dialog, use the pulldown to select where to save your command.
E. Click 'Add from file'.
F. Navigate to the DLL that contains the custom command.
G. Select the file and click Open.
H. When the 'Added Objects' dialog box appears, it reports the new objects that have been registered with ArcMap, ArcCatalog, or ArcScene. Click OK.
I. Click and drag the command from the Commands list and drop it onto a toolbar. Note that the Category you assigned appears in the left-hand panel.
J. Click Close.
- Open the VB Editor. In ArcMap, select Tools > Macros > Visual Basic Editor.
- Copy the code below into the Project's ThisDocument code module.
Public Sub GetGeometry()
Dim pApp As IApplication
Dim pDoc As IDocument
Dim pGeometry As IGeometry
Dim pCmdItem As ICommandItem
Dim pCmd As ICommand
Dim pClsSelection As AoDemo.clsGeom
Dim pID As New UID
pID = "AoDemo.clsGeom"
Set pApp = Application
Set pDoc = pApp.Document
Set pCmdItem = pDoc.CommandBars.Find(pID, True, False)
Set pCmd = pCmdItem.Command
Set pClsSelection = pCmd
Set pGeometry = pClsSelection.Geometry
If Not pGeometry Is Nothing Then
If Not pGeometry.IsEmpty Then
MsgBox "X: " & pGeometry.Envelope.LowerLeft.X
MsgBox "Y: " & pGeometry.Envelope.LowerLeft.Y
MsgBox "Geometry Is Empty"
MsgBox "Geometry is Nothing"
- Click Tools and select References.
- Scroll to 'ArcObjects Geometry' that was assigned to the Project Description in Step 1 and enable it.
- In ArcMap, click the command to initialize the Geometry.
- In ArcMap, run the macro GetGeometry. In ArcMap, click Tools > Macros > Macros.
To demonstrate that the geometry object has been passed from the DLL to ArcMap, MsgBoxes displays the X and Y coordinates of the point created in the DLL.