Knowledge Base - Technical Articles


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

Article ID: 27277
Software:  ArcGIS - ArcEditor 9.0, 9.1, 9.2, 9.3, 9.3.1 ArcGIS - ArcInfo 9.0, 9.1, 9.2, 9.3, 9.3.1 ArcGIS - ArcView 9.0, 9.1, 9.2, 9.3, 9.3.1
Platforms: N/A

Question

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

Answer

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.

Created: 7/22/2004
Last Modified: 5/3/2011

Article Rating: (3)
If you would like to post a comment, please login

Comments

By Anonymous - 01/31/2007 7:11 AM

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

for the line Dim pUID As New UID, I get a Compile error. "Can't find project or library"

By Anonymous - 01/17/2007 6:25 AM

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

Has this been updated for 9.2? I'm running into problems with: Dim pATBExt As IArcToolboxExtension. Looking forward to hearing back on this issue. Thanks, Nick

Rating:

By Anonymous - 05/13/2005 10:30 AM

Could a version of this article that works in ArcMap (so that selected records can be used) and that suppresses the model window (for models that have no parameters) be made available? Thanks.

Rating:

By Anonymous - 01/11/2005 6:44 AM

How can I get the names of the other tools like buffer-Tool. For example the idw-tool from Spatial Analyst Toolbox Thanks in advance

Rating:

By Anonymous - 01/06/2005 2:03 PM

This didnt work for my model... I even edited documentation keyword to see if it would find the model string there. How can I run a model from a button?

By Anonymous - 11/18/2004 7:33 AM

I tried this sample exactly how it was described and it doesn't seem to work. I'm receiving an automation error message. Model builder automation seems to hold a lot of promise, but I'm increasingly becoming frustrated with trying to run models from code. Ben Aller Chester County Health Deptartment West Chester, PA 610.344.4558