Running Excel from VBS script

2020-02-16 03:10发布

I am trying to run the following code from a .vbs file under Windows, but cannot get it to run. I have added some commands from a macro I recorded, and apparently that does not work. Could you point me in the right direction? (I realize this must be a trivial problem)

Set fso = CreateObject("Scripting.FileSystemObject")
Set xl  = CreateObject("Excel.Application")
xl.Visible = True

For Each f In fso.GetFolder("C:\Temp").Files
  If LCase(fso.GetExtensionName(f.Name)) = "xlsm" Then
    Set wb = xl.Workbooks.Open(f.Path)
    Set ws = wb.Worksheets("Data")
    ' make sure number formatting is OK
    ws.Cells.Select
    ws.Selection.NumberFormat = "General"
    ' kill header row
    ws.Rows("1:1").Select
    ws.Selection.Delete Shift:=xlUp
    ' save output as CSV
    wb.SaveAs Filename:= f.Path & ".csv", FileFormat:= xlCSVMSDOS, CreateBackup:=False
    wb.Close
  End If
Next

xl.Quit

1条回答
forever°为你锁心
2楼-- · 2020-02-16 03:44

VBScript does neither supports named arguments nor xl* constant as in your:

ws.Selection.Delete Shift:=xlUp

See named/positional args, consts, other.

查看更多
登录 后发表回答