From: Leviatano on


"Nick Biggs" wrote:

> And here is the fixed code...
>
> Question is this... should the Excel10EventHelper class that is inheriting from IExcelAppEvents10 also be inheriting the interfaces [DispId] attributes or not ?
>
> Nick
>
>
> using System;
> using System.Runtime.InteropServices;
> using Excel=Microsoft.Office.Interop.Excel;
>
> namespace ExcelAppEvents10
> {
> [InterfaceType(ComInterfaceType.InterfaceIsIDispatch),
> GuidAttribute("00024413-0000-0000-C000-000000000046")]
> public interface IExcelAppEvents10
> {
> [DispId(0x0000061d)] void NewWorkbook(Excel.Workbook Wb);
> [DispId(0x00000616)] void SheetSelectionChange(object sh, Excel.Range Target);
> [DispId(0x00000617)] void SheetBeforeDoubleClick(object sh, Excel.Range Target, ref bool Cancel);
> [DispId(0x00000618)] void SheetBeforeRightClick(object sh, Excel.Range Target, ref bool Cancel);
> [DispId(0x00000619)] void SheetActivate(object sh);
> [DispId(0x0000061a)] void SheetDeactivate(object sh);
> [DispId(0x0000061b)] void SheetCalculate(object sh);
> [DispId(0x0000061c)] void SheetChange(object sh, Excel.Range Target);
> [DispId(0x0000061f)] void WorkbookOpen(Excel.Workbook Wb);
> [DispId(0x00000620)] void WorkbookActivate(Excel.Workbook Wb);
> [DispId(0x00000621)] void WorkbookDeactivate(Excel.Workbook Wb);
> [DispId(0x00000622)] void WorkbookBeforeClose(Excel.Workbook Wb, ref bool Cancel);
> [DispId(0x00000623)] void WorkbookBeforeSave(Excel.Workbook Wb, bool SaveAsUI, ref bool Cancel);
> [DispId(0x00000624)] void WorkbookBeforePrint(Excel.Workbook Wb, ref bool Cancel);
> [DispId(0x00000625)] void WorkbookNewSheet(Excel.Workbook Wb, object sh);
> [DispId(0x00000626)] void WorkbookAddinInstall(Excel.Workbook Wb);
> [DispId(0x00000627)] void WorkbookAddinUninstall(Excel.Workbook Wb);
> [DispId(0x00000612)] void WindowResize(Excel.Workbook Wb, Excel.Window Wn);
> [DispId(0x00000614)] void WindowActivate(Excel.Workbook Wb, Excel.Window Wn);
> [DispId(0x00000615)] void WindowDeactivate(Excel.Workbook Wb, Excel.Window Wn);
> [DispId(0x0000073e)] void SheetFollowHyperlink(object sh, Excel.Hyperlink Target);
> [DispId(0x0000086d)] void SheetPivotTableUpdate(object sh, Excel.PivotTable Target);
> [DispId(0x00000870)] void WorkbookPivotTableCloseConnection(Excel.Workbook Wb, Excel.PivotTable Target);
> [DispId(0x00000871)] void WorkbookPivotTableOpenConnection(Excel.Workbook Wb, Excel.PivotTable Target);
> }
>
> public class Excel10EventHelper : IExcelAppEvents10, IDisposable
> {
> public Excel10EventHelper()
> {
> m_oConnectionPoint = null;
> m_Cookie = 0;
> }
>
> [DispId(0x0000061d)]
> public void NewWorkbook(Excel.Workbook Wb)
> {System.Diagnostics.Debug.WriteLine("NewWorkbook");}
>
> [DispId(0x00000616)]
> public void SheetSelectionChange(object sh, Excel.Range Target)
> {System.Diagnostics.Debug.WriteLine("SheetSelectionChange");}
>
> [DispId(0x00000617)]
> public void SheetBeforeDoubleClick(object sh, Excel.Range Target, ref bool Cancel)
> {System.Diagnostics.Debug.WriteLine("SheetBeforeDoubleClick");}
>
> [DispId(0x00000618)]
> public void SheetBeforeRightClick(object sh, Excel.Range Target, ref bool Cancel)
> {
> System.Diagnostics.Debug.WriteLine("SheetBeforeRightClick");
> Cancel = true; // Cancel the right click!
> }
>
> [DispId(0x00000619)]
> public void SheetActivate(object sh)
> {System.Diagnostics.Debug.WriteLine("SheetActivate");}
>
> [DispId(0x0000061a)]
> public void SheetDeactivate(object sh)
> {System.Diagnostics.Debug.WriteLine("SheetDeactivate");}
>
> [DispId(0x0000061b)]
> public void SheetCalculate(object sh)
> {System.Diagnostics.Debug.WriteLine("SheetCalculate");}
>
> [DispId(0x0000061c)]
> public void SheetChange(object sh, Excel.Range Target)
> {System.Diagnostics.Debug.WriteLine("SheetChange");}
>
> [DispId(0x0000061f)]
> public void WorkbookOpen(Excel.Workbook Wb)
> {System.Diagnostics.Debug.WriteLine("WorkbookOpen");}
>
> [DispId(0x00000620)]
> public void WorkbookActivate(Excel.Workbook Wb)
> {System.Diagnostics.Debug.WriteLine("WorkbookActivate");}
>
> [DispId(0x00000621)]
> public void WorkbookDeactivate(Excel.Workbook Wb)
> {System.Diagnostics.Debug.WriteLine("WorkbookDeactivate");}
>
> [DispId(0x00000622)]
> public void WorkbookBeforeClose(Excel.Workbook Wb, ref bool Cancel)
> {System.Diagnostics.Debug.WriteLine("WorkbookBeforeClose");}
>
> [DispId(0x00000623)]
> public void WorkbookBeforeSave(Excel.Workbook Wb, bool SaveAsUI, ref bool Cancel)
> {System.Diagnostics.Debug.WriteLine("WorkbookBeforeSave");}
>
> [DispId(0x00000624)]
> public void WorkbookBeforePrint(Excel.Workbook Wb, ref bool Cancel)
> {System.Diagnostics.Debug.WriteLine("WorkbookBeforePrint");}
>
> [DispId(0x00000625)]
> public void WorkbookNewSheet(Excel.Workbook Wb, object sh)
> {System.Diagnostics.Debug.WriteLine("WorkbookNewSheet");}
>
> [DispId(0x00000626)]
> public void WorkbookAddinInstall(Excel.Workbook Wb)
> {System.Diagnostics.Debug.WriteLine("WorkbookAddinInstall");}
>
> [DispId(0x00000627)]
> public void WorkbookAddinUninstall(Excel.Workbook Wb)
> {System.Diagnostics.Debug.WriteLine("WorkbookAddinUninstall");}
>
> [DispId(0x00000612)]
> public void WindowResize(Excel.Workbook Wb, Excel.Window Wn)
> {System.Diagnostics.Debug.WriteLine("WindowResize");}
>
> [DispId(0x00000614)]
> public void WindowActivate(Excel.Workbook Wb, Excel.Window Wn)
> {System.Diagnostics.Debug.WriteLine("WindowActivate");}
>
> [DispId(0x00000615)]
> public void WindowDeactivate(Excel.Workbook Wb, Excel.Window Wn)
> {System.Diagnostics.Debug.WriteLine("WindowDeactivate");}
>
> [DispId(0x0000073e)]
> public void SheetFollowHyperlink(object sh, Excel.Hyperlink Target)
> {System.Diagnostics.Debug.WriteLine("SheetFollowHyperlink");}
>
> [DispId(0x0000086d)]
> public void SheetPivotTableUpdate(object sh, Excel.PivotTable Target)
> {System.Diagnostics.Debug.WriteLine("SheetPivotTableUpdate");}
>
> [DispId(0x00000870)]
> public void WorkbookPivotTableCloseConnection(Excel.Workbook Wb, Excel.PivotTable Target)
> {System.Diagnostics.Debug.WriteLine("WorkbookPivotTableCloseConnection");}
>
> [DispId(0x00000871)]
> public void WorkbookPivotTableOpenConnection(Excel.Workbook Wb, Excel.PivotTable Target)
> {System.Diagnostics.Debug.WriteLine("WorkbookPivotTableOpenConnection");}
>
> private UCOMIConnectionPoint m_oConnectionPoint;
> private int m_Cookie;
> private Excel.ApplicationClass m_xlApp;
>
> public void SetupConnection(Excel.ApplicationClass app)
> {
> if (m_Cookie != 0) return;
>
> // QI for IConnectionPointContainer.
> UCOMIConnectionPointContainer oConnPointContainer = (UCOMIConnectionPointContainer) app;
>
> // GUID of the Excel AppEvents dispinterface.
> Guid guid = new Guid("{00024413-0000-0000-C000-000000000046}");
> // Guid guid = typeof(Excel.AppEvents).GUID; also works!
>
> // Find the connection point.
> oConnPointContainer.FindConnectionPoint(ref guid, out m_oConnectionPoint);
>
> // Call Advise to sink up the connection.
> m_oConnectionPoint.Advise(this, out m_Cookie);
> }
>
> public void RemoveConnection()
> {
> if (m_Cookie != 0)
> {
> m_oConnectionPoint.Unadvise(m_Cookie);
> m_oConnectionPoint = null;
> m_Cookie = 0;
> }
> }
>
> public void Dispose(){RemoveConnection();}
> }
> }
From: Leviatano on


