简体   繁体   English

使用VBScript打开Excel并运行宏

[英]Open Excel and Run Macro using VBScript

I'm trying to use this VBScript to open a file and run a macro. 我正在尝试使用此VBScript打开文件并运行宏。 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

The script does not open any Excel file, independent of if it being on a server or in my C drive. 该脚本不会打开任何Excel文件,无论它是在服务器上还是在我的C驱动器中。

There's been a fair number of posts on this. 这方面有相当多的帖子 I did some research . 我做了一些研究

I tried: 我试过了:

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

as well as 以及

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

I've tried opening .xls, .xlsm, .xlsb all to no avail. 我试过打开.xls,.xlsm,.xlsb都无济于事。

I am not getting any error messages. 我没有收到任何错误消息。 The cmd console opens and then closes with this cmd控制台打开然后关闭

Microsoft (R) Windows Script Host Version 5.8 Microsoft(R)Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. 版权所有(C)Microsoft Corporation。 All rights reserved. 版权所有。

Thanks to @AlexK. 感谢@AlexK。 Turned out to be a comma in the wrong place. 结果是在错误的地方逗号。

Also thanks to this post changed: 还要感谢这篇帖子改变了:

 xlApp.Run "Module1.MyMacro"

to: 至:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM