Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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);


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?

share|improve this question
In what version and ESRI product are you using the code? – MathiasWestin May 13 '11 at 7: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:… 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 9:34
up vote 2 down vote accepted

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

share|improve this answer worked...! – ujjwalesri May 18 '11 at 4:48

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"))

        ArcMap.Document.FocusMap.ActiveView.Extent = layer.AreaofInterest;
share|improve this answer
Yes eventually I ended up doing it manualy :) – ujjwalesri May 18 '11 at 4:29

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

share|improve this answer
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

Your Answer


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.