Find the distance between two features using IProximityOperator::ReturnDistance

Last Published: April 25, 2020


This article pertains to ArcGIS versions 8.x. Later versions of ArcGIS may contain different functionality, as well as different names and locations for menus, commands and geoprocessing tools.

The sample code in this article returns the distance between two features using VBA and ArcObjects.

Support for Visual Basic for Applications (VBA) for ArcMap and ArcCatalog ended with the ArcGIS 10.2.2 release, and Esri has not included VBA compatibility setups since version 10.5. See: ArcGIS Desktop and VBA Moving Forward


  1. Open ArcMap.
  2. Create a new UIButtonControl: How To: Create a new UIButtonControl​
For more information on creating a UIControl, see the ArcGIS Desktop Help topic: 'Creating custom commands with VBA and UI Controls'
  1. Right-click the UIButtonControl and select View Source.
  2. Paste this code into the UIButtonControl's click event.
Dim pDoc As IMxDocument
Dim pMap As IMap

Set pDoc = ThisDocument
Set pMap = pDoc.FocusMap

Dim pFLayer As IFeatureLayer
Set pFLayer = pMap.Layer(0)

Dim pFeat As IFeature
Dim pFeat1 As IFeature
Dim dDist As Double
Dim pFc As IFeatureClass
Set pFc = pFLayer.FeatureClass
'Get features 1 and 4
Set pFeat = pFc.GetFeature(1)
Set pFeat1 = pFc.GetFeature(4)

Dim pProxOp As IProximityOperator
Dim pGeom As IGeometry
Dim pGeom1 As IGeometry
Set pGeom1 = pFeat1.Shape
Set pGeom = pFeat.Shape
Set pProxOp = pGeom

'Find the Distance
dDist = pProxOp.ReturnDistance(pGeom1)

MsgBox "The distance is: " & dDist
  1. Close the Visual Basic Editor.
  2. Add data to ArcMap.
  3. Click the new button to display the distance.

Article ID:000004977

  • ArcMap 8 x

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options