Select to view content in your preferred language

Running a model from vba

1194
2
01-17-2011 11:18 AM
GregoryAllspaw
Occasional Contributor
Below is some code that I have successfully used in previous projects to run a model from a vba script.  The problem is that I have pasted the code to a new project (with newly created models)and I can't get it to work.  I do know that there are certain references that have to be set (ESRI GeoprocessingUI Object Library and the ESRI Geoprocessing Object Library).  However, with these references set, the script still will not run.  The only error message I get is of the "Automation Error, Unspecified Error" variety.  Am I missing another reference that needs to be set?

I have marked the line of code that seems to be the problem.  It can't seem to find the model by it's name, even though it appears in the list in ArcToolbox for this project. 

I assume I am missing something simple...but I just can't figure it out.  Anyone have any ideas?




Sub test()

AutoRunModel "010_DeleteFields"

End Sub

Sub AutoRunModel(ModelName As String)

    Dim pUID As New UID
    Dim pArcToolboxExtension As IArcToolboxExtension
    Dim pArcToolbox As IArcToolbox
    Dim pTrackCancel As ITrackCancel
    Dim pEnvironmentManager As IGPEnvironmentManager
    Dim pMessages As IGPMessages
    Dim pTool As IGPTool
   
    Dim pParams As Variant
   

    'The Script
    pUID = "esriGeoprocessingUI.ArcToolboxExtension"
    Set pArcToolboxExtension = Application.FindExtensionByCLSID(pUID)
    Set pArcToolbox = pArcToolboxExtension.ArcToolbox
   

'*******************************************************************
'This line will not execute!!!
    Set pTool = pArcToolbox.GetToolbyNameString(ModelName)
'**********************************************************************


 
    'The script arguments/parameters
 
   Set pParams = pTool.ParameterInfo
    '=====================================
    'Ignore this bit, you don't have any params... left in just for example
    'Param 1
    'Set pParameter = pParams.Element(0)
    'Set pParamEdit = pParameter
    'Set pDataType = pParameter.DataType
    'sValue = "Test1"
    'Set pParamEdit.Value = pDataType.CreateValue(sValue)
   
    'Param 2
    'Set pParameter = pParams.Element(1)
    'Set pParamEdit = pParameter
    'Set pDataType = pParameter.DataType
    'sValue = "True"
    'Set pParamEdit.Value = pDataType.CreateValue(sValue)
   
    'Start the script running
    Set pTrackCancel = New CancelTracker
    Set pEnvironmentManager = New GPEnvironmentManager
    Set pMessages = New GPMessages
    pTool.Execute pParams, pTrackCancel, pEnvironmentManager, pMessages



End Sub
0 Kudos
2 Replies
DuncanHornby
MVP Notable Contributor
Gregory,

Is the string ModelName being set to the name of your model? The Name of your model is not the same as the display label which you see in your Toolbox. In ArcToolbox right click on your model and select properties. Then under the general tab check that you are passing the Name and not Label.

Also don't start your model name with a number.

Duncan
0 Kudos
GregoryAllspaw
Occasional Contributor
Thanks, Duncan.  That was exactly the problem.  I had forgotten about the name vs label issue.  The numbers don't seem to be a problem, but I did have to remove the underscores.

Thanks for the quick response!
0 Kudos