This document is archived and information here might be outdated.  Recommended version.


Find Command and Execute Snippet (ArcObjects .NET 10.6 SDK)
ArcObjects Library Reference

Find Command and Execute Snippet

Find a command and click it programmatically.

[C#]
///<summary>Find a command and click it programmatically.</summary>
///  
///<param name="application">An IApplication interface.</param>
///<param name="commandName">A System.String that is the name of the command to return. Example: "esriFramework.HelpContentsCommand" or "{D74B2F25-AC90-11D2-87F8-0000F8751720}"</param>
///   
///<remarks>Refer to the EDN document http://edndoc.esri.com/arcobjects/9.1/default.asp?URL=/arcobjects/9.1/ArcGISDevHelp/TechnicalDocuments/Guids/ArcMapIds.htm for a listing of available CLSID's and ProgID's that can be used as the commandName parameter.</remarks>
public void FindCommandAndExecute(ESRI.ArcGIS.Framework.IApplication application, System.String commandName)
{
  ESRI.ArcGIS.Framework.ICommandBars commandBars=application.Document.CommandBars;
  ESRI.ArcGIS.esriSystem.UID uid=new ESRI.ArcGIS.esriSystem.UIDClass();
  uid.Value=commandName; // Example: "esriFramework.HelpContentsCommand" or "{D74B2F25-AC90-11D2-87F8-0000F8751720}"
  ESRI.ArcGIS.Framework.ICommandItem commandItem=commandBars.Find(uid, false, false);

  if (commandItem != null)
    commandItem.Execute();
}
[Visual Basic .NET]
'''<summary>Find a command and click it programmatically.</summary>
'''  
'''<param name="application">An IApplication interface.</param>
'''<param name="commandName">A System.String that is the name of the command to return. Example: "esriFramework.HelpContentsCommand" or "{D74B2F25-AC90-11D2-87F8-0000F8751720}"</param>
'''   
'''<remarks>Refer to the EDN document http://edndoc.esri.com/arcobjects/9.1/default.asp?URL=/arcobjects/9.1/ArcGISDevHelp/TechnicalDocuments/Guids/ArcMapIds.htm for a listing of available CLSID's and ProgID's that can be used as the commandName parameter.</remarks>
Public Sub FindCommandAndExecute(ByVal application As ESRI.ArcGIS.Framework.IApplication, ByVal commandName As System.String)

  Dim commandBars As ESRI.ArcGIS.Framework.ICommandBars=application.Document.CommandBars
  Dim uid As ESRI.ArcGIS.esriSystem.UID=New ESRI.ArcGIS.esriSystem.UIDClass
  uid.Value=commandName ' Example: "esriFramework.HelpContentsCommand" or "{D74B2F25-AC90-11D2-87F8-0000F8751720}"
  Dim commandItem As ESRI.ArcGIS.Framework.ICommandItem=commandBars.Find(uid, False, False)
  If Not (commandItem Is Nothing) Then
    commandItem.Execute()
  End If

End Sub

Additional Requirements
  • The code in this document requires the following References added to the Visual Studio project:
  • ESRI.ArcGIS.Framework
  • ESRI.ArcGIS.System