我試圖根據this的帖子在Microsoft Word中創建一個右鍵單擊菜單項。如何在Microsoft Office 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();
}
}
}
,當我右鍵點擊一個字母結果:
但有一張桌子,我不能做到這一點。退房的屏幕截圖,看看我的意思是:
我無法添加項目菜單,當我用鼠標右鍵單擊MS Word中的表格。請幫幫我。 謝謝!
對不起我的英語,...
https://msdn.microsoft.com/zh-cn/office/aa905340.aspx – MickyD
你是什麼意思,「用表我不能做」?實際發生了什麼,你期望什麼?你屏幕截圖中的箭頭是什麼意思? –
我想在右鍵菜單中添加一個菜單項。用桌子我不能這樣做。我想知道在ms word 2013中做的代碼。 – Quang