How to invoke a geoprocessing tool from a button on a toolbar with parameters


This sample shows how to invoke a geoprocessing tool from a button on a toolbar with parameters.

How to use

  1. Copy the function into VBA. You will need to have a UI Button set up. This code will go in the Click method for that button.
  2. Update the path to reference valid data on your computer.
  3. You will need to have ESRI Geoprocessing and ESRI GeoprocessingUI Libraries referenced.
[VBA]
Private Sub UIButtonControl1_Click()
    Dim pApp As IApplication
    Set pApp = Application
    
    Dim pUID As New UID
    pUID = "esriGeoprocessingUI.ArcToolboxExtension"
    
    Dim pATBExt As IArcToolboxExtension
    Set pATBExt = Application.FindExtensionByCLSID(pUID)
    
    Dim pAtb As IArcToolbox
    Set pAtb = pATBExt.ArcToolbox
    
    Dim pTool As IGPTool
    Set pTool = pAtb.GetToolbyNameString("buffer_analysis")
    
    Dim pParams As IArray
    Set pParams = pTool.ParameterInfo
    
    Dim pParameter As IGPParameter
    Dim pParamEdit As IGPParameterEdit
    Dim pDataType As IGPDataType
    Dim sValue As String
    
    'First Parameter
    Set pParameter = pParams.Element(0)
    Set pParamEdit = pParameter
    Set pDataType = pParameter.DataType
    sValue = "D:\workspace\Newfoundland.mdb\roads"
    Set pParamEdit.Value = pDataType.CreateValue(sValue)
    
    'Second Parameter
    Set pParameter = pParams.Element(1)
    Set pParamEdit = pParameter
    Set pDataType = pParameter.DataType
    sValue = "D:\workspace\Newfoundland.mdb\roads_buf"
    Set pParamEdit.Value = pDataType.CreateValue(sValue)
    
    'Third Parameter
    Set pParameter = pParams.Element(2)
    Set pParamEdit = pParameter
    Set pDataType = pParameter.DataType
    sValue = "500 Meters"
    Set pParamEdit.Value = pDataType.CreateValue(sValue)
    
    Dim pCommand As IGPToolCommandHelper
    Set pCommand = New GPToolCommandHelper
    pCommand.SetTool pTool
    pCommand.Invoke pParams
End Sub