' using events in Excel
' taken from Example08.vb
'
Private Sub Example08Main()

 ' start excel and turn off msg boxes
 Dim excelApplication As New Excel.Application()
 excelApplication.DisplayAlerts = False

 ' we register some events. note: the event trigger was called from excel,  
' means an other Thread. you can get event notifys from various objects:
' Application or Workbook or Worksheet for example Dim newWorkbookHandler As Excel.Application_NewWorkbookEventHandler = _
AddressOf Me.excelApplication_NewWorkbook AddHandler excelApplication.NewWorkbookEvent, newWorkbookHandler Dim beforeCloseHandler As Excel.Application_WorkbookBeforeCloseEventHandler = _
AddressOf Me.excelApplication_WorkbookBeforeClose AddHandler excelApplication.WorkbookBeforeCloseEvent, beforeCloseHandler Dim workbookActivateHandler As Excel.Application_WorkbookActivateEventHandler = _
AddressOf Me.excelApplication_WorkbookActivate AddHandler excelApplication.WorkbookActivateEvent, workbookActivateHandler Dim workbookDeactivateHandler As Excel.Application_WorkbookDeactivateEventHandler = _
AddressOf Me.excelApplication_WorkbookDeactivate AddHandler excelApplication.WorkbookDeactivateEvent, workbookDeactivateHandler Dim sheetActivateHandler As Excel.Application_SheetActivateEventHandler = _
AddressOf Me.excelApplication_SheetActivateEvent AddHandler excelApplication.SheetActivateEvent, sheetActivateHandler Dim sheetDeactivateHandler As Excel.Application_SheetDeactivateEventHandler = _
AddressOf Me.excelApplication_SheetDeactivateEvent AddHandler excelApplication.SheetDeactivateEvent, sheetDeactivateHandler ' add a new workbook add a sheet and close Dim workBook As Excel.Workbook = excelApplication.Workbooks.Add() Dim workSheet As Excel.Worksheet = workBook.Worksheets(1) workBook.Close() excelApplication.Quit() excelApplication.Dispose()End Sub Private Sub excelApplication_SheetDeactivateEvent(ByVal Sh As COMObject) textBoxEvents.BeginInvoke(_updateDelegate, _
New Object() {"Event SheetDeactivate called."}) Sh.Dispose() End Sub Private Sub excelApplication_SheetActivateEvent(ByVal Sh As COMObject) textBoxEvents.BeginInvoke(_updateDelegate, _
New Object() {"Event SheetActivate called."}) Sh.Dispose() End Sub Private Sub excelApplication_NewWorkbook(ByVal Wb As Excel.Workbook) textBoxEvents.BeginInvoke(_updateDelegate, _
New Object() {"Event NewWorkbook called."}) Wb.Dispose() End Sub Private Sub excelApplication_WorkbookBeforeClose(ByVal Wb As Excel.Workbook, _
ByRef Cancel As Boolean) textBoxEvents.BeginInvoke(_updateDelegate, _
New Object() {"Event WorkbookBeforeClose called."}) Wb.Dispose() End Sub Private Sub excelApplication_WorkbookActivate(ByVal Wb As Excel.Workbook) textBoxEvents.BeginInvoke(_updateDelegate, _
New Object() {"Event WorkbookActivate called."}) Wb.Dispose() End Sub Private Sub excelApplication_WorkbookDeactivate(ByVal Wb As Excel.Workbook) textBoxEvents.BeginInvoke(_updateDelegate, _
New Object() {"Event WorkbookDeactivate called."}) Wb.Dispose() End Sub Private Sub UpdateTextbox(ByVal message As String) textBoxEvents.AppendText(message & vbNewLine) End Sub

Last edited May 14, 2012 at 4:54 AM by SebastianDotNet, version 9

Comments

No comments yet.