Knowledge Base - Technical Articles


Technical Article   HowTo:  Execute geoprocessing tools from within VBA

Article ID: 31110
Software:  ArcGIS - ArcEditor 9.0, 9.1 ArcGIS - ArcInfo 9.0, 9.1 ArcGIS - ArcView 9.0, 9.1
Platforms: N/A

Summary

Geoprocessing (GP) tools can be executed from within ArcToolbox, Model Builder or any COM-compliant integrated development environment (IDE) such as Python, VB Script or VBA. For an overview of scripting in the geoprocessing framework, please read the Writing Geoprocessing Scripts PDF on the ESRI Library CD from the installation CDs.

Instructions provided demonstrate the various ways a GP tool is executed from within VBA and the ArcObjects application programming interface (API).

 Please make sure that the correct object library references have been added to your project prior to running the code below.


Procedure

  1. Execute a geoprocessing tool from IGpDispatch. This method does not show the geoprocessing dialog boxes.


    Public Sub Test()
    

    Dim filePath As String
    filePath = "C:\Program Files\ArcGIS\Bin\TemplateData\USA"
    Dim inputName As String
    Dim clipName As String
    inputName = "cities.shp"

    Dim pGP As Object
    Set pGP = CreateObject("esriGeoprocessing.GPDispatch.1")

    On Error GoTo EH
    pGP.Workspace = filePath
    ' note: the inputs can also be objects; however the output must be a string.
    pGP.buffer_analysis filePath + "\" + inputName, "C:\temp\bufferfc1.shp", "1"

    Exit Sub
    EH:
    MsgBox pGP.GetMessages(), vbOKOnly, "Test"
    End Sub



  2. Same method as above, but demonstrating how to call a custom model or script tool.



    Public Sub Test()

    Dim pGP As Object
    Set pGP = CreateObject("esriGeoprocessing.GPDispatch.1")

    On Error GoTo EH
    pGP.AddToolbox ("C:\PathToYourToolbox\ToolboxName.tbx")

    pGP.MyModel

    Exit Sub
    EH:
    MsgBox pGP.GetMessages(), vbOKOnly, "Test"
    End Sub


  3. Execute a GP tool using IGpTool. This method is more complicated, but allows for items as parameter validation, and other. This method does not show the geoprocessing dialog boxes.


    Sub GP_Exexute_Buffer()
    
    'Modified from user forums
    Dim pUID As New UID
    Dim pAtbExtension As IArcToolboxExtension
    Dim pAtb As IArcToolbox
    Dim pTool As IGPTool
    Dim pParams As IArray
    Dim pParameter As IGPParameter
    Dim pParamEdit As IGPParameterEdit
    Dim pDataType As IGPDataType
    Dim sValue As String
    Dim pTrackCancel As ITrackCancel
    Dim pEnvmgr As IGPEnvironmentManager
    Dim pMess As IGPMessages
    pUID = "esriGeoprocessingUI.ArcToolboxExtension"
    Set pAtbExtension = Application.FindExtensionByCLSID(pUID)
    Set pAtb = pAtbExtension.ArcToolbox
    Set pTool = pAtb.GetToolbyNameString("buffer_analysis")
    Set pParams = pTool.ParameterInfo
    Set pParameter = pParams.Element(0)
    Set pParamEdit = pParameter
    Set pDataType = pParameter.DataType
    sValue = "C:\ESRI\ESRIDATA\CANADA\Rivers.shp"
    Set pParamEdit.Value = pDataType.CreateValue(sValue)
    Set pParameter = pParams.Element(1)
    Set pParamEdit = pParameter
    Set pDataType = pParameter.DataType
    sValue = "C:\ESRI\ESRIDATA\CANADA\Rivers_BUFF.shp"
    Set pParamEdit.Value = pDataType.CreateValue(sValue)
    Set pParameter = pParams.Element(2)
    Set pParamEdit = pParameter
    Set pDataType = pParameter.DataType
    sValue = "500 Meters"
    Set pParamEdit.Value = pDataType.CreateValue(sValue)
    Set pTrackCancel = New CancelTracker
    Set pEnvmgr = New GPEnvironmentManager
    'Set pMess = New GPMessages
    Set pMess = pTool.Validate(pParams, False, pEnvmgr)
    pTool.Execute pParams, pTrackCancel, pEnvmgr, pMess
    End Sub


  4. Open a geoprocessing tool using IGpToolCommandHelper. This method shows the geoprocessing dialog boxes so the user can add or alter values for the tool. Users click OK to execute the tool.


    Sub OpenGPTool()
    
    'Dim pApp As IGxApplication
    '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 = "C:\ESRI\ESRIDATA\CANADA\cities.shp"
    Set pParamEdit.Value = pDataType.CreateValue(sValue)

    'Second Parameter
    Set pParameter = pParams.Element(1)
    Set pParamEdit = pParameter
    Set pDataType = pParameter.DataType
    sValue = "C:\ESRI\ESRIDATA\CANADA\citiesZed2.shp"
    Set pParamEdit.Value = pDataType.CreateValue(sValue)

    'Third Parameter
    Set pParameter = pParams.Element(2)
    Set pParamEdit = pParameter
    Set pDataType = pParameter.DataType
    sValue = "100 Meters"
    Set pParamEdit.Value = pDataType.CreateValue(sValue)

    Dim pCommand As IGPToolCommandHelper
    Set pCommand = New GPToolCommandHelper
    pCommand.SetTool pTool
    pCommand.Invoke Nothing
    End Sub


  5. Example 2 that uses IGpToolCommandHelper. This sample doesn't supply any parameters. It also shows how to specify a custom model or script tool:


    Private Sub UIButtonControl1_Click() 
    
    'Ensure that there are references set to the
    'ESRI GeoprocessingUI Object Library and the
    'ESRI Geoprocessing Object Library.
    'Go to Tools > References to do this

    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

    '* Use this line for custom tools or models
    '* ========================================
    ' Set pTool = pAtb.GetToolbyNameString("MyModel")

    '* Use this line for existing tools
    '* ================================
    Set pTool = pAtb.GetToolbyNameString("buffer_analysis")

    Dim pCommand As IGPToolCommandHelper
    Set pCommand = New GPToolCommandHelper
    pCommand.SetTool pTool
    pCommand.Invoke Nothing

    End Sub


Created: 6/23/2006
Last Modified: 12/10/2009

If you would like to post a comment, please login

Comments

By Anonymous - 04/06/2007 7:14 AM

Other - See details below.

I copy and paste the number 4 code into my ArcMap and it didn't run!

By Anonymous - 10/12/2006 2:13 PM

The article is incorrect or the solution didn’t work.

The line "pCommand.Invoke Nothing" in Number 4 (the line before End Sub) should read "pCommand.Invoke pParams." In the context of the example the user expects that the buffer tool will open with the path to the shapefiles already being populated and a buffer distance being set. If you run the code as the example shows, none of the parameters are set when the tool opens.