none
Adding command bar button to Word image context command bar RRS feed

  • Question

  • This is in VS2010 SP1, Word 2010, using VSTO & C#.  I can add & use a command bar button for the "Text" context menu in my Word add-in. It works as expected as long as I right-click in the text areas. 

    I've tried to add the same button for the Word picture context menu and so far have been unsuccessful.  I've tried menus for "Picture", "Edit Picture", "Floating Picture", "Inline Picture", "Shapes", "Standard", etc.  Over 100 different menu objects in the Application.CommandBars menu list.

    Open Word,  insert a picture (jpeg), right-click on the picture.  The menu that appears never has my button attached.  Very frustrating!

    Any help is appreciated. The code used is posted here & is similar to that posted in other discussions on this board.

     

     

    public partial class ThisAddIn
    {
      private Word.Application myApp;
      private Word.Template myTemplate;
      private string[] myContextList = {"Text", "Edit Picture" };
    
      #region Add-In Base Events
      private void ThisAddIn_Startup(object sender, System.EventArgs e)
      {
        myApp = this.Application;
        GetCustomTemplate();
        RemoveExistingMenuItem();
        AddMenuItem();
      }
    }
    
    private void GetCustomTemplate()
    {
      object TemplatePath = Path.Combine(AppSetting.ConfigPath, "myTemplate.dotx");
      object install = true;
    
      foreach (Word.Template installedTemplate in myApp.Templates)
      {
        if (installedTemplate.FullName == (string)TemplatePath)
        {
          install = false;
          break;
        }
      }
    
      try
      {
        if ((bool)install)
        {
          myApp.AddIns.Add(TemplatePath.ToString(), ref install);
        }
        myTemplate = myApp.Templates.get_Item(ref TemplatePath);
      }
      catch (Exception ex)
      {
        MessageBox.Show(ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
        myTemplate = null;
      }
    }
    
    private void RemoveExistingMenuItem()
    {
      myApp.CustomizationContext = myTemplate;
      foreach (string index in myContextList)
      {
        try
        {
          Core.CommandBar contextMenu = myApp.CommandBars[index];
    
          Core.CommandBarButton control =
            (Core.CommandBarButton)contextMenu.FindControl
            (Core.MsoControlType.msoControlButton, missing,
            "myMenuItem", true, true);
    
          if ((control != null))
          {
            control.Delete(true);
          }
        }
        catch (Exception ex)
        {
          MessageBox.Show(ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
      }
      myTemplate.Saved = true;
    }
    
    private void AddMenuItem()
    {
      myApp.CustomizationContext = myTemplate;
      Core.MsoControlType menuItem = Core.MsoControlType.msoControlButton;
    
      foreach (string index in myContextList)
      {
        try
        {
          Core.CommandBarButton ctxButton =
            (Core.CommandBarButton)wtApp.CommandBars[index].Controls.Add(menuItem, missing, missing, 1, true);
    
          ctxButton.Style = Core.MsoButtonStyle.msoButtonCaption;
          ctxButton.Caption = "My Button";
          ctxButton.Tag = "myMenuItem";
          ctxButton.Visible = true;
        }
        catch (Exception ex)
        {
          MessageBox.Show(ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
      }
      myTemplate.Saved = true;
      GC.Collect();
    }
    

     

    • Edited by cmm-Cliff Thursday, October 6, 2011 7:11 PM
    Thursday, October 6, 2011 7:08 PM

Answers