2014-07-11 99 views
1

我有一個Excel加載項Proect下面的代碼在VB.Net:的Excel VSTO,工作簿打開事件不觸發

Public Class ThisAddIn 

    Private Sub Application_WorkbookOpen(Wb As Microsoft.Office.Interop.Excel.Workbook) Handles Application.WorkbookOpen 
     Beep() 
     MsgBox("fad") 
    End Sub 
End Class 

這是由VB編輯器中產生。它是工作簿打開時的事件處理程序。當我按F5並運行代碼時,顯然事件處理程序不會執行。有任何想法嗎?

編輯:如果從打開的工作簿打開工作簿,但不會爲原始工作簿本身運行,則會運行事件處理程序。

回答

1

嗯,你知道在Excel啓動時不叫打開事件,該事件被稱爲只有當你打開現有工作簿。

有一個事件** NewWorkbook *它足夠有趣也不會被解僱......

我找到了一種方法如何處理這個,但不得不說,我只測試1分鐘,試試看並讓我們知道

Public Class ThisAddIn 
    Private Sub ThisAddIn_Startup() Handles Me.Startup 
     AddHandler Globals.ThisAddIn.Application.WorkbookOpen, AddressOf MyWorkbookOpenEvent 
     AddHandler Globals.ThisAddIn.Application.NewWorkbook, AddressOf MyNewWorkbookEvent 
     If Globals.ThisAddIn.Application.Workbooks.Count = 1 Then MyWorkbookOpenEvent(Globals.ThisAddIn.Application.Workbooks(1)) 
    End Sub 

    Private Sub MyWorkbookOpenEvent(ByVal Wb As Microsoft.Office.Interop.Excel.Workbook) 
     System.Windows.Forms.MessageBox.Show("OPEN workbook event") 
    End Sub 

    Private Sub MyNewWorkbookEvent(ByVal Wb As Microsoft.Office.Interop.Excel.Workbook) 
     System.Windows.Forms.MessageBox.Show("NEW Workbook event") 
    End Sub 

    Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown 
     RemoveHandler Globals.ThisAddIn.Application.WorkbookOpen, AddressOf MyWorkbookOpenEvent 
     RemoveHandler Globals.ThisAddIn.Application.NewWorkbook, AddressOf MyNewWorkbookEvent 
    End Sub 
End Class 
相關問題