You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Office插件中编辑customUI添加右键菜单按钮实现文本查询?

Adding a Context Menu Item to Your Office Plugin with customUI14.xml

Hey David, awesome idea for your abbreviation lookup plugin! You absolutely can add a right-click context menu item using customUI14.xml in your Visual Studio 2017 VSTO project. Here's a step-by-step guide to make it work:

Step 1: Add the customUI14.xml file to your project

  • Right-click your Office add-in project in Solution Explorer.
  • Hover over Add and select New Item.
  • Choose XML File from the list, name it customUI14.xml, and click Add.

Step 2: Set the correct Build Action for the file

This is critical—Office won't recognize the file if it's not embedded properly:

  • Right-click the customUI14.xml file you just added.
  • Select Properties.
  • Under Build Action, choose Embedded Resource from the dropdown menu.

Step 3: Edit the customUI14.xml content

Paste the following XML into your file (this example targets Word's text selection context menu; adjust for other apps with the right idMso):

<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
  <contextMenus>
    <!-- Target the right-click menu for selected text -->
    <contextMenu idMso="ContextMenuText">
      <button id="btnLookupAbbreviation"
              label="查询缩写含义"
              onAction="OnLookupAbbreviationClicked"
              insertBeforeMso="Cut" />
    </contextMenu>
  </contextMenus>
</customUI>
  • idMso="ContextMenuText" ensures the menu item appears when text is selected.
  • onAction links the button to a method in your add-in code.
  • insertBeforeMso="Cut" positions your button before the built-in "Cut" option (tweak this to place it where you want).

Step 4: Implement the callback method in your add-in code

Open your ThisAddIn.cs (or ThisWordAddIn.cs, etc.) file and add this public method—make sure the name matches the onAction value from your XML:

public void OnLookupAbbreviationClicked(Office.IRibbonControl control)
{
    // Get the selected text from the document
    var selection = Globals.ThisAddIn.Application.Selection;
    
    if (selection != null && !string.IsNullOrWhiteSpace(selection.Text.Trim()))
    {
        string selectedAbbreviation = selection.Text.Trim();
        
        // Call your API to fetch the abbreviation meaning
        string meaning = FetchAbbreviationMeaningFromApi(selectedAbbreviation);
        
        // Display the result (you could also insert it directly into the document)
        MessageBox.Show($"缩写「{selectedAbbreviation}」的含义:\n{meaning}", "查询结果");
    }
    else
    {
        MessageBox.Show("请先选中要查询的缩写文本!", "提示");
    }
}

// Replace this with your actual API query logic
private string FetchAbbreviationMeaningFromApi(string abbreviation)
{
    // Example placeholder—add your API call here
    return "示例含义:这是测试缩写的解释内容";
}
  • The method must be public so Office can access it.
  • The Office.IRibbonControl parameter is required for all ribbon callback methods.

Step 5: Test your plugin

Start debugging (press F5). When the Office app opens:

  1. Type or select an abbreviation in the document.
  2. Right-click the selected text—your "查询缩写含义" menu item should appear.
  3. Click it to trigger the API lookup and see the result!

Troubleshooting tips

  • If the menu item doesn't show up: Double-check the Build Action is set to Embedded Resource, and the XML namespace (http://schemas.microsoft.com/office/2009/07/customui) is correct.
  • If the callback doesn't run: Ensure the method name in onAction matches your code exactly (case-sensitive!) and the method is public.
  • For other Office apps: Adjust the idMso value (e.g., ContextMenuCell for Excel cells, ContextMenuText works for text selection in most apps).

内容的提问来源于stack exchange,提问作者David

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:23:53