Visual Studio Development Bookmark and Share   
 index > Visual Studio Tools for Office > Excel CommandButton Event Not firing for 2ND Time Click.
 

Excel CommandButton Event Not firing for 2ND Time Click.

Hi

I have created a collection of Excel CommandBarButtons on workBook_open event and added to my CommandBar.while iterating through a loop i have also assigned event handler for each buttons.

I am getting a funny output when exceuting this>

1) when for the first timei open the Excel workbook all buttons are loaded perfectly on the excel command bar.
2) For the first time if i click any of the buttons(only once)it worked and respective output is shown in a window form.
3) Next time after closing the form if i click ,i am not getting any event.(No event takes place)
4)Last commanbar button whcich has created at the last iterate , fires the event on every click.

Can anybody suggest me where i am doing the mistake ???

MyCode Below:

commandBar = applicationObject.CommandBars.Add("ExcelCommadBar", 1, System.Reflection.Missing.Value, true);

ButtonInfo=ParseXML(); //ButtonInfo is a DataTable

for(int rowCount=0;rowCount<ButtonInfo.Rows.Count;rowCount++)

{

_btnNewBrowser = (Microsoft.Office.Core.CommandBarButton)

commandBar.Controls.Add(1,

System.Reflection.Missing.Value,

System.Reflection.Missing.Value,

System.Reflection.Missing.Value,

System.Reflection.Missing.Value);

_btnNewBrowser.Caption = ButtonInfo.Rows[rowCount].ItemArray[1].ToString();

_btnNewBrowser.Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonIconAndCaption;

_btnNewBrowser.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(_ButtonWorkSheet_Click);

}

Thanks

__Das


Dwipayan Das  Friday, August 29, 2008 9:30 AM
Hi

I Found a great articile on this and solved the problem for ever.This is a genuine problem occurs in Excel if you want to create more than one CommandBar button and add eventHandlers for the Buttons

The link below.

http://www.angelosp.com/blog/post/Extending-Word-with-VSTO-2005-e28093-Adding-a-toolbar.aspx.

Thanks

__Das
Dwipayan Das  Thursday, September 04, 2008 3:49 PM

Your problem might be in your _ButtonWorkSheet_Click event.

This is where you are handling the clicks. Can youshow that code?

incre-d  Friday, August 29, 2008 10:11 AM

Hi

This is code as follows.Refresh is a windows form.

public void _ExecuteCommand_Click(Microsoft.Office.Core.CommandBarButton Ctrl,

ref bool CancelDefault)

{

Refresh Ref = new Refresh();

if (commandBar.Controls[1].Caption == Ctrl.Caption)

{

try

{

_XLWorkBook = (Excel.Workbook)applicationObject.ActiveWorkbook;

_AmbWs = (Excel.Worksheet)_XLWorkBook.Worksheets["Sheet_Data"];

GetWorkBookDetails();

Ref.Application = this.applicationObject;

Ref.ScriptEnable = true;

Ref.NavigateURL = HTMLPath + "\\sheet.htm";

Ref.Width = 800;

Ref.Height = 640;

Ref.frmHeight = Ref.Height;

Ref.frmWidth = Ref.Width;

Ref.Show();

}

catch

{

Ref.Application = this.applicationObject;

Ref.NavigateURL = HTMLPath + "\\sheet.htm";

Ref.Width = 800;

Ref.Height = 640;

Ref.frmHeight = Ref.Height;

Ref.frmWidth = Ref.Width;

Ref.Show();

}

}

}

Thanks

_Das

Dwipayan Das  Monday, September 01, 2008 12:57 PM

Hi the 2 signatures don't match

Code Snippet
_btnNewBrowser.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(_ButtonWorkSheet_Click);

Code Snippet

public void _ExecuteCommand_Click(Microsoft.Office.Core.CommandBarButton Ctrl,

ref bool CancelDefault)

incre-d  Monday, September 01, 2008 2:10 PM

Hi

You are correct but Idid change the eventhandler name(_ButtonWorkSheet_Click) to _ExecuteCommand_Click ,and i forgot to mention you in the post .

It still not working my friend.

Thanks

-Das

Dwipayan Das  Tuesday, September 02, 2008 3:29 AM
Hi

I Found a great articile on this and solved the problem for ever.This is a genuine problem occurs in Excel if you want to create more than one CommandBar button and add eventHandlers for the Buttons

The link below.

http://www.angelosp.com/blog/post/Extending-Word-with-VSTO-2005-e28093-Adding-a-toolbar.aspx.

Thanks

__Das
Dwipayan Das  Thursday, September 04, 2008 3:49 PM
Hi,
The link
http://www.angelosp.com/blog/post/Extending-Word-with-VSTO-2005-e28093-Adding-a-toolbar.aspx.
doesnot work anymore, does anybody know where to find this article now.
/Patrik

pla
Patrik Lilja  Sunday, October 18, 2009 5:08 PM

You can use google to search for other answers

Custom Search

More Threads

• insert image
• Word 2007 Add-in Ribbon Question
• Copy & Paste VSTO
• Need Mail Merge Help
• Word Application OnTime() method question
• word document 2007 template
• Possible for an outlook plugin to restart itself??
• MessageNext and MessagePrevious buttons mysteriously disabled in Outlook Inspector
• Sendkeys in VSTO or Collapse groups programmatically
• Accessing Word Document from WebForm