如何同时拥有VSTO功能区和上下文菜单?

时间:2011-07-11 15:47:26

标签: vsto contextmenu outlook-addin ribbon

编辑:海报答案是正确的,除非它应该读取包含的xmlns =“http://schemas.microsoft.com/office/2009/07/customui”。作为副作用,XML文件中定义的功能区和上下文菜单在Office 2007中不起作用。如果需要在2007中添加上下文菜单,请使用现已弃用,并且Outlook 2007消息窗口中的上下文菜单不可用。

this.Application.ItemContextMenuDisplay += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ItemContextMenuDisplayEventHandler(Application_ItemContextMenuDisplay);

我已创建了功能区和上下文菜单,但我不知道如何同时部署它们。

My Ribbon XML看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>

<customUI onLoad="Ribbon_Load" xmlns="http://schemas.microsoft.com/office/2006/01/customui">

<ribbon>

<tabs>

  <tab id="testTab" label="Test Label">

    <group id="testGroup" label="test">

      <button id="testButton" onAction="testAction" label="Test" size="large" 

          getImage ="GetCustomImage" screentip="Test Ribbon Functionality."/>         

    </group>      

  </tab>

 </tabs>

</ribbon>

</customUI>

Ribbon.cs有

public string GetCustomUI(string ribbonID)

{

  String ui = null;

  // Examine the ribbonID to see if the current item

  // is a Mail inspector.

  if (ribbonID == "Microsoft.Outlook.Mail.Read" ||

    ribbonID == "Microsoft.Outlook.Mail.Compose")

  {

    // Retrieve the customized Ribbon XML.

    ui = GetResourceText("WDCrypt2.Ribbon.xml") ;



  }

  return ui;

}

ContextMenu XML看起来像(来自教程)

<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">

 <contextMenus>

  <contextMenu idMso="ContextMenuText">

   <button idMso="FontDialog" visible="false" />

   <toggleButton id="MyToggle" label="My Toggle Button" />

   <button id="MyButton" label="My Button" insertBeforeMso="HyperlinkInsert" onAction="GetButtonID" />

   <menuSeparator id="MySeparator" />

   <menu id="MySubMenu" label="My Submenu" >

    <button id="MyButton2" label="Button on submenu" />

   </menu>

   <gallery id="galleryOne" label="My Gallery">

    <item id="item1" imageMso="HappyFace" />

    <item id="item2" imageMso="HappyFace" />

    <item id="item3" imageMso="HappyFace" />

    <item id="item4" imageMso="HappyFace" />

   </gallery>

   <dynamicMenu id="MyDynamicMenu" label= "My Dynamic Menu" getContent="GetMyContent" />

  </contextMenu>

 </contextMenus>

</customUI>

其cs文件看起来像:

private Office.IRibbonUI ribbon;

public Ribbon2()
{
}

#region IRibbonExtensibility Members

public string GetCustomUI(string ribbonID)
{
  return GetResourceText("WDCrypt2.Ribbon2.xml");
}

问题是要在我的Addin Class中使用我必须:

protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
{

  return new Ribbon();

}

OR

protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
{

  return new Ribbon2(); //The Context Menu

}

但我不能两者兼顾。如何同时获取上下文菜单和功能区?

编辑:我还想避免使用Application.ItemContextMenuDisplay,因为API正式弃用。

1 个答案:

答案 0 :(得分:11)

您需要组合两个功能区XML文件,然后只有一个回调文件:

<?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
<ribbon>
<tabs>
  <tab id="testTab" label="Test Label">
    <group id="testGroup" label="test">
      <button id="testButton" onAction="testAction" label="Test" size="large" 
          getImage ="GetCustomImage" screentip="Test Ribbon Functionality."/>         
    </group>      
  </tab>
 </tabs>
</ribbon>
<contextMenus>
  <contextMenu idMso="ContextMenuText">
   <button idMso="FontDialog" visible="false" />
   <toggleButton id="MyToggle" label="My Toggle Button" />
   <button id="MyButton" label="My Button" insertBeforeMso="HyperlinkInsert" onAction="GetButtonID" />
   <menuSeparator id="MySeparator" />
   <menu id="MySubMenu" label="My Submenu" >
    <button id="MyButton2" label="Button on submenu" />
   </menu>
   <gallery id="galleryOne" label="My Gallery">
    <item id="item1" imageMso="HappyFace" />
    <item id="item2" imageMso="HappyFace" />
    <item id="item3" imageMso="HappyFace" />
    <item id="item4" imageMso="HappyFace" />
   </gallery>
   <dynamicMenu id="MyDynamicMenu" label= "My Dynamic Menu" getContent="GetMyContent" />
  </contextMenu>
 </contextMenus>
</customUI>