0

エラーの取得: 行 22、文字 60 の「Expected ')'」(つまり、QueryTables.Add 関数の「接続」の右側)。この VBA はバッチ ファイルによって呼び出されています。VB ジョブを呼び出してテキスト ファイルを書式設定された CSV に変換できるように、構文の何が問題なのかを理解しようとしています。テキスト ファイルは既にタブ区切りになっています。

バッチファイル:

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"
4

1 に答える 1

0

2 番目のスクリプトは、Excel VBA メソッドを呼び出しているにもかかわらず、VBA ではなく VBScript です。これら 2 つの言語にはいくつかの類似点がありますが、根本的な違いもいくつかあります。

  1. VBA メソッドおよびコレクションにアクセスするには、ハンドルが必要です。

  2. VBScript では名前引数を使用できません。

また、すでに変数で接続文字列を構築しているようですsourceFile

次の 2 行を変更します。

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)

そして問題は消えるはずです。

于 2013-05-11T23:32:43.517 に答える