Microsoft Word 插件 - 添加到上下文菜单 [英] Microsoft Word Add-In - Add to Contextual Menu

查看:40
本文介绍了Microsoft Word 插件 - 添加到上下文菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为 Word 构建了一个加载项,现在想添加一个选项,以便在用户突出显示一个单词并右键单击它时从中调用函数.我找到了文档 here 关于如何修改 manifest.xml 文件,但它似乎没有显示如何添加到上下文菜单的完整示例,仅显示如何添加按钮和删除下拉菜单.

I've built an Add-In for Word and would now like to add an option to call a function from it when a user highlights a word and right clicks on it. I've found documentation here on how to modify the manifest.xml file but it doesn't seem to show a full example of how to add to the contextual menu, only how to add buttons and drop down menus.

文档还指向我一个 github 页面展示示例,但同样缺少上下文菜单.它还指向 this 视频,该视频似乎在大约 1 时显示了我想要的内容:20 但也没有展示如何实现它.

The documentation also points me to a github page show examples but again lacks one for contextual menus. It also points to this video which seems to show what I want at about 1:20 but also doesn't show how to implement it.

到目前为止,我有这个(添加在 下方):

So far I have this (added below the <FunctionFile>):

<ExtensionPoint xsi:type="ContextMenu">
  <OfficeMenu id="ContextMenuText">
    <Control xsi:type="Menu" id="TestMenu">
      <Label resid="ContextMenuLabel" />
      <Supertip>
          <Title resid="ContextualMenuTitle" />
          <Description resid="ContextualMenuTitleDesc" />
      </Supertip>
    </Control>         
  </OfficeMenu>
</ExtensionPoint>

当我尝试使用这个工具验证清单文件时,它告诉我我缺少 Icon 元素,但我不需要上下文菜单的图像?

When I try and validate the manifest file with this tool it tells me that I'm missing an Icon element, but I don't need an image for a context menu?

我正在尝试做的事情是否可行,如果可行,有人可以给我举个例子吗?

Is what I'm trying to do possible and if so could someone point me to an example?

我已经更新了我的代码以反映@Mavi Domates 所写的内容

I've updated my code to reflect what @Mavi Domates wrote

<ExtensionPoint xsi:type="ContextMenu">
  <OfficeMenu id="ContextMenuText">
    <Control xsi:type="Button" id="openSearchButton">
      <Label resid="openSearchButtonLabel" />
      <Supertip>
        <Title resid="openSearchButtonTitle" />
        <Description resid="openSearchButtonDescription" />
      </Supertip>
      <Icon>
        <bt:Image size="16" resid="Contoso.tpicon_16x16" />
        <bt:Image size="32" resid="Contoso.tpicon_32x32" />
        <bt:Image size="80" resid="Contoso.tpicon_80x80" />
      </Icon>
      <Action xsi:type="ExecuteFunction">
        <FunctionName>getData</FunctionName>
      </Action>
    </Control>
  </OfficeMenu>
</ExtensionPoint>

当我添加此代码(在默认 正下方时,我的加载项不再显示在我的加载项"菜单中.我下载了微软的清单验证器,它告诉我我的清单没问题.

When I add this code (right below the default <ExtensionPoint xsi:type="PrimaryCommandSurface"> My add-in no longer shows up in the "My Add-ins" menu. I downloaded Microsoft's manifest validator and it tells me my manifest is fine.

我已将其范围缩小到导致问题的 Control 节点.如果我只是添加:

I've narrowed it down to the Control node causing the problem. If I just add:

<ExtensionPoint xsi:type="ContextMenu">
  <OfficeMenu id="ContextMenuText">
  </OfficeMenu>
</ExtensionPoint>

我的加载项仍然显示在菜单中.我还在 <bt:ShortStrings>:

my add-in still shows in the menu. I've also added the relevant strings in my resources under <bt:ShortStrings>:

<bt:String id="openSearchButtonLabel" DefaultValue="Check it out!" />
<bt:String id="openSearchButtonTitle" DefaultValue="Hover over me" />
<bt:String id="openSearchButtonDescription" DefaultValue="For more info go here" />

我的代码似乎和文档完全一样.我不知道从这里去哪里.如果情况有所改变,我将使用 Mac.

My code seems to be exactly the same as the documentation. I'm not sure where to go from here. I'm on a mac if that changes things.

推荐答案

您正在尝试做的事情是可能的,而且您已经非常接近了 :)

What you're trying to do is possible and you're very very close to it :)

