Excelmacros通过命令行运行

我想通过bash命令运行excelmacros像:cscript fileformat.vbs我每次都得到错误。

“C:\ xampp \ htdocs \ magento \ readcsvitem \ fileformat.vbs(4,48)Microsoft VBScript编译错误:预期')'”

Sub siddfinal1() ' siddfinal1 Macro ' Read Item format With ActiveSheet.QueryTables.Add(Connection:= _ "TEXT;C:\xampp\htdocs\magento\readcsvitem\csvitem\CmCSVExport-final.csv", _ Destination:=Range("$A$1")) .Name = "CmCSVExport-final" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .TextFilePromptOnRefresh = False .TextFilePlatform = 437 .TextFileStartRow = 1 .TextFileParseType = xlDelimited .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = True .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = False .TextFileSpaceDelimiter = False .TextFileColumnDataTypes = Array(1, 1) .TextFileTrailingMinusNumbers = True .Refresh BackgroundQuery:=False End With ActiveWorkbook.SaveAs Filename:="C:\xampp\htdocs\magento\readcsvitem\csvitem\Book1.csv", _ FileFormat:=xlCSV, CreateBackup:=False End Sub 

你有什么是VBA代码。 这和VBS非常相似,但是有一些关键的区别: https : //www.safaribooksonline.com/library/view/vbscript-in-a/1565927206/ch01s03.html

您收到的错误消息是由用于QueryTables.Add()的命名参数引起的 – 在VBS中不允许使用命名参数。 (4,48)表示在字符48的第4行发生了错误。这是冒号,指示命名参数。

Refresh方法也使用命名参数, SaveAs也是如此。 您可以通过删除参数名称来解决这个问题,并确保它们以正确的顺序logging,并且可选参数不会被留空(跳过)。

正如ShaggyRogers在他的回答中已经指出的那样, ActiveWorkbook在VBS中不可用,因为没有Excel主机应用程序可以使用该活动工作簿。 您必须先创build一个新的Excel应用程序,然后创build您要在其中创buildQueryTable的工作簿。

此外,用于为QueryTable对象(如xlInsertDeleteCells )设置属性的预定义常量是Excel VBA核心的一部分。 由于没有加载,你不能访问这些常量。 它们必须由各自的价值取代。

我整合了这些改变,并且清理了一些代码。 这现在应该适合你:

 Option Explicit Const strcConnection = "TEXT;C:\xampp\htdocs\magento\readcsvitem\csvitem\CmCSVExport-final.csv" Const strcOutputFile = "C:\xampp\htdocs\magento\readcsvitem\csvitem\Book1.csv" Dim app Dim wb ' create a new Excel instance Set app = CreateObject("Excel.Application") Set wb = app.Workbooks.Add() ' create a new QueryTable With wb.Sheets(1).QueryTables.Add(strcConnection, wb.Sheets(1).Range("A1")) .Name = "CmCSVExport-final" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = 1 ' xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .TextFilePromptOnRefresh = False .TextFilePlatform = 437 .TextFileStartRow = 1 .TextFileParseType = 1 ' xlDelimited .TextFileTextQualifier = 1 ' xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = True .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = False .TextFileSpaceDelimiter = False .TextFileColumnDataTypes = Array(1, 1) .TextFileTrailingMinusNumbers = True .Refresh False End With ' save the data imported from the QueryTable as a *.csv file wb.SaveAs strcOutputFile, 6 ' xlCSV