2

您好我正在尝试使用以下 VB 脚本在 test.xls 中调用宏

Option Explicit

Dim returnVal 
returnVal = 0
WScript.Echo returnVal

Dim xlApp, xlBook

Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("D:\test.xls", 0, True)
xlApp.Run "macro1"
xlBook.Close false
xlApp.Quit

Set xlBook = Nothing
Set xlApp = Nothing

WScript.Echo returnVal
''WScript.Quit returnVal

test.xls 中定义的宏是:

Dim returnVal as Boolean
sub macro1()
   returnVal = 1
   Exit Sub
...
End Sub

当我尝试运行 VB 脚本时,我得到一个值为 0 的弹出窗口(这是我在开始时所做的回声)。然后我再次得到一个值为 0 的弹出窗口。看起来宏中的值没有被返回。

我在哪里错了。

谢谢,莫妮卡

4

1 回答 1

2

将宏定义为函数:

Function macro1()
    macro1 = 1
End Function

然后获取值:

returnVal = xlApp.Run("macro1", "My Application")

在此处查找更多示例:http:
//support.microsoft.com/kb/306682

于 2013-02-19T09:21:38.667 回答