Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am now looking for a code in VBA that can call Tools from ArcToolBox(The little red box)

I have find a Arcobject code(I thought it's can run under VBA) but it's don't work!

I have to delete the URL here....

in the end of the page it's have Arcobject code

But it's don't work!

can anyone tells me how to use it?

thanks for the help!!

@ Mike Toews

Compile error:

Sub or Function not defined

at OpenPointDataset

@artwork21 I call it from Tools>marcos>visual basic editor It's a simple compiler in GIS not a ui control

I have tried the method with the page http://edndoc.esri.com/arcobjects/9.0/Samples/Geoprocessing/RunToolWithGPDispatch.htm

and I try my Kriging Tool

Sub RunGPTool()
 'Create the Geoprocessor object
   Dim GP As Object
   Set GP = CreateObject("esriGeoprocessing.GpDispatch.1")

  'Set the toolbox
   GP.Toolbox = "C:\Program Files\ArcGIS\ArcToolBox\Toolboxes\Spatial Analyst Tools.tbx"

   'Execute tools
  GP.Kriging "C:\sample.shp", _
    "F30", "C:\AA\456.shp", "250", "12"


 End Sub

but it's highlight error at GP.Kriging "C:\sample.shp", _ "F30", "C:\AA\456.shp", "250", "12"

and says
automation error
unspecified error

how to fix it??

update 2011/06/11 10:00PM(+8)

@Casey

http://edndoc.esri.com/arcobjects/9.0/Samples/Geoprocessing/RunToolWithGPDispatch.htm

in this page

there is no "_analysis"in the sample code...

and I tried GP.Kriging_sa

but it's still don't work....

thanks for you support!

by the way I have tried buffer it's works!!

share|improve this question
    
Elaborate on how it does not work, e.g. error messages, etc. –  Mike T Jun 9 '11 at 9:56
    
how are you calling the code, from a UI Control or macro? –  artwork21 Jun 9 '11 at 11:20

1 Answer 1

up vote 3 down vote accepted

Here is sample code from ESRI on one method to run a Toolbox tool from VBA: http://edndoc.esri.com/arcobjects/9.0/Samples/Geoprocessing/RunToolWithGPDispatch.htm.

There has been some user discussion regarding this that may help: http://forums.esri.com/Thread.asp?c=93&f=992&t=132081 http://forums.arcgis.com/threads/6117-Strange-problem-running-GP-tools-from-VBA

In my experience it's a lot easier to call the Toolbox Tools from the command line or python (scripting). The help page for each tool contains code samples for both of these methods, for example: http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=intersect_%28analysis%29

UPDATE 6/10/11

It looks like the ESRI code sample doesn't work as is (go figure). You need to comment out the gp.Toolbox line and add the name of the toolbox to the references of the tools. For example, to fix the sample code:

Sub RunGPTool()
 'Create the Geoprocessor object
   Dim GP As Object

   Set GP = CreateObject("esriGeoprocessing.GpDispatch.1")

   'Set the toolbox --COMMENT OUT THE GP.Toolbox LINE
   'GP.Toolbox = "D:\ArcGIS\ArcToolbox\Toolboxes\Analysis Tools.tbx"

   'Execute tools --APPEND THE NAME OF THE TOOLBOX TO THE TOOL
   GP.Clip_analysis "D:\Workspace\Portland.mdb\Region\streets", _
   "D:\Workspace\Portland.mdb\Region\downtown", "D:\Workspace\Portland.mdb\Region\dt_streets"

   GP.Buffer_analysis "D:\Workspace\Portland.mdb\Region\dt_streets", _
"D:\Workspace\Portland.mdb\Region\dt_streetsbuf", "500"
 End Sub

Here is what I tested on my machine (I don't have Spatial Analysis to test your code):

Sub RunGPTool()

   'Create the Geoprocessor object
    Dim GP As Object
    Set GP = CreateObject("esriGeoprocessing.GpDispatch.1")

   'Set the toolbox
   'GP.Toolbox = "D:\ArcGIS\ArcToolbox\Toolboxes\Analysis Tools.tbx"

   'Execute tools
   GP.MakeFeatureLayer_management "C:\Temp\NKCDC.shp", "temp_NKCDC"

End Sub

You would change

 GP.Kriging 

to

 GP.Kriging_sa
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.