.xlam excel插件的切入点是什么?

时间:2013-04-09 09:28:54

标签: excel vba excel-vba excel-2010

我在excel addIn abc.xlam上做了一些工作。 这个addIn在excel addIns中启用。 每当我在excel中打开工作表(新的或现有的)时,我想启动一个.exe文件。 我想在打开工作簿的事件中在.xlam addIn中编写此.exe启动部分。 请告诉我怎么办?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

中创建了一个新函数
Private Sub MyMacro 
 MsgBox "HI" 
End Sub

然后,我从ThisWorkbook

调用了这个函数
 Private Sub Workbook_Open()
 Run "MyMacro"
 Exit Sub