Commit change when clicking an add-in button

O

Olivier B.

Hi,

I have an add-in button bar, and I want to retrieve the data of the active
cell when I clic on one of the buttons.

Here is the code :
********************************************************
//Gets a range object for the active cell
object activeCell = Application.GetProperty("ActiveCell");

string cellValue
=activeCell.GetType().InvokeMember("Value",BindingFlags.GetProperty,null,activeCell,new object[0]).ToString();

MessageBox.Show(cellValue);
********************************************************

It works fine as long as the user isn't editing the cell. If the cell
content is "AB" and that the user types "ABCDE" without commiting and clics
on the button, I get "AB" whereas I want to get "ABCDE".

What I would like to do is to programaticaly commit the cell change.

I tried to select an other cell and activate it, but the first one keeps the
edition cursor, and when I press Enter the datas are validated in the cell I
have activated.

**********************************************************
//Selects next cell
object nextCell =
activeCell.GetType().InvokeMember("Next",BindingFlags.GetProperty,null,activeCell,new object[0]);

//Activates the cell
nextCell.GetType().InvokeMember("Activate",BindingFlags.InvokeMethod,null,nextCell,new object[0]);
*********************************************************

I also tried to use SendKeys and passing the Enter value, but all I get is a
new line in the currently edited cell as if I had typed [ALT]+[ENTER] ...

***********************************************************
Application.SendKeys("~",false);
System.Windows.Forms.Application.DoEvents();
***********************************************************

Does anyone have an other idea ?

Thanks.
Olivier
 
O

Olivier B.

I finally worked around that problem by detecting that the user is editing a
cell and asking him to validate before using the button.

There is no easy way to detect that a cell is in edit mode. When you edit a
cell the standard menu bars are disabled, so you have to detect that
behaviour to know that the user is currently editing a cell.

Here is my code :
//Gets the "new file" menu item .
object fileMenu = Application.CommandBars[1].Controls[1];
object newFileMenu =
fileMenu.GetType().InvokeMember("Controls",BindingFlags.GetProperty,null,fileMenu,new object[] {1});

//Checks if the "New file" menu is enabled. When a cell is in "edit mode"
then that menu is disabled.
//There seems to be no other way to detect edit mode.
bool isEnabled =(bool)
newFileMenu.GetType().InvokeMember("Enabled",BindingFlags.GetProperty,null,newFileMenu,new object[0]);

if (isEnabled)
{
//Do something ...
}
else
{
//Show message : Please, commit your changes before using the button.
MessageBox.Show(mConfig.Localization.GetString("excelplugin.commitchanges"));
}

Olivier
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top