[英]VBScript to open Excel then add a VBA macro
我需要一個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
跟着這些步驟:
Export...
在您的VBScript中,添加以下代碼行:
objWorkbook.VBProject.VBComponents.Import "/path/to/your/module.bas" objWorkbook.Save
請注意,在Excel 2007+中,您不能將宏保存在xlsx
文件中。 您將需要使用SaveAs
代替,並為文件指定xslm
擴展名。 或者,您可以使用舊的xls
格式(這就是您的示例所使用的格式)。
這不是直截了當的,但是我要做的是使用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
但是另一種方法是使用鼠標和鍵盤宏記錄器(可以編程為模仿動作的獨立應用程序)。 我個人已經使用KeyText十多年了。
您可以使用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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.