"Nick Biggs" wrote:

> Oh but you can! It is done through the Excel application events. Specifically you simply set the "Cancel" parameter to "True" inside the SheetBeforeRightClick event handler. I can do this from VBA in an XLS workbook and also from VB6 via Ole Automation.
>
> My problem is that it does not work from C#. KB Article 830519 has something to say but it does not appear to work for Excel. Check out the post by Chris Peacock titled "Problem implementing connection point sink" in this newsgroup for a further discussion and some dodgy code by my good self!
>
> "AA2e72E" wrote:
>
> > I don't believe you can disable the right click on the activesheet. If you set the AllowPropertyToolbox to false, it disables the Property Toolbox on the right click menu. There is no other property that can be set. The list of properties is:
> >
> > ActiveCell ActivePane ActiveSheet
> > AllowPropertyToolbox AutoFit BuildNumber
> > CSVData CSVURL CanUndo
> > Cells Columns Constants
> > DataType Dirty DisplayColHeaders
> > DisplayGridlines DisplayHorizontalScrollBar DisplayPropertyToolbox
> > DisplayRowHeaders DisplayTitleBar DisplayToolbar
> > DisplayVerticalScrollBar EnableAutoCalculate EnableEvents
> > EnableUndo HTMLData HTMLURL
> > MajorVersion MaxHeight MaxWidth
> > MinorVersion MoveAfterReturn MoveAfterReturnDirection
> > Range RevisionNumber RightToLeft
> > Rows ScreenUpdating Selection
> > TitleBar Version ViewableRange
> >
> > I can't see that there is a right click event.The list of events is:
> >
> > BeforeCommand Calculate CancelEdit
> > Change Click Command
> > DblClick EndEdit KeyDown
> > KeyPress KeyUp MouseDown
> > MouseOut MouseOver MouseUp
> > SelectionChange SelectionChanging StartEdit
> > ViewChange
> >
> > Like you, I think the OWC PIA is screwed up. Can you help with my post "OWC Web Component Spreadsheet: C#" in this news group?
> > "Nick Biggs" wrote:
> >
> > > I am trying to use Excel from C# and need to disable the menu that appears when clicking the right mouse button on a cell.
> > >
> > > I am trying to do this using the Excel.AppEvents_SheetBeforeRightClickEventHandler from my C# WinForm application.
> > >
> > > I have a MessageBox that pops up that proves my event handler actually fires via my delegate but after setting the "Cancel" variable to "true" it has no effect for me. The Excel RMB menu appears when it should not. The Cancel parameter is declared as "ref bool".
> > >
> > > I am wondering if there is a problem with the marshalling of this variable in the Office PIA which I am using. I would be happy to send code that demos my problem. Can someone please let me know if they can make it work ?
> > >
> > > By the way I have already written some VB6 code that works fine and VBA code in an Excel Workbook that also works just fine as well. Both disable the menu as I require when I set "Cancel" to "True". The problem is that I need to do this from a C# application.
> > >
> > > Can anyone shed some light on this at all ?
> > >
> > > I am using...
> > >
> > > Microsoft Excel 2002 (10.4302.4219) SP-2
> > > Visual Studio 2003 v7.1.3088
> > > Microsoft .NET Framework 1.1 v1.1.4322
> > >
> > > I obtained the Office XP Primary Interop Assemblies from the following link
> > >
> > > http://www.microsoft.com/downloads/details.aspx?familyid=c41bd61e-3060-4f71-a6b4-01feba508e52&displaylang=en
> > >
> > >
From: Leviatano on


