【问题标题】:VBScript to open Excel then add a VBA macroVBScript 打开 Excel 然后添加 VBA 宏
【发布时间】:2015-09-19 20:01:23
【问题描述】:

我需要一个VBScript来打开某个Excel文档,然后打开它时必须添加一个宏并保存。

我可以打开Excel文档但不知道如何打开宏屏幕(Alt+F11)然后添加代码并保存...

有没有办法做到这一点?

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\test.xls")
objExcel.Application.DisplayAlerts = False
objExcel.Application.Visible = True`

'Macro Script

Sub HideRows()
    Dim cell As Range   
    For Each cell In Range("H1:W200")
        If Not isEmpty(cell) Then
            If cell.Value <> "" And cell.Value = 0 Then 
                cell.EntireRow.Hidden = True
                Columns("H").EntireColumn.Hidden = True
                Columns("I").EntireColumn.Hidden = True
                Columns("J").EntireColumn.Hidden = True
                Columns("M").EntireColumn.Hidden = True
                Columns("N").EntireColumn.Hidden = True
                Columns("O").EntireColumn.Hidden = True
                Columns("P").EntireColumn.Hidden = True
                Columns("Q").EntireColumn.Hidden = True
                Columns("S").EntireColumn.Hidden = True
                Columns("T").EntireColumn.Hidden = True
                Columns("V").EntireColumn.Hidden = True
            End If
        End If
    Next
End Sub

【问题讨论】:

  • 我强烈建议您在不使用 SendKeys 的情况下解决此问题,因为这种方法可能会(不情愿地)被用户破坏,并且还会在持续时间内使屏幕无用。 Microsoft Support 或 @Zam 的链接应该对您有所帮助。

标签: excel vbscript vba


【解决方案1】:

按照以下步骤操作:

  1. 在 Excel 中打开 VBA 编辑器并添加一个新模块。
  2. 将您的宏代码粘贴到其中。
  3. 右键单击模块并选择Export...
  4. 给它一个文件名并将其保存在某个地方。
  5. 在您的 VBScript 中,添加以下代码行:

    objWorkbook.VBProject.VBComponents.Import "/path/to/your/module.bas"
    objWorkbook.Save
    

    请注意,在 Excel 2007+ 中,您无法将宏保存在 xlsx 文件中。您需要改用SaveAs 并为文件提供xslm 扩展名。或者,您可以使用旧的 xls 格式(这就是您在示例中使用的格式)。

【讨论】:

    【解决方案2】:

    您可以使用VBProject 对象的VBComponents 对象以编程方式添加代码。所以在你的代码的最后一行添加这个:

    Set objModule = objworkbook.VBProject.VBComponents.Add(1)       ' 1 = vbext_ct_StdModule
    
    objExcel.Visible = True    ' not necessary if you close Excel anyway
    
    theSource = ""
    theSource = theSource & "Sub HideRows()" & vbCrLf
    theSource = theSource & "    Dim cell As Range   " & vbCrLf
    theSource = theSource & "    For Each cell In Range(""H1:W200"")" & vbCrLf
    theSource = theSource & "        If Not isEmpty(cell) Then" & vbCrLf
    theSource = theSource & "            If cell.Value <> """" And cell.Value = 0 Then " & vbCrLf
    theSource = theSource & "                cell.EntireRow.Hidden = True" & vbCrLf
    theSource = theSource & "                Columns(""H"").EntireColumn.Hidden = True" & vbCrLf
    theSource = theSource & "                Columns(""I"").EntireColumn.Hidden = True" & vbCrLf
    theSource = theSource & "                Columns(""J"").EntireColumn.Hidden = True" & vbCrLf
    theSource = theSource & "                Columns(""M"").EntireColumn.Hidden = True" & vbCrLf
    theSource = theSource & "                Columns(""N"").EntireColumn.Hidden = True" & vbCrLf
    theSource = theSource & "                Columns(""O"").EntireColumn.Hidden = True" & vbCrLf
    theSource = theSource & "                Columns(""P"").EntireColumn.Hidden = True" & vbCrLf
    theSource = theSource & "                Columns(""Q"").EntireColumn.Hidden = True" & vbCrLf
    theSource = theSource & "                Columns(""S"").EntireColumn.Hidden = True" & vbCrLf
    theSource = theSource & "                Columns(""T"").EntireColumn.Hidden = True" & vbCrLf
    theSource = theSource & "                Columns(""V"").EntireColumn.Hidden = True" & vbCrLf
    theSource = theSource & "            End If" & vbCrLf
    theSource = theSource & "        End If" & vbCrLf
    theSource = theSource & "    Next" & vbCrLf
    theSource = theSource & "End Sub" & vbCrLf
    
    objModule.CodeModule.AddFromString theSource
    
    'objExcel.Quit
    
    'Set objExcel = Nothing
    

    【讨论】:

      【解决方案3】:

      这不是直截了当的,但我要做的是使用SendKeys 函数来模拟 Alt+F11

      Application.SendKeys "%{F11}", True
      

      然后使用相同的逻辑,使用击键导航到正确的窗口,添加一个模块,然后使用以下命令将宏代码粘贴到正确的位置:

      Application.SendKeys ""^V"
      Application.SendKeys ""^V", True   'Incase that one above does not work
      

      然后你可以保存使用:

      Application.SendKeys ""^S", True
      

      您可以阅读更多关于 herehere 的信息

      但另一种方法是使用鼠标和键盘宏记录器(可以编程以模仿动作的独立应用程序)。我个人使用KeyText 做这种事情已经有 10 多年了。

      【讨论】:

      • 顺便提一下:很多人认为 SendKeys 风险太大,一般不建议将其用于业务解决方案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多