2013-04-09 19 views
1

我正在做一些關於excel addIn abc.xlam的工作。 這個addIn在excel addIns中啓用。 我想在Excel中打開一個工作表(新的或現有的)時啓動一個.exe文件。 我想在打開工作簿的事件中在.xlam addIn中編寫此.exe啓動部分。 請告訴我我該怎麼做?什麼是.xlam excel插件的入口點

回答

0

好吧,我做到了! 我只是在現有的宏

Private Sub MyMacro 
MsgBox "HI" 
End Sub 

創建了一個新的功能,然後,我從的ThisWorkbook

Private Sub Workbook_Open() 
Run "MyMacro" 
Exit Sub 
+0

好,很好。如果你將'MsgBox「HI」'移動到'Workbook_Open',你可以從另一個答案中找到我的例子。 :)現在你需要捕捉'NewWorkbook'事件。重新閱讀你的問題,你還需要'Application'對象中的'WorkbookOpen'事件,以便在新的和現有的工作簿打開時運行代碼。工作表可能有類似的事件 - 「WorkbookNewSheet」和「SheetActivate」? – 2013-04-10 11:52:44

1

您需要訪問Excel應用程序的NewWorkbook事件,因此您需要在加載插件時設置對Application對象的引用。

將下面的示例代碼中ThisWorkbook模塊:

Option Explicit '***** Always use Option Explicit! 

Private WithEvents oXl As Application 


Private Sub oXl_NewWorkbook(ByVal Wb As Workbook) 
    '***** Trapping the NewWorkbook event 
    Call MsgBox("It's me again. (" & oXl.Workbooks.Count & ")", vbInformation, "Hi. Again.") 

    '***** Your code here! 
End Sub 


Private Sub Workbook_BeforeClose(Cancel As Boolean) 
    '***** Remove reference to oXL object 
    Set oXl = Nothing 
End Sub 


Private Sub Workbook_Open() 
    '***** Set reference to the current Excel application 
    Set oXl = ThisWorkbook.Application 

    '***** Testing the oXL object 
    Call MsgBox("Hello, there! (" & oXl.Workbooks.Count & ")", vbInformation, "Hi") 
End Sub 
+0

調用此函數我不知道什麼是我錯了,這些事件從未被打到!對不起,我是VBA的新手 – 2013-04-09 10:06:44

+0

您是否加載了插件?我在加載插件後立即得到'Workbook_Open'事件。 NewWorkbook事件也適用。 – 2013-04-09 10:21:10

+0

addIn始終加載。像我不必每次加載它 – 2013-04-09 10:27:25