How to execute commands in a context menu programmatically


The ZoomToReferenceScale VBA macro shows how to programmatically execute the built-in command in a context menu. Setting the ContextItem property is the key.

How to use

  1. Open a map document, click the menum item of View->Data Frame Properties..., and choose General tab to set the reference scale.
  2. Paste the ZoomToReferenceScale macro into a module in the Visual Basic Editor.
  3. Run the macro to execute the command.
[VBA]
Public Sub ZoomToReferenceScale()
    Dim pDoc As IMxDocument
    Set pDoc = ThisDocument
    pDoc.ContextItem = pDoc.FocusMap
    Dim pCmdItem As ICommandItem
    Set pCmdItem =
    Application.Document.CommandBars.Find(arcid.DataFrameView_ZoomToReferenceScale)
    pCmdItem.Execute
End Sub