4

I am trying to call ZoomToLayer from my custom code (shown below).

ESRI.ArcGIS.esriSystem.UID pUID = new ESRI.ArcGIS.esriSystem.UID();
ICommandItem CmdItem;

//pUID.Value = "{18DF94D9-0F8A-11D2-94B1-080009EEBECB}";
pUID.Value = "esriArcMapUI.LayerContextMenuItems";
pUID.SubType = 7;

CmdItem = IApplication.Document.CommandBars.Find(pUID, true, false);

CmdItem.Execute();

Upon investigation I found that CommandItem.Action is throwing COMException:

'CmdItem.Action' threw an exception of type 'System.Runtime.InteropServices.COMException - This method cannot be called on built in commands.

I am trying to use it as a command and not tool...hence I cannot understand the exception.

Any ideas why it's happening?

Hornbydd
  • 43,380
  • 5
  • 41
  • 81
ujjwalesri
  • 1,808
  • 12
  • 19
  • In what version and ESRI product are you using the code? – MathiasWestin May 13 '11 at 07:04
  • I am using ESRI 9.2 – ujjwalesri May 16 '11 at 10:22
  • Here is a snippet ArcGIS 10, I guess it's the same as for 9.2: http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#/Find_Command_and_Execute_Snippet/0049000000n0000000/ only difference I can see is that you are not using a recursive find. – MathiasWestin May 16 '11 at 13:53
  • @mathias I tried with recursive find as well, but same result. Basically the correct command item is being return, but it's action property is throwing exception...I dunno why :( – ujjwalesri May 17 '11 at 09:34

3 Answers3

2

You also need to set the IMxDocument.ContextItem to the layer that you want to zoom to. See ESRI forum: Re: Zooming In On A Shapefile To Highlight Areas

MathiasWestin
  • 4,212
  • 2
  • 29
  • 48
2

If you have the Layer of interest in your table of contents you could go this way and not have to mess with ContextMenuItems by trying this:

        ILayer layer = null;
        for (int a = 0; a < layercount; a++)
        {
            layer = focusmap.get_Layer(a);
            if (layer.Name.Contains("YOURLAYERNAMEHERE"))
            {

                break;
            }
        }
        ArcMap.Document.FocusMap.ActiveView.Extent = layer.AreaofInterest;
        ArcMap.Document.FocusMap.ActiveView.Refresh();
Luke
  • 331
  • 4
  • 11
1

Ok, I have not done VB.NET, but you are calling

CmdItem = IApplication.Document.CommandBars.Find(pUID, true, false);

on an the interface definition of IApplication and not an instance of the coclass

Ragi Yaser Burhum
  • 15,339
  • 2
  • 59
  • 76
  • Hi,The code is correct, but while posting it here a reference to another utility application was removed, hence your concern :) – ujjwalesri May 16 '11 at 10:22