soulshined
soulshined

Reputation: 10622

VBA|VSTO| CommandBar ContextMenu for Pictures/Objects only

I need help adding a context menu item (CommandBar) to my application:

Something that will limit the actual control to populate for only pictures and shapes:

Public WithEvents ContextMenuItem_SaveAsPicture As Office.CommandBarButton
Dim ContextMenu As Office.CommandBar = Application.CommandBars("Pictures&Objects")

Enumerating through all the CommandBars in my application produce:

Worksheet Menu Bar
Chart Menu Bar
WordArt
Picture
Drawing Canvas
Organization Chart
Diagram
Ink Drawing and Writing
Ink Annotations
Circular Reference
Standard
Formatting
PivotTable
Chart
Reviewing
Forms
Stop Recording
External Data
Formula Auditing
Full Screen
PivotChart Menu
Visual Basic
Web
Control Toolbox
Exit Design Mode
Refresh
Watch Window
PivotTable Field List
Workbook Queries
Data Catalog Search
Shared Queries
Borders
Protection
Text To Speech
List
Compare Side by Side
Workbook tabs
Cell
Column
Row
Cell
Column
Row
Ply
XLM Cell
Document
Desktop
Nondefault Drag and Drop
AutoFill
Button
Dialog
Series
Plot Area
Floor and Walls
Trendline
Chart
Format Data Series
Format Axis
Format Legend Entry
Formula Bar
PivotTable Context Menu
Query
Query Layout
AutoCalculate
Object/Plot
Title Bar (Charting)
Layout
Pivot Chart Popup
Phonetic Information
Auto Sum
Paste Special Dropdown
Find Format
Replace Format
List Range Popup
List Range Layout Popup
XML Range Popup
List Range Layout Popup
Nil
Filter Names
Excel Previewer
&Legacy Keyboard Support
Row
Column
Drawing
Shadow Settings
3-D Settings
Borders
Borders
Draw Border
Chart Type
Pattern
Font Color
Fill Color
Line Color
Drawing and Writing Pens
Annotation Pens
Drawing and Writing Pens
Annotation Pens
Order
Nudge
Align or Distribute
Rotate or Flip
Lines
Connectors
AutoShapes
Callouts
Flowchart
Block Arrows
Stars & Banners
Basic Shapes
Insert Shape
Shapes
Inactive Chart
Excel Control
Curve
Curve Node
Curve Segment
Pictures Context Menu
OLE Object
ActiveX Control
WordArt Context Menu
Rotate Mode
Connector
Script Anchor Popup
Canvas Popup
Organization Chart Popup
Diagram
Layout
Select
FaceID Browser
Custom 1
History
Task Pane

Property Editor
Office Clipboard
XML Source
Research
XML Document
Signatures
Document Actions
Clip Art
Selection
Format Object
Document Management
Document Updates
Mail Merge Panes
Fax Service
Meeting Workspace
Attachment Options
Accessibility Checker
Editor
Dictionaries
Thesaurus
Share
Smart Lookup
Activity
Researcher
Tap
Help
Online Content
Alt Text
Changes
Ribbon Adapter
Add Command
Built-in Menus
Clipboard
Envelope

Team
Status Bar
Ribbon

So far the eye catchers like 'Shapes' or 'Pictures Context Menu' or 'Picture' or even 'Basic Shapes' don't produce any results, not even an error.

Here is how I'm adding it if it helps:

 ContextMenuItem_SaveAsPicture = ContextMenu.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlButton, Before:=1, Temporary:=False)

The only thing that is working for me is the wonderful 'Cell' or List Range Popup, for tables, CommandBars

I guess I'll do the good ole trial and error 1 by time-consuming 1, but didn't see this on SO so thought I would ask it in case one of you smart people have the answer before I get to try #100

Upvotes: 2

Views: 1340

Answers (1)

soulshined
soulshined

Reputation: 10622

Although this can be done run-time, I've found it extensively easier to go the XML avenue, since that's what Microsoft really wants you to do anyways, and it doesn't hurt that I already have a Ribbon set up.

I've found none of those CommandBar id's/names are current, and majority are obsolete because most of them are just placeholders for pre-FluentUI Excel versions, I'm assuming.

If you use XML here is an, incomplete, example to populate an item for only Pictures/Shapes, but can be altered to suit your needs of course using the identifiers mentioned below:

<!-- language: lang-xml -->
....
<contextMenus>

  <!--Handles Pictures-->
  <contextMenu idMso="ContextMenuPicture">
    <button id="ContextMenu_SaveAsPicture2" label="Save As Picture" imageMso="WebControlSubmitWithImage" onAction="onAction"/>
  </contextMenu>

  <!--Handles Shapes : Basic, Block, Flowcharts and otherwise-->
  <contextMenu idMso="ContextMenuShape">
    <button id="ContextMenu_SaveAsPicture" label="Save As Picture" imageMso="WebControlSubmitWithImage" onAction="onAction"/>
  </contextMenu>

  <!--Handles when you group shapes, applies to pictures as well, or a mix of both-->
  <contextMenu idMso="ContextMenuObjectsGroup">
    <button id="ContextMenu_SaveAsPicture3" label="Save As Picture" imageMso="WebControlSubmitWithImage" onAction="onAction"/>
  </contextMenu>

  <!--Handles Line Shapes and line segments-->
  <contextMenu idMso="ContextMenuShapeConnector">
    <button id="ContextMenu_SaveAsPicture4" label="Save As Picture" imageMso="WebControlSubmitWithImage" onAction="onAction"/>
  </contextMenu>

  <!--Handles Freefrom drawn shapes -->
  <contextMenu idMso="ContextMenuShapeFreeform">
    <button id="ContextMenu_SaveAsPicture5" label="Save As Picture" imageMso="WebControlSubmitWithImage" onAction="onAction"/>
  </contextMenu>
</contextMenus>
....

Further more, after some serious digging on MSDN, I stumbled upon a great, and extremely thorough, mark up identifier PDF for all circumstances, not limited to this question. Which is useful when trying to insert before or after certain idMso and don't know what it's called. I'v really wanted to use ContextualTabs but previously had an extremly hard time finding identifiers. Thought this would help others in the search for Ribbon Identifiers, but it's a good find for me personally.

Here is the direct link from Microsofts download portal: [MSDN-CUICT/Excel]

It downloads as .txt, but I exported mine into Excel for readability. Regardless, a quick CTRL+F will help you navigate to any identifier/category. It even lists supported Excel versions for each identifier


And in case of link rot, it was found navigating to: [MS-CUSTOMUI2]: Custom UI XML Markup Version 2 Specification > Then go to the most recent date > click on the hyperlink for CT_ContextMenu, not CT_ContextMenus > and in the idMso description click on whatever section hyperlink reference they provide > then click on the Excel Command Table link

Upvotes: 2

Related Questions