"Nick Biggs" wrote:

> I am trying to use Excel from C# and need to disable the menu that appears when clicking the right mouse button on a cell.
>
> I am trying to do this using the Excel.AppEvents_SheetBeforeRightClickEventHandler from my C# WinForm application.
>
> I have a MessageBox that pops up that proves my event handler actually fires via my delegate but after setting the "Cancel" variable to "true" it has no effect for me. The Excel RMB menu appears when it should not. The Cancel parameter is declared as "ref bool".
>
> I am wondering if there is a problem with the marshalling of this variable in the Office PIA which I am using. I would be happy to send code that demos my problem. Can someone please let me know if they can make it work ?
>
> By the way I have already written some VB6 code that works fine and VBA code in an Excel Workbook that also works just fine as well. Both disable the menu as I require when I set "Cancel" to "True". The problem is that I need to do this from a C# application.
>
> Can anyone shed some light on this at all ?
>
> I am using...
>
> Microsoft Excel 2002 (10.4302.4219) SP-2
> Visual Studio 2003 v7.1.3088
> Microsoft .NET Framework 1.1 v1.1.4322
>
> I obtained the Office XP Primary Interop Assemblies from the following link
>
> http://www.microsoft.com/downloads/details.aspx?familyid=c41bd61e-3060-4f71-a6b4-01feba508e52&displaylang=en
>
>
From: Leviatano on


""Peter Huang"" wrote:

> Hi Nick,
>
> Based on my research, we need to specified the attribute as below.
> [ClassInterface(ClassInterfaceType.None)]
> public class Excel10EventHelper : IExcelAppEvents10, IDisposable
>
> So that we can guarantee that there is no default interface generate for
> Excel10EventHelper class.
> You may have a try and let me know the result.
>
> Best regards,
>
> Peter Huang
> Microsoft Online Partner Support
>
> Get Secure! - www.microsoft.com/security
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>
From: Leviatano on


""Peter Huang"" wrote:

> Hi Nick,
>
> Now I am researching the issue, and I will get back here and update you
> with new information ASAP.
>
> Best regards,
>
> Peter Huang
> Microsoft Online Partner Support
>
> Get Secure! - www.microsoft.com/security
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>