打开Excel并使用VBScript运行宏

问题描述:

我正在尝试使用此VBScript打开文件并运行宏.理想情况下,我会从我的个人工作簿中打开它,但很乐意在任何地方运行以作为一种折衷方案.

I'm trying to use this VBScript to open a file and run a macro. Ideally I'd open it from my personal workbook, but happy to run from anywhere as a compromise.

Option Explicit

On Error Resume Next

RunExcelMacro

Sub RunExcelMacro()

Dim xlApp
Dim xlBook

If CheckAppOpen("excel.application")  Then
       'MsgBox "App Loaded"
        Set xlApp = GetObject(, "Excel.Application")   
Else
        ' MsgBox "App Not Loaded"
        Set  xlApp = CreateObject(,"Excel.Application")   
End If

xlApp.visible = True
set xlBook = xlApp.Workbooks.Open ("C:\Users\....\PERSONAL.xlsb", 0, True)
xlApp.Run "Module1.My Macro"
xlApp.Quit()

xlBook = Nothing
xlApp = Nothing

End Sub

该脚本不会打开任何Excel文件,而与它在服务器上还是在我的C驱动器中无关.

The script does not open any Excel file, independent of if it being on a server or in my C drive.

有一个公平 帖子的数量.我做了一些

There's been a fair number of posts on this. I did some research.

我尝试过:

Set xlApp = GetObject("Excel.Application")  
Set xlApp = CreateObject("Excel.Application")  

以及

Dim Filename as string
Filename = "C:/....."
set xlBook = xlApp.Workbooks.Open (Filename)

我尝试将.xls,.xlsm和.xlsb全部打开,但无济于事.

I've tried opening .xls, .xlsm, .xlsb all to no avail.

我没有收到任何错误消息. cmd控制台将打开,然后以此关闭

I am not getting any error messages. The cmd console opens and then closes with this

Microsoft(R)Windows脚本主机版本5.8
版权所有(C)Microsoft Corporation.保留所有权利.

Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.

感谢@AlexK.原来是在错误位置的逗号.

Thanks to @AlexK. Turned out to be a comma in the wrong place.

还要感谢此帖子已更改:

 xlApp.Run "Module1.MyMacro"

收件人:

   xlApp.Run xlBook.name & "!Module1.MyMacro"