日期:2014-05-18  浏览次数:20890 次

用VS2005做word外接程序并建立了多个按钮,但按钮是响应几次
http://www.cnblogs.com/marksun/archive/2012/03/05/2380542.html
用这种方式多建立几个按钮后,按钮只是响应几次.然后就不响应了.怎么办????


------解决方案--------------------
CommandBar toolBar;
try
{
toolBar = WordApp.CommandBars["XXXX"];
_firstInitToolbar = false;
}
catch (Exception)
{
//如果不存在,创建工具条
toolBar = WordApp.CommandBars.Add("XXXX",MsoBarPosition.msoBarTop, false, true);
_firstInitToolbar = true;
}
//添加按钮
try
{
if (!_firstInitToolbar)
{
SetWordEvent();
return;
}
InitBotton(toolBar);
_btnOpen.Click += BtnWordOpenClick;
_btnSave.Click += BtnWordSaveClick;
toolBar.Visible = true;
}
catch (Exception)
{
Utils.ShowWarningMessage(SR.GetString("Connect_AddOutlookToolbar_AddButtonFalure"));
}



private void InitBotton(CommandBar toolBar)
{
_btnOpen =
(CommandBarButton)
toolBar.Controls.Add(MsoControlType.msoControlButton, Type.Missing, Type.Missing, Type.Missing,
Type.Missing);
_btnOpen.Style = MsoButtonStyle.msoButtonIconAndCaption;
_btnOpen.Caption = SR.GetString("Connect_InitBotton_Download");
_btnOpen.Tag = SR.GetString("Connect_InitBotton_edoc2Download");
_btnOpen.Picture = ConvertImage.Convert(Resources.unfolder);

_btnSave =
(CommandBarButton)
toolBar.Controls.Add(MsoControlType.msoControlButton, Type.Missing, Type.Missing, Type.Missing,
Type.Missing);
_btnSave.Style = MsoButtonStyle.msoButtonIconAndCaption;
_btnSave.Caption = SR.GetString("Connect_InitBotton_Upload");
_btnSave.Tag = SR.GetString("Connect_InitBotton_edoc2Upload");
_btnSave.Picture = ConvertImage.Convert(Resources.Save);
}


 private void BtnWordOpenClick(CommandBarButton ctrl, ref bool cancel)
{
OpenWord();
}

private void BtnWordSaveClick(CommandBarButton ctrl, ref bool cancel)
{
SaveWord();
}

可以解决你的问题