Logo
HOWTO: Execute a command by Guid and Id from a Visual Studio package.

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio 2010
Date: July 2014   Microsoft Visual Studio 2012
      Microsoft Visual Studio 2013
Introduction

Although you can execute a command by name (ex: "Tools.Options") using the automation model DTE.ExecuteCommand method or by Guid/Id using the DTE.Commands.Raise method, as explained in the article HOWTO: Execute a command by Guid and Id from a Visual Studio add-in,  you can do it also without using the DTE object, using instead the GlobalInvoke method of the OleMenuCommandService service.

More Information

The following package shows the Visual Studio Options window when a solution is loaded:

Language: C#   Copy Code Copy Code (IE only)
using System;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
using System.ComponentModel.Design;
using Microsoft.Win32;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Shell;

namespace Company.CSharpVSPackageCommandExec
{
   [PackageRegistration(UseManagedResourcesOnly = true)]
   [InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)]
   [Guid(GuidList.guidCSharpVSPackageCommandExecPkgString)]
   [ProvideAutoLoad(Microsoft.VisualStudio.VSConstants.UICONTEXT.SolutionExists_string)]
   public sealed class CSharpVSPackageCommandExecPackage : Package
   {
      public CSharpVSPackageCommandExecPackage()
      {
      }

      protected override void Initialize()
      {
         OleMenuCommandService oleMenuCommandService;
         CommandID commandId;
         Type type;

         try
         {
            base.Initialize();

            type = typeof(Microsoft.VisualStudio.VSConstants.VSStd97CmdID);

            commandId = new CommandID(type.GUID, Microsoft.VisualStudio.VSConstants.cmdidToolsOptions);

            oleMenuCommandService = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;

            oleMenuCommandService.GlobalInvoke(commandId);
         }
         catch (Exception ex)
         {
            System.Windows.Forms.MessageBox.Show(ex.ToString());
         }
      }
   }
}
Language: VB.NET   Copy Code Copy Code (IE only)
Imports Microsoft.VisualBasic
Imports System
Imports System.Diagnostics
Imports System.Globalization
Imports System.Runtime.InteropServices
Imports System.ComponentModel.Design
Imports Microsoft.Win32
Imports Microsoft.VisualStudio
Imports Microsoft.VisualStudio.Shell.Interop
Imports Microsoft.VisualStudio.OLE.Interop
Imports Microsoft.VisualStudio.Shell

<PackageRegistration(UseManagedResourcesOnly:=True), _
   InstalledProductRegistration("#110", "#112", "1.0", IconResourceID:=400), _
   Guid(GuidList.guidVBNetVSPackageCommandExecPkgString),
   ProvideAutoLoad(Microsoft.VisualStudio.VSConstants.UICONTEXT.SolutionExists_string)> _
Public NotInheritable Class VBNetVSPackageCommandExecPackage
   Inherits Package

   Public Sub New()
   End Sub

   Protected Overrides Sub Initialize()

      Dim oleMenuCommandService As OleMenuCommandService
      Dim commandId As CommandID
      Dim type As Type

      MyBase.Initialize()

      Try

         MyBase.Initialize()

         type = GetType(Microsoft.VisualStudio.VSConstants.VSStd97CmdID)

         commandId = New CommandID(type.GUID, Microsoft.VisualStudio.VSConstants.cmdidToolsOptions)

         oleMenuCommandService = DirectCast(GetService(GetType(IMenuCommandService)), OleMenuCommandService)

         oleMenuCommandService.GlobalInvoke(commandId)

      Catch ex As Exception

         System.Windows.Forms.MessageBox.Show(ex.ToString())

      End Try

   End Sub

End Class


Go to the 'Visual Studio Extensibility (VSX)' web site for more articles like this (Articles section)