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