1

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

4

2 回答 2

1

您需要访问NewWorkbookExcel 应用程序的事件,因此您需要在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
于 2013-04-09T09:53:59.423 回答
0

好的,我做到了!我只是在现有宏中创建了一个新函数

Private Sub MyMacro 
 MsgBox "HI" 
End Sub

然后,我从 ThisWorkbook 调用了这个函数

 Private Sub Workbook_Open()
 Run "MyMacro"
 Exit Sub
于 2013-04-10T07:43:52.303 回答