什么打开了我的Excel工作簿?

时间:2013-02-25 15:40:20

标签: excel excel-vba vbscript automation vba

我正在编写大量VBScript来自动处理Excel工作簿。

这些工作簿包含在工作簿打开时运行的VBA代码。当用户打开它并且我的脚本打开它时,我需要VBA表现出不同的行为。

有谁知道如何区分这两种情况? (我无法编辑VBS和VBA)

1 个答案:

答案 0 :(得分:1)

Sub Test()
'This is VBA but should easily convert to vbscript...
Dim wb As Workbook

    Application.EnableEvents = False
    Set wb = Workbooks.Open("C:\local files\tester.xlsm")
    Application.EnableEvents = True

    Application.Run "'" & wb.Name & "'!ThisWorkBook.IsAutomated"
    Application.Run "'" & wb.Name & "'!ThisWorkBook.Workbook_Open"

End Sub

在“tester”工作簿(ThisWorkBook代码模块)中:

Private m_automated As Boolean

Public Sub IsAutomated()
    m_automated = True
End Sub


Public Sub Workbook_Open()

    If m_automated Then
        MsgBox "automated"
    Else
        MsgBox "not automated"
    End If

End Sub