从命令行或批处理文件运行 Excel 宏的方法? [英] Way to run Excel macros from command line or batch file?

查看:38
本文介绍了从命令行或批处理文件运行 Excel 宏的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Excel VBA 宏,在从批处理文件访问文件时需要运行它,但不是每次打开它时都需要运行(因此不使用打开文件事件).有没有办法从命令行或批处理文件运行宏?我不熟悉这样的命令.

I have an Excel VBA macro which I need to run when accessing the file from a batch file, but not every time I open it (hence not using the open file event). Is there a way to run the macro from the command line or batch file? I'm not familiar with such a command.

假设一个 Windows NT 环境.

Assume a Windows NT environment.

推荐答案

您可以启动 Excel,打开工作簿并从 VBScript 文件运行宏.

You can launch Excel, open the workbook and run the macro from a VBScript file.

将下面的代码复制到记事本中.

Copy the code below into Notepad.

更新MyWorkbook.xls"和MyMacro"参数.

使用 vbs 扩展名保存并运行它.

Save it with a vbs extension and run it.

Option Explicit

On Error Resume Next

ExcelMacroExample

Sub ExcelMacroExample() 

  Dim xlApp 
  Dim xlBook 

  Set xlApp = CreateObject("Excel.Application") 
  Set xlBook = xlApp.Workbooks.Open("C:MyWorkbook.xls", 0, True) 
  xlApp.Run "MyMacro"
  xlApp.Quit 

  Set xlBook = Nothing 
  Set xlApp = Nothing 

End Sub 

运行宏的关键行是:

xlApp.Run "MyMacro"

这篇关于从命令行或批处理文件运行 Excel 宏的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