【问题标题】:How to open and run an excel macro using a batch file如何使用批处理文件打开和运行 Excel 宏
【发布时间】:2012-05-24 04:17:13
【问题描述】:

我有一个批处理文件,它可以做几件事,最后一步是打开一个 Excel 文档并运行其中包含的宏来更新文件的内容。只需单击一个按钮即可完美运行宏,但我希望在您运行 .bat 文件时完成所有操作。

我知道我可以将宏附加到 open 事件中,以便在您打开宏时运行,但我只希望它在您运行 bat 文件时自动更新,而不是每次打开时自动更新。

也许我可以传递一个参数让它知道它是从 .bat 运行的?还是直接用excel命令运行?

like this?
run excel.exe /runMacro "mymacro"   

我在任何地方都找不到我需要的东西,谢谢。

【问题讨论】:

    标签: macros batch-file command-line-arguments


    【解决方案1】:

    在我的 Excel 2013 版本(15.0.4649.1000 64 位)上,我不得不编写以下代码:

    #If VBA7 Then
     Private Declare PtrSafe Function GetCommandLine Lib "kernel32" Alias "GetCommandLineW" () As LongPtr
     Private Declare PtrSafe Function lstrlenW Lib "kernel32" (ByVal lpString As LongPtr) As Long
     Private Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (MyDest As Any, MySource As Any, ByVal MySize As LongPtr)
    #Else
    ' Declare Function GetCommandLine Lib "kernel32" Alias "GetCommandLineW" () As Long
    ' Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As Long
    ' Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (MyDest As Any, MySource As Any, ByVal MySize As Long)
    #End If
    
    
    #If VBA7 Then
     Function CmdToSTr(Cmd As LongPtr) As String
    #Else
     Function CmdToSTr(Cmd As Long) As String
    #End If
     Dim Buffer() As Byte
     Dim StrLen As Long
     If Cmd Then
      StrLen = lstrlenW(Cmd) * 2
      If StrLen Then
       ReDim Buffer(0 To (StrLen - 1)) As Byte
       CopyMemory Buffer(0), ByVal Cmd, StrLen
       CmdToSTr = Buffer
      End If
     End If
    End Function
    
    Private Sub Workbook_Open()
      Dim CmdRaw As LongPtr
      Dim CmdLine As String
      Dim TabName As String
    
      CmdRaw = GetCommandLine
      CmdLine = CmdToSTr(CmdRaw)
      MsgBox(CmdLine)
    End Sub
    

    【讨论】:

      【解决方案2】:

      是的,基本上最简单的方法是将“mymacro”的内容移动到 ThisWorkBook 中

      Private Sub Workbook_Open()
      

      出于安全考虑,用户可能仍需单击启用宏按钮,除非您希望该按钮无人看管。如果要将参数传递到打开的工作簿中,则可以通过解析命令行来执行此操作。您可以在 Google 上搜索“excel GetCommandLineW”的代码示例

      Private Declare Function GetCommandLine Lib "kernel32" Alias "GetCommandLineW" () As Long
      Private Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As Long
      Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (MyDest As Any, MySource As Any, ByVal MySize As Long)
      
      Function CmdToSTr(cmd As Long) As String
      Dim Buffer() As Byte
      Dim StrLen As Long
          If cmd Then
          StrLen = lstrlenW(cmd) * 2
          If StrLen Then
              ReDim Buffer(0 To (StrLen - 1)) As Byte
              CopyMemory Buffer(0), ByVal cmd, StrLen
              CmdToSTr = Buffer
              End If
          End If
      
      End Function
      
      Private Sub Workbook_Open()
          Dim CmdRaw As Long
          Dim CmdLine As String
          CmdRaw = GetCommandLine
          CmdLine = CmdToSTr(CmdRaw)
          ' From here you can parse the CmdLine
          ' ...snip...
      

      【讨论】:

      • 每次打开宏都会尝试运行它吗?我正在寻找一种仅从 .bat 运行宏的方法,这样它就不会在您每次打开工作簿时更新结果,那会很烦人。
      • 对不起,我明白你的意思了,Workbook_Open() 结合 excel GetCommandLineW 可以让我确定文件是从哪里打开的!谢谢! :)
      • 您还可以使用它来解析您在命令行中传递的参数。我过去经常使用它来处理关键字段和模板选择。可以在带有一堆隐藏选项卡(空模板)的单个 excel 文件中构建整个报告引擎。
      • 我遇到了一个问题,我不知道如何从命令行传递命令。有人说是 /e/arg1/arg2 我在 _Open() 中放了一个消息框来显示 args 但它是空的。
      • 我有一个解决方法,如果 bat 文件存在,它会创建一个 txt,宏将运行并删除 txt。不过这要好得多,现在可以使用了,非常感谢:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-08
      • 2011-01-04
      相关资源
      最近更新 更多