VBScript打开Excel,然后添加一个VBAmacros
我需要一个VBScript打开一个特定的Excel文档,然后打开它时,必须添加一个macros并保存。
我可以打开Excel文档,但我不知道如何打开macros屏幕( 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
按着这些次序:
- 在Excel中打开VBA编辑器并添加一个新模块。
- 将您的macros代码粘贴到它。
- 用鼠标右键单击该模块,然后select
Export...
- 给它一个文件名并保存在某个地方。
-
在您的VBScript中,添加以下几行代码:
objWorkbook.VBProject.VBComponents.Import "/path/to/your/module.bas" objWorkbook.Save
请注意,在Excel 2007+中,不能将macros保存在
xlsx
文件中。 您需要使用SaveAs
并为文件指定一个xslm
扩展名。 或者,你可以使用旧的xls
格式(这是你的例子中使用的)。
这不是直截了当的,但我会做的是使用SendKeys
函数来模拟Alt + F11 。
Application.SendKeys "%{F11}", True
然后使用相同的逻辑,使用按键导航到适当的窗口,添加一个模块,然后将macros代码粘贴到正确的位置使用:
Application.SendKeys ""^V" Application.SendKeys ""^V", True 'Incase that one above does not work
然后你可以使用
Application.SendKeys ""^S", True
你可以在这里和这里阅读更多
但另一种方法是使用鼠标和键盘的macroslogging器(独立的应用程序,可以编程模仿行动)。 我个人已经使用KeyText超过10年来做这样的事情。
您可以通过使用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