Mark Purcell

Ok so i am checking my commands to see if I have already added them.

string CommandName = this.addInInstance.ProgID + "." + Name;

Command command = null;
command = this.applicationObject.Commands.Item(CommandName, -1);
catch { }

in C# this is throwin an exception "Value does not fall within the expected range."

I checked that the CommandName is correct and its the same as the name the command gets when it is added so Im assuming that the -1 is the issue.

It is required to be an int and the Microsoft docs say to pass -1.

Any sugguestions to get rid of these exceptions

Re: Visual Studio Extensibility DTE.Commands.Item parameter issue

Carlos Quintero - MVP

If you create a Visual C# 2005 add-in with the wizard and after the line:

//Add a command to the Commands collection:

Command command = commands.AddNamedCommand2(_addInInstance, "MyAddin2", "MyAddin2", "Executes the command for MyAddin2", true, 59, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported+(int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);

you add this line:

command = commands.Item(_addInInstance.ProgID + "." + "MyAddin2", -1);

it works correctly without throwing an exception. Can you try it

So, the -1 is not the problem. It could be that:

1) The name is incorrect, after all

2) The name is correct but the command does not exist.

FWIW, see how to add commands and buttons here:

HOWTO: Adding buttons, commandbars and toolbars to Visual Studio .NET from an add-in.

Re: Visual Studio Extensibility DTE.Commands.Item parameter issue

Mark Purcell

Ok so I am using the commands.Item method to look for a command that may or may not exists and obviously this is the wrong way to do it since the Item method throws out if the command does not exist.

I have been refering to your examples on temporary toolbars but I might still be getting confused about if a command exists or not when debugging with /resetaddin

I guess the best answer is to use a method to find a command if it exists that does not throw out exceptions when it does not exists.

Any suggestions appart from iterating through all the commands


Re: Visual Studio Extensibility DTE.Commands.Item parameter issue

Carlos Quintero - MVP

What you must do is to catch the exception. If that happens it means that the command does not exist. This is what I do, because there is no FindCommand method and iterating the commands collection is expensive...