[英]Run Excel Macro from external VBScript Error
I have two files. 我有两个文件。
I am trying to call the Macro inside the example.wsf. 我试图在example.wsf内部调用宏。
The example.wsf code: 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>
The Excel Macro code in example.xlsm: 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
The Macro itself is working well if it is triggered inside the Excel workbook. 如果宏本身在Excel工作簿中触发,则宏本身将运行良好。
But when the Macro is called from the VBScript, the EXTERNAL_API fail to execute, ie, the following line does not execute: 但是,当从VBScript调用宏时,EXTERNAL_API无法执行,即,以下行不执行:
varResult = API.RunApplication("OfficeAPI", "application=excel,method=RefreshAll")
I am suspecting that this may be related to early binding but basically have no clue what went wrong. 我怀疑这可能与早期绑定有关,但基本上不知道出了什么问题。
Looks like you are almost there. 看来您快要到了。 Just some formatting issues.
只是一些格式问题。 See the snippet below.
请参见下面的代码段。 Notice my comments about the file path and where the macro is located within the excel file.
请注意我对文件路径以及宏在excel文件中的位置的评论。 Let me know if it works for you.
请让我知道这对你有没有用。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.