gpt4 book ai didi

c# - 如何在 Microsoft Office Word 中添加菜单项

转载 作者:太空狗 更新时间:2023-10-30 01:16:44 27 4
gpt4 key购买 nike

我尝试根据 this 在 Microsoft Word 中创建右键单击菜单项发布。

这是我的代码:

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
try
{
eventHandler = new _CommandBarButtonEvents_ClickEventHandler(MyButton_Click);
Word.Application applicationObject = Globals.ThisAddIn.Application as Word.Application;
applicationObject.WindowBeforeRightClick += new Microsoft.Office.Interop.Word.ApplicationEvents4_WindowBeforeRightClickEventHandler(App_WindowBeforeRightClick);
}
catch (Exception exception)
{
MessageBox.Show("Error: " + exception.Message);
}
}

void App_WindowBeforeRightClick(Microsoft.Office.Interop.Word.Selection Sel, ref bool Cancel)
{
try
{
this.AddItem();
}
catch (Exception exception)
{
MessageBox.Show("Error: " + exception.Message);
}

}
private void AddItem()
{
Word.Application applicationObject = Globals.ThisAddIn.Application as Word.Application;
CommandBarButton commandBarButton = applicationObject.CommandBars.FindControl(MsoControlType.msoControlButton, missing, "HELLO_TAG", missing) as CommandBarButton;
if (commandBarButton != null)
{
System.Diagnostics.Debug.WriteLine("Found button, attaching handler");
commandBarButton.Click += eventHandler;
return;
}
CommandBar popupCommandBar = applicationObject.CommandBars["Text"];
bool isFound = false;
foreach (object _object in popupCommandBar.Controls)
{
CommandBarButton _commandBarButton = _object as CommandBarButton;
if (_commandBarButton == null) continue;
if (_commandBarButton.Tag.Equals("HELLO_TAG"))
{
isFound = true;
System.Diagnostics.Debug.WriteLine("Found existing button. Will attach a handler.");
commandBarButton.Click += eventHandler;
break;
}
}
if (!isFound)
{
commandBarButton = (CommandBarButton)popupCommandBar.Controls.Add(MsoControlType.msoControlButton, missing, missing, missing, true);
System.Diagnostics.Debug.WriteLine("Created new button, adding handler");
commandBarButton.Click += eventHandler;
commandBarButton.Caption = "h5";
commandBarButton.FaceId = 356;
commandBarButton.Tag = "HELLO_TAG";
commandBarButton.BeginGroup = true;
}
}

private void RemoveItem()
{
Word.Application applicationObject = Globals.ThisAddIn.Application as Word.Application;
CommandBar popupCommandBar = applicationObject.CommandBars["Text"];
foreach (object _object in popupCommandBar.Controls)
{
CommandBarButton commandBarButton = _object as CommandBarButton;
if (commandBarButton == null) continue;
if (commandBarButton.Tag.Equals("HELLO_TAG"))
{
popupCommandBar.Reset();
}
}
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
Word.Application App = Globals.ThisAddIn.Application as Word.Application;
App.WindowBeforeRightClick -= new Microsoft.Office.Interop.Word.ApplicationEvents4_WindowBeforeRightClickEventHandler(App_WindowBeforeRightClick);

}

#region VSTO generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
//Event Handler for the button click

private void MyButton_Click(CommandBarButton cmdBarbutton, ref bool cancel)
{
System.Windows.Forms.MessageBox.Show("Hello !!! Happy Programming", "l19 !!!");
RemoveItem();
}
}

当我右键点击一个字母时的结果:

enter image description here

但是对于一张 table 我做不到。查看屏幕截图以了解我的意思:

enter image description here

当我右键单击 ms word 的表格时,我无法添加项目菜单。请帮我。谢谢!!

对不起我的英语,...

最佳答案

Word 维护不止一个上下文菜单。你可以通过枚举Application.CommandBars中的所有CommandBar对象来查看它们,它们的位置是msoBarPopup:

foreach (var commandBar in applicationObject.CommandBars.OfType<CommandBar>()
.Where(cb => cb.Position == MsoBarPosition.msoBarPopup))
{
Debug.WriteLine(commandBar.Name);
}

链接示例中使用的命令栏名为“文本”,该命令栏与右键单击段落文本某处时弹出的上下文菜单相关。

但是,要向表格的上下文菜单中添加内容,您必须将按钮添加到适当的与表格相关的上下文菜单中。表格具有不同的上下文菜单,具体取决于您单击时选择的内容:

  • applicationObject.CommandBars["表格"]
  • applicationObject.CommandBars["表格文本"]
  • applicationObject.CommandBars["表格单元格"]
  • applicationObject.CommandBars["表格标题"]
  • applicationObject.CommandBars["表列表"]
  • applicationObject.CommandBars["表格图片"]

所以我建议您提取一个方法,将按钮添加到 CommandBar,然后使用要添加按钮的所有命令栏调用该方法。类似于以下内容:

private void AddButton(CommandBar popupCommandBar)
{
bool isFound = false;
foreach (var commandBarButton in popupCommandBar.Controls.OfType<CommandBarButton>())
{
if (commandBarButton.Tag.Equals("HELLO_TAG"))
{
isFound = true;
Debug.WriteLine("Found existing button. Will attach a handler.");
commandBarButton.Click += eventHandler;
break;
}
}
if (!isFound)
{
var commandBarButton = (CommandBarButton)popupCommandBar.Controls.Add
(MsoControlType.msoControlButton, missing, missing, missing, true);
Debug.WriteLine("Created new button, adding handler");
commandBarButton.Click += eventHandler;
commandBarButton.Caption = "Hello !!!";
commandBarButton.FaceId = 356;
commandBarButton.Tag = "HELLO_TAG";
commandBarButton.BeginGroup = true;
}
}

// add the button to the context menus that you need to support
AddButton(applicationObject.CommandBars["Text"]);
AddButton(applicationObject.CommandBars["Table Text"]);
AddButton(applicationObject.CommandBars["Table Cells"]);

关于c# - 如何在 Microsoft Office Word 中添加菜单项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34280329/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com