Select to view content in your preferred language

Is it possible to start a model directly from an ArcMap-Toolbar?

2007
9
Jump to solution
05-02-2012 07:08 PM
MOHAMMEDHABBOUB
Deactivated User
Hello,

Is it possible to start a model (generated from model builder) directly from an ArcMap-Toolbar? I don't want to start the toolbox just for one frequently used tool.
I have 9.3 edition 🙂

thanks
0 Kudos
1 Solution

Accepted Solutions
DuncanHornby
MVP Notable Contributor
Mohammed,

Below is some VBA I used to execute a custom model in a toolbox, this should get you going.

Duncan

      ' Create workspacefactory     Dim pToolboxWorkspaceFactory As IWorkspaceFactory     Set pToolboxWorkspaceFactory = New esriGeoprocessing.ToolboxWorkspaceFactory          'Open a toolbox workspace     Dim pToolboxWorkspace As IToolboxWorkspace     Set pToolboxWorkspace = pToolboxWorkspaceFactory.OpenFromFile(g_ToolBoxPath, 0)          ' Connect to tool     Dim pGPToolBox As IGPToolbox     Set pGPToolBox = pToolboxWorkspace.OpenToolbox("WFD49e Processing Tools Enhanced.tbx")     Dim pGPTool As IGPTool     Set pGPTool = pGPToolBox.OpenTool("mdSubSetFloodplain")      ' Run tool     Dim pGPToolCommandHelper As IGPToolCommandHelper     Set pGPToolCommandHelper = New GPToolCommandHelper     pGPToolCommandHelper.SetTool pGPTool     pGPToolCommandHelper.Invoke Nothing      ' Destroy objects     Set pToolboxWorkspaceFactory = Nothing     Set pToolboxWorkspace = Nothing     Set pGPToolBox = Nothing     Set pGPTool = Nothing     Set pGPToolCommandHelper = Nothing

View solution in original post

0 Kudos
9 Replies
MOHAMMEDHABBOUB
Deactivated User
anyone can help?
0 Kudos
MOHAMMEDHABBOUB
Deactivated User
Hi,
I have wrote this code, but an error appears (see image):

Option Explicit
Private Sub UIButtonControl1_Click()
Public Sub Test()



  Dim pGP As Object

  Set pGP = CreateObject("esriGeoprocessing.GPDispatch.1")



On Error GoTo EH

pGP.AddToolbox ("D:\DRASTIC_MODEL.tbx")



  pGP.MyModel



  Exit Sub

EH:

  MsgBox pGP.GetMessages(), vbOKOnly, "Test"

End Sub


End Sub


reference: http://support.esri.com/en/knowledgebase/techarticles/detail/31110

Anyone can help?
0 Kudos
DuncanHornby
MVP Notable Contributor
Mohammed,

Below is some VBA I used to execute a custom model in a toolbox, this should get you going.

Duncan

      ' Create workspacefactory     Dim pToolboxWorkspaceFactory As IWorkspaceFactory     Set pToolboxWorkspaceFactory = New esriGeoprocessing.ToolboxWorkspaceFactory          'Open a toolbox workspace     Dim pToolboxWorkspace As IToolboxWorkspace     Set pToolboxWorkspace = pToolboxWorkspaceFactory.OpenFromFile(g_ToolBoxPath, 0)          ' Connect to tool     Dim pGPToolBox As IGPToolbox     Set pGPToolBox = pToolboxWorkspace.OpenToolbox("WFD49e Processing Tools Enhanced.tbx")     Dim pGPTool As IGPTool     Set pGPTool = pGPToolBox.OpenTool("mdSubSetFloodplain")      ' Run tool     Dim pGPToolCommandHelper As IGPToolCommandHelper     Set pGPToolCommandHelper = New GPToolCommandHelper     pGPToolCommandHelper.SetTool pGPTool     pGPToolCommandHelper.Invoke Nothing      ' Destroy objects     Set pToolboxWorkspaceFactory = Nothing     Set pToolboxWorkspace = Nothing     Set pGPToolBox = Nothing     Set pGPTool = Nothing     Set pGPToolCommandHelper = Nothing
0 Kudos
MOHAMMEDHABBOUB
Deactivated User
Hello Hornbydd,

first of all thanks for responding:

consedering that toolbox name:DRASTIC_MODEL, Model name: Model8

I worte the following :
Private Sub UIButtonControl1_Click() 
' Create workspacefactory 
Dim pToolboxWorkspaceFactory As IWorkspaceFactory 
Set pToolboxWorkspaceFactory = New esriGeoprocessing.ToolboxWorkspaceFactory 
  
'Open a toolbox workspace 
Dim pToolboxWorkspace As IToolboxWorkspace 
Set pToolboxWorkspace = pToolboxWorkspaceFactory.OpenFromFile  (g_ToolBoxPath, 0)
  
' Connect to tool 
Dim pGPToolBox As IGPToolbox 
Set pGPToolBox = pToolboxWorkspace.OpenToolbox("  DRASTIC_MODEL.tbx") 
Dim pGPTool As IGPTool 
Set pGPTool = pGPToolBox.OpenTool("  Model8") 

' Run tool 
Dim pGPToolCommandHelper As IGPToolCommandHelper 
Set pGPToolCommandHelper = New GPToolCommandHelper 
pGPToolCommandHelper.SetTool pGPTool 
pGPToolCommandHelper.Invoke Nothing 

' Destroy objects 
Set pToolboxWorkspaceFactory = Nothing 
Set pToolboxWorkspace = Nothing 
Set pGPToolBox = Nothing 
Set pGPTool = Nothing 
Set pGPToolCommandHelper = Nothing 
End Sub 


Note: what does blue text mean (I a beginner in VBA)
Is there any any else modification? because error appears as illustrated in image
0 Kudos
DuncanHornby
MVP Notable Contributor
You need to replace my variable string g_ToolBoxPath with the folder where the tool box is. For example "C:\XYZ\ModelTools"
0 Kudos
MOHAMMEDHABBOUB
Deactivated User
This is what I got 😞

I saved the toolbox in D:\
Toolbox name: Drastic_Model.tbx
Model Name: Model5

see image
0 Kudos
DuncanHornby
MVP Notable Contributor
Are you using the correct name for the model?  For this line of code to be correct you need to be using the name not the label of your model.

Set pGPTool = pGPToolBox.OpenTool("xxx")

In toolbox right click on your model and go to properties. You should be using the model NAME not the LABEL.

Duncan
0 Kudos
MOHAMMEDHABBOUB
Deactivated User
Hello,

I don't know why is does not work. please see attached and check if there is some thing wrong

Thanks
0 Kudos
MOHAMMEDHABBOUB
Deactivated User
Hornbydd,

thanks a lot it works :). the problem is that i did not add the toolbox to arcmap toolboxes.:rolleyes:

Thanks again 🙂
0 Kudos