简体   繁体   English

VBScript打开Excel,然后添加VBA宏

[英]VBScript to open Excel then add a VBA macro

I need a VBScript to open a certain Excel Document, then when open it must add a Macro and save. 我需要一个VBScript来打开某个Excel文档,然后在打开它时必须添加一个宏并保存。

I can open the Excel document but I do not know how to open the Macro screen( Alt + F11 ) and then add the code and save... 我可以打开Excel文档,但不知道如何打开“宏”屏幕( Alt + F11 ),然后添加代码并保存...

Is there anyway to do this? 反正有这样做吗?

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

Follow these steps: 跟着这些步骤:

  1. Open the VBA Editor in Excel and add a new Module. 在Excel中打开VBA编辑器,然后添加一个新模块。
  2. Paste your macro code into it. 将您的宏代码粘贴到其中。
  3. Right-click the Module and choose Export... . 右键单击模块,然后选择Export...
  4. Give it a filename and save it somewhere. 给它一个文件名并将其保存在某个地方。
  5. In your VBScript, add the following lines of code: 在您的VBScript中,添加以下代码行:

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

    Note that, in Excel 2007+, you can't save macros in xlsx files. 请注意,在Excel 2007+中,您不能将宏保存在xlsx文件中。 You'll need to use SaveAs instead and give the file an xslm extension. 您将需要使用SaveAs代替,并为文件指定xslm扩展名。 Or, you can use the old xls format (which is what you appear to be using from your example). 或者,您可以使用旧的xls格式(这就是您的示例所使用的格式)。

This is not straight forward, but what I would do is use SendKeys function to simulate Alt + F11 . 这不是直截了当的,但是我要做的是使用SendKeys函数来模拟Alt + F11

Application.SendKeys "%{F11}", True

Then using the same logic, use keystrokes to navigate to the proper window, adding a module, then paste the macro code in the right place using: 然后使用相同的逻辑,使用击键导航到适当的窗口,添加模块,然后使用以下命令将宏代码粘贴到正确的位置:

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

Then you could save using: 然后,您可以使用以下方法保存:

Application.SendKeys ""^S", True

You can read more about that here and here 你可以在这里这里阅读更多

But another way is to use a mouse and keyboard macro recorder (stand alone application that can be programmed to mimic actions). 但是另一种方法是使用鼠标和键盘宏记录器(可以编程为模仿动作的独立应用程序)。 I personally have used KeyText for more than 10 years to do that sort of thing. 我个人已经使用KeyText十多年了。

You can add code programmatically by using the VBComponents object of the VBProject object. 您可以使用VBProject对象的VBComponents对象以编程方式添加代码。 So add this behind the last line of your code: 因此,将其添加到代码的最后一行:

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

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

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