|
office 版本为 Microsoft 365MSO (版本 2202 Build 16.0.14912.20000) 64 位
visual 2022
代码运行时报无法将类型为 System. Comobject的COM对制转换为接口类型Microsoft. Office.Core. Command Barbutton"。此操作失败的原因是对ID为
000030E-0000-0000-0000-000000006的接口的COM组件调用 Queryinterface因以下错误而失欢不支持此接口(异常来自 HRESULT:0x80004002( ENOINTERFACE),新手自学 应该如何解决,谢谢各位 代码如下:
private void CheckIfMenuBarExists()
{
try
{
Office.CommandBarPopup foundMenu = (Office.CommandBarPopup)this.Application.CommandBars.ActiveMenuBar.FindControl(Office.MsoControlType.msoControlPopup, System.Type.Missing, menuTag, true, true);
if (foundMenu != null)
{
foundMenu.Delete(true);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void AddMenuBar()
{
try
{
Office.CommandBarPopup cmdBarControl = null;
Office.CommandBar menubar = (Office.CommandBar)Application.CommandBars.ActiveMenuBar;
int controlCount = menubar.Controls.Count;
string menuCaption = "&New Menu";
cmdBarControl = (Office.CommandBarPopup)menubar.Controls.Add(Office.MsoControlType.msoControlPopup, missing, missing, controlCount, true);
if (cmdBarControl != null)
{
cmdBarControl.Caption = menuCaption;
//增加菜单
menuCommand = (Office.CommandBarButton)menubar.Controls.Add(Office.MsoControlType.msoControlPopup, missing, missing, missing, true);
menuCommand.Caption = "&New Menu Command";
menuCommand.Tag = "NewMenuCommand";
menuCommand.FaceId = 65;
//menuCommand.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(menuCommand_Click);
menuCommand.Click += new Office._CommandBarButtonEvents_ClickEventHandler(menuCommand_Click);
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
//增加一个文本当菜单点击事件
private void menuCommand_Click(Microsoft.Office.Core.CommandBarButton ctrl,ref bool CancelDefault)
{
Globals.Sheet1.Range["A1", missing].Value2 = "the menu command was clicked";
}
|
|