WorkbookEvents_SheetCalculateEventHandler

1,341 views
Skip to first unread message

RS DID

unread,
Nov 16, 2010, 10:33:01 AM11/16/10
to Excel-Dna
Hi,
I am trying to implement the WorkbookEvents_SheetCalculateEventHandler
to perform some actions when the excel workbook gets a calculation
event. I am having little successin getting this to work. Does
anyone have a simple example of how to implement this?

private void WorkbookSheetCalculate()
{
Excel.WorkbookEvents_SheetCalculateEventHandler cc = new
Excel.WorkbookEvents_SheetCalculateEventHandler(ThisWorkbook_SheetCalculate);

}

void ThisWorkbook_SheetCalculate(object Sh)
{
MessageBox.Show("Code has been called");
}


Thanks in advance.

RS DID

unread,
Nov 16, 2010, 1:08:08 PM11/16/10
to Excel-Dna
I discovered my problem.  I simply needed to assign the Eventhandler to the workbook SheetCalculate


--
You received this message because you are subscribed to the Google Groups "Excel-Dna" group.
To post to this group, send email to exce...@googlegroups.com.
To unsubscribe from this group, send email to exceldna+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/exceldna?hl=en.


Govert van Drimmelen

unread,
Nov 16, 2010, 1:31:12 PM11/16/10
to Excel-Dna
Hi Rob,

There is also an OnRecalc event in the C API. Below I paste an add-in
that shows how it can be used.
I'm not sure it's easier or better than the COM event, but one
advantage is that you wouldn't need the interop assembly. (I remember
you asked about xlcOnKey before.)
I haven't quite figured out how to handle COM events late-bound (it is
possible, but needs some connection point set-up).

Cheers,
Govert


<DnaLibrary Name="Test OnRecalc" Language="VB">
<Reference Path="System.Windows.Forms.dll" />
<![CDATA[
Imports System.Windows.Forms

Public Module Test
Public Sub ShowRecalc()
MessageBox.Show("Recalculating...", "Test OnRecalc")
End Sub

Public Sub ShowRecalcSheet()
MessageBox.Show("Recalculating a sheet ...", "Test OnRecalc")
End Sub

<ExcelCommand(MenuText:="Install OnRecalc Handler")> _
Public Sub InstallOnRecalc()
XlCall.Excel(XlCall.xlcOnRecalc, Nothing, "ShowRecalc")
MessageBox.Show("OnRecalc handler installed.", "Test
OnRecalc")
End Sub

<ExcelCommand(MenuText:="Uninstall OnRecalc Handler")> _
Public Sub UninstallOnRecalc()
XlCall.Excel(XlCall.xlcOnRecalc, Nothing, Nothing)
MessageBox.Show("OnRecalc handler uninstalled.", "Test
OnRecalc")
End Sub

<ExcelCommand(MenuText:="Install OnRecalc Handler for Single
Worksheet")> _
Public Sub InstallOnRecalcForSheet()

Dim SheetName As String
Dim InputType_Text As Integer = 2

SheetName = XlCall.Excel(XlCall.xlfInput, "Which sheet should
be watched? " & vbCrLf & "(In the format [Book1.xls]Sheet2)",
InputType_Text, "Test OnRecalc")
' TODO: Check the SheetName is valid
XlCall.Excel(XlCall.xlcOnRecalc, SheetName, "ShowRecalcSheet")
MessageBox.Show("OnRecalc handler installed.", "Test
OnRecalc")
End Sub

<ExcelCommand(MenuText:="Uninstall OnRecalc Handler for Single
Worksheet")> _
Public Sub UninstallOnRecalcForSheet()

Dim SheetName As String
Dim InputType_Text As Integer = 2

SheetName = XlCall.Excel(XlCall.xlfInput, "Which sheet should
be cleared? " & vbCrLf & "(In the format [Book1.xls]Sheet2)",
InputType_Text, "Test OnRecalc")
XlCall.Excel(XlCall.xlcOnRecalc, SheetName, Nothing)
MessageBox.Show("OnRecalc handler uninstalled.", "Test
OnRecalc")
End Sub
End Module

]]>
</DnaLibrary>
> > exceldna+u...@googlegroups.com<exceldna%2Bunsubscribe@googlegroups.c om>
> > .

RS DID

unread,
Nov 16, 2010, 1:52:54 PM11/16/10
to exce...@googlegroups.com
Thanks for your reply..  Like a few other post in ExcelDna, I am trying to accomplish writing a matrix of data to excel via one function call, avoiding the formula array mechanism.  I do not know the size of the matrix until the function is done retrieving the results from its source.   I have tried the timers, I have tried using the xlcOnKey, but that has been deemed an unacceptable solution, not by me.  So now I am trying to accomplish this by using the workbook calc event.

If I get a decent solution to solve this problem I will post it, since I see many who would like to know how to solve this problem.  This is what my callback looks like so far, it's working, but needs some robustness built in..  I have to do a lot more testing, I just thought I would post for other to see.  Again, not sure this is a viable solution yet.

          Excel.Application xlApp = ExcelDnaUtil.Application as Excel.Application;

            Excel.Workbook wb = xlApp.ActiveWorkbook;

            Excel.Sheets sheets = wb.Worksheets;

            Excel.Worksheet ws = wb.ActiveSheet as Excel.Worksheet;

            Excel.Range rng = ws.Cells[refRng.RowFirst + 1, refRng.ColumnFirst + 5] as Excel.Range;
            Excel.Range oRng = rng.get_Resize(recCnt, 5);
            oRng.set_Value(Excel.XlRangeValueDataType.xlRangeValueDefault, theRecs);


Thanks again

To unsubscribe from this group, send email to exceldna+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages