Excel宏通过命令行运行

时间:2016-05-06 05:34:39

标签: excel vba excel-vba vbscript

我想通过bash命令运行excel宏像: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

1 个答案:

答案 0 :(得分:0)

你拥有的是VBA代码。它与VBS非常相似,但存在一些关键差异:https://www.safaribooksonline.com/library/view/vbscript-in-a/1565927206/ch01s03.html

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

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

正如ShaggyRogers在他的回答中所说,ActiveWorkbook在VBS中不可用,因为没有Excel主机应用程序可以从中获取活动工作簿。您必须先创建一个新的Excel应用程序,然后创建要在其中创建QueryTable的工作簿。

此外,用于设置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