2013-05-09 71 views
0

在第22行,字符60(即在QueryTables.Add函数中的“连接”右边)出现错误:“Expected”)'“。这个VBA被一个批处理文件调用。我试图理解我的语法有什么问题,以便我可以调用VB作业来将文本文件转换为格式化的CSV。该文本文件已被制表符分隔。QueryTables Error:Expected')'

批处理文件:

pushd %~dp0     **Used to get current DIR 
set path=%~dp0     **Used to set a path variable to send to VBScript 
txtToCsv.vbs %path%   **Used to invoke the VBScript 

VBA脚本:

if WScript.Arguments.Count < 1 Then 
    WScript.Echo "Error! Please specify the source path and the destination. Usage: txtToCsv Destination.csv" 
    Wscript.Quit 
End If 
Dim oExcel 
Set oExcel = CreateObject("Excel.Application") 
oExcel.Visible = False 
oExcel.DisplayAlerts = False 

Dim oBook 
Set oBook = oExcel.Workbooks.Add() 
With oBook 
    .Title = "Deal Data" 
    .Subject = "Deal Data" 
    .SaveAs WScript.Arguments(0)&"Deal_Data.xlsx"&YEAR(Date)&MONTH(Date)&DAY(Date) 
End With 

Dim sourceFile 
Set sourceFile = "TEXT;"&WScript.Arguments(0)&"deal_data.txt" 

Set ActiveSheet = Worksheets("Sheet1") 
With ActiveWorkbook.ActiveSheet.QueryTables.Add(Connection:="TEXT;" & sourceFile, Destination:=ActiveCell) 
    .Name = "deal_data" 
     .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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) 
    .TextFileTrailingMinusNumbers = True 
    .Refresh BackgroundQuery:=False 
End With 

oBook.Close False 
oExcel.Quit 
WScript.Echo "Done" 

回答

0

你的第二个脚本是VBScript中,没有VBA,即使你调用它的Excel VBA方法。虽然这两种语言有一些相似之处,但也有一些根本的区别。

  1. 您需要一个用于访问VBA方法和集合的句柄。

  2. 您不能在VBScript中使用名称参数。

此外,您似乎已经在变量sourceFile中构建了连接字符串。

改变这两条线:

Set ActiveSheet = Worksheets("Sheet1") 
With ActiveWorkbook.ActiveSheet.QueryTables.Add(Connection:="TEXT;" & sourceFile, Destination:=ActiveCell) 

到这一点:

Set ActiveSheet = oBook.Worksheets("Sheet1") 
With ActiveSheet.QueryTables.Add(sourceFile, oExcel.ActiveCell) 

,问题应该会消失。

相关问题