views:

249

answers:

1

I'm trying to write a COM add in for Excel in C# that disables the ability to double click a cell and then edit it, I want to popup a box saying that editing a cell this way is prohibited and then stop all execution.

Looking through the documentation at Microsoft this seems like a very easy task, you create an Application event AppEvents_SheetBeforeDoubleClickEventHandler with the signature func(object sheet, Range Target, ref bool Cancel) and you set Cancel to true so that execution stops. However, this isn't happening.

I have the following:

public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)
{
    Excel.Application app = application as Excel.Application;
    app.SheetBeforeDoubleClick += Excel.AppEvents_SheetBeforeDoubleClickEventHandler(beforeDoubleClick);
}

void beforeDoubleClick(object sheet, Excel.Range Target, ref bool Cancel)
{
    MessageBox.Show("Cannot edit cells this way sorry.");
    Cancel = true;
}

The message box is shown, but the cell is then put into edit mode, however, if I have the same thing from VBA it works.

The exact same thing happens with a VB.NET addin using the following code.

Private WithEvents app As Excel.Application
Public Sub OnConnection(ByVal application As Object, 
                            ByVal connectMode As Extensibility.ext_ConnectMode, 
                            ByVal addInInst As Object, 
                            ByRef custom As System.Array) 
                            Implements Extensibility.IDTExtensibility2.OnConnection
    app = application
End Sub

Sub Worksheet1_BeforeDoubleClick(ByVal obj As Object, 
                                 ByVal Target As Excel.Range, 
                                 ByRef Cancel As Boolean) 
                                 Handles app.SheetBeforeDoubleClick
    MsgBox("Double-clicking in this sheet is not allowed.")
    Cancel = True
End Sub

I'm using Excel XP :(

+1  A: 

Hey PintSizedCat,

I know this question is a month old, and you've almost certainly moved on from this (I hope!), but I decided to take a look at it.

I had no problems executing your code: the 'Cancel' behavior worked properly and correctly prevented the double-click operation.

Some searching turned up the following from MSKB: BUG: Cancel parameter for Office events is ignored in Visual Studio .NET 2003.

This behavior of ignoring the cancel parameter seems to exist for Excel versions 2002 and below when using .NET code created using Visual Studio .NET 2003 (.NET Framework 1.1).

There is a complex work-around given in the article that involves hand-tuning the Interop Assembly. It looks pretty doable, though, if a fair amount of effort.

It would be easier to upgrade to VS 2005 or VS 2008, if that is at all possible for you. I compiled my code using VS 2008 targeting .NET 3.5, and ran my code on Excel 2007 with no troubles.

By the way, even if you get this running, the user will still be able to enter a value into the cell by typing the value within the Formula Bar, executing a Copy-Paste command, or the like. So I am not sure that this is a great way to try to prevent the user from entering a value into a cell. Perhaps you might want to look into protecting the worksheet instead?

Anyway, I know that this is very late, but I hope that it is of some value or help.

Mike

Mike Rosenblum
Hey, just wanted to say thank you for putting in the time. I've been working on a project recently that I need this kind of functionality for and actually I compile with VS 2008 using .NEt 3.5 but for Excel 2002. I'll try the workaround. Thanks again.
PintSizedCat
According to that article, you should be ok with VS 2008. Hmmm... Ok, you might want to check out http://support.microsoft.com/kb/948461. It states that an error should be thrown, but, if not, then .NET 1.1 might just quietly run, and therefore kick in the 'ignore the cancel event' behavior'?
Mike Rosenblum