您已将 ContextMenu 控件选择为 Menu 类型而不是 Button.这意味着您需要一些子菜单控件.

You have selected your ContextMenu control to be a Menu type and not a Button. That means you want some sub-menu controls.

来自此处的文档:https://docs.microsoft.com/en-us/office/dev/add-ins/develop/create-addin-commands#step-7-add-the-resources-元素

每组至少需要一个对照.一个控制元素可以是按钮或菜单.使用 Menu 指定下拉列表按钮控制.目前,仅支持按钮和菜单.看按钮控件和菜单控件部分了解更多信息.

Each group requires at least one control. A Control element can be either a Button or a Menu. Use Menu to specify a drop-down list of button controls. Currently, only buttons and menus are supported. See the Button controls and Menu controls sections for more information.

这意味着如果您想要嵌套菜单,您的清单是正确的,您只需要向其中添加项目 + 图标.您可以在下面看到示例代码,这些代码根据您单击的子菜单打开任务窗格/或执行功能.

That means if you want to have nested menus, your manifest is correct and you just need to add items + icons to it. Below you can see example code which is opening a taskpane / or executing a function depending on which sub-menu you click.

<ExtensionPoint xsi:type="ContextMenu">
  <OfficeMenu id="ContextMenuText">
    <Control xsi:type="Menu" id="TestMenu">
      <Label resid="ContextMenuLabel" />
      <Supertip>
          <Title resid="ContextualMenuTitle" />
          <Description resid="ContextualMenuTitleDesc" />
      </Supertip>
      <Icon>
         <bt:Image size="16" resid="your_icon_16"/>
         <bt:Image size="32" resid="your_icon_32"/>
         <bt:Image size="64" resid="your_icon_64"/>
         <bt:Image size="80" resid="_icon_80"/>
      </Icon>
      <!-- Add your context sub-menu items -->
      <Items>
         <Item id="contextitem1">
            <Label resid="somecontextlabel1"/>
            <Supertip>
                <Title resid="somecontexttitle1"/>
                <Description resid="somedescription1"/>
            </Supertip>
            <Icon>
                <bt:Image size="16" resid="someimage16"/>
                <bt:Image size="32" resid="someimage32"/>
                <bt:Image size="64" resid="someimage64"/>
                <bt:Image size="80" resid="someimage80"/>
            </Icon>
            <Action xsi:type="ShowTaskpane">
                <TaskpaneId>Mysupertaskpane1</TaskpaneId>
                <SourceLocation resid="TaskPaneSourceLocation1"/>
            </Action>
        </Item>

        <Item id="contextitem2">
            <Label resid="somecontextlabel2"/>
            <Supertip>
                <Title resid="somecontexttitle2"/>
                <Description resid="somedescription2"/>
            </Supertip>
            <Icon>
                <bt:Image size="16" resid="someimage16"/>
                <bt:Image size="32" resid="someimage32"/>
                <bt:Image size="64" resid="someimage64"/>
                <bt:Image size="80" resid="someimage80"/>
            </Icon>
            <Action xsi:type="ExecuteFunction">
                <FunctionName>dosomejsmagic</FunctionName>
            </Action>
        </Item>
      </Items>
    </Control>         
  </OfficeMenu>
</ExtensionPoint>

如果情况并非如此,并且您只是在寻找 Button(因此没有子菜单) - 那么您可以以此为例查看控件类型现在设置为成为按钮

If that's not the case and you're only looking for a Button (so no sub menus) - then you can use this as an example see the control type is now set to be a Button

<ExtensionPoint xsi:type="ContextMenu">
    <OfficeMenu id="ContextMenuText">
        <Control xsi:type="Button" id="Button1Id1">
            <Label resid="residLabel" />
            <Tooltip resid="residToolTip" />
            <Supertip>
                <Title resid="residLabel" />
                <Description resid="residToolTip" />
            </Supertip>
            <Icon>
                <bt:Image size="16" resid="icon1_32x32" />
                <bt:Image size="32" resid="icon1_32x32" />
                <bt:Image size="80" resid="icon1_32x32" />
            </Icon>
            <Action xsi:type="ExecuteFunction">
                <FunctionName>getData</FunctionName>
            </Action>
        </Control>
    </OfficeMenu>
</ExtensionPoint>

这篇关于Microsoft Word 插件 - 添加到上下文菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