我有两个文件。
我试图在example.wsf中调用Macro。
example.wsf代码:
<package>
<job id="example">
<script language="vbscript">
Set objApp = CreateObject("Excel.Application")
Set objExcel = objApp.Workbooks.Open("example.xlsm",0,FALSE)
objApp.Visible = True
objApp.DisplayAlerts = False
objApp.Run("RefreshData")
msgbox "closing"
objExcel.Close
objApp.Quit
Set objExcel = Nothing
Set objApp = Nothing
set objShell = Nothing
</script>
</job>
</package>
example.xlsm中的Excel宏代码:
Public Sub RefreshData()
Dim API As New EXTERNAL_API
Dim varResult As Variant
Dim vSheet As String
Dim LastRow, LastCol, vResult
vSheet = "D"
ActiveWorkbook.Sheets(vSheet).Select
Range("A6").Select
Selection.Copy
Range(Selection, Selection.End(xlDown)).Select
LastRow = ActiveWorkbook.Sheets(vSheet).Cells(8, 1).End(xlDown).Row
LastCol = 18
API.ActivateAPI
varResult = API.RunApplication("OfficeAPI", "application=excel,method=RefreshAll")
MsgBox (varResult)
End Sub
如果在Excel工作簿中触发宏,则它本身运行良好。
但是当从VBScript调用宏时,EXTERNAL_API无法执行,即,以下行不会执行:
varResult = API.RunApplication("OfficeAPI", "application=excel,method=RefreshAll")
我怀疑这可能与早期绑定有关,但基本上不知道出了什么问题。
答案 0 :(得分:0)
看起来你几乎就在那里。只是一些格式问题。请参阅下面的代码段。请注意我对文件路径的注释以及宏在excel文件中的位置。让我知道它是否适合你。
Set objApp = CreateObject("Excel.Application")
Set objExcel = objApp.Workbooks.Open("example.xlsm", 0, False) ''make sure you have valid path, such as a full UNC path
objApp.Visible = True
objApp.DisplayAlerts = False
objApp.Run "example.xlsm!RefreshData" ' this is if the Macro is in a Module. Use "example.xlsm!sheet1.RefreshData" if it's on
' sheet1, for example