FAQ: Can a model be called from a UI button using ArcObjects code?


Can a model be called from a UI button using ArcObjects code?


Yes. Attach the ArcObjects script below to the click event of a custom UI button on the GUI. The following sample shows how to run a custom model or an existing tool.

Please note that VBA will probably raise a syntax error for the reference to IArcToolboxExtension. To fix this, add a reference to the ESRI Geoprocessing UI library. Do this using the Tools > References menu item.

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

If calling a user created model, uncomment the line:

Set pTool = pAtb.GetToolbyNameString("MyModel")

and comment the line:

Set pTool = pAtb.GetToolbyNameString("buffer_analysis")

In addition, change 'MyModel' to the name of your model. The model requires the actual name specified in the model properties, found by right-clicking the model > Properties > name. This name is used rather then the label that appears in ArcToolBox.