Select to view content in your preferred language

Running python script from VBA code

4992
4
11-05-2010 12:18 PM
LornaMurison
Regular Contributor
Hello,

Now that my python script is working (from ArcToolbox) I'd like to set it up to run from my VBA code.  The code essentially provides an interface for the user to input the parameters that the script requires. Here are the important snippets:
VBA:
Dim Parameters As IVariantArray
Set Parameters = New VarArray

Parameters.Add CatchFile
Parameters.Add LandCover
Parameters.Add "H:\TEMP"
Parameters.Add LCTabOutF
Parameters.Add txtboxOutTabN.Value

Dim gp As Object
Set gp = CreateObject("esriGeoprocessing.GpDispatch.1")
gp.AddToolbox ("F:/Hydrology_Tools/Toolboxes/CatchmentDelineation.tbx")
gp.Execute "LandCover", Parameters, Nothing ' Automation error, unspecified error


Python:
# Import Modules
import arcgisscripting
# Create Geoprocessor object
gp = arcgisscripting.create(9.3)
gp.overwriteoutput = True
# Set up parameters
catchmentsFC = gp.GetParameterAsText(0) #in a toolbox GUI, Type = FeatureLayer
landCoverFC = gp.GetParameterAsText(1) #in a toolbox GUI, Type = FeatureLayer
tempWorkspace = gp.GetParameterAsText (2) #in a toolbox GUI, Type = Workspace
tableFolder = gp.GetParameterAsText (3) #in a toolbox GUI, Type = Folder
tableName = gp.GetParameterAsText (4) #in a toolbox GUI, Type = String
tableView = tableFolder + "\\" + tableName
gp.Workspace = tempWorkspace
wsDesc = gp.Describe(tempWorkspace)
wsType = wsDesc.WorkspaceType
...


The parameters are added correctly to the array, but I get an unspecified error when I try to execute the script.

Thank-you!
0 Kudos
4 Replies
JeffLee
Deactivated User
Lorna;

    Why would you use VBA to program a GUI, when an interface that looks like the image I've attached can easily be built within the toolbox? 😉
0 Kudos
LornaMurison
Regular Contributor
Hi Jeff,
There are a couple of reasons.
1. The code doesn't just provide a GUI for the parameters, it also uses that information to select which script to run.  I could just use a separate script tool for each task, but I want to make it intuitive for other people to use when I'm gone.
2. I didn't think that you could include a combo box in the toolbox GUI.
3. I'm curious to know how it's done.
0 Kudos
LoganPugh
Frequent Contributor
Check these links for the how's and why's of script tools:
http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=An_overview_of_creating_script_tools
http://resources.esri.com/geoprocessing/index.cfm?fa=codeGalleryDetails&scriptID=16491

I would just educate your users better on knowing which tool to run. If you really needed to you could probably still use a VBA or .NET GUI to open each tool's dialog.

For combo boxes, you can use a string parameter with a fixed value list, which would allow users to pick a value from a dropdown.
0 Kudos
JeffLee
Deactivated User
Lorna please read below.

Hi Jeff,
There are a couple of reasons.
1. The code doesn't just provide a GUI for the parameters, it also uses that information to select which script to run.  I could just use a separate script tool for each task, but I want to make it intuitive for other people to use when I'm gone.

Lorna;

   Running a python script from VBA can be a PITA.  If you have several scripts that you need to run, one option could be to put all the code in one big script and have a drop down menu to pick which part of the script to run.  if you only do geoprocessing, I would try to stick to python.  However, if you need VBA for GIS operations that are not available in the toolbox, you may need to stick to VBA.  The thing however is that VBA will eventually go the way of the dodo as version 10 is the last version with VBA support. 

2. I didn't think that you could include a combo box in the toolbox GUI.
3. I'm curious to know how it's done.

Really easy see attached word document and toolbox in zipped file.   The demo toolbox can be imported into arcgis.  Please forgive my bad grammar and layout in the attached document.   to Conclude, you can do a lot with python and it's only gonna get better with time.  I hope this helps.
0 Kudos