2013-05-09 2 views
0

이 VBA는 배치 파일에 의해 호출됩니다. 텍스트 파일을 형식화 된 CSV로 바꾸기 위해 VB 구문을 호출 할 수 있도록 구문에 무엇이 잘못된지 이해하려고합니다. 텍스트 파일은 이미 탭으로 구분됩니다.QueryTables 오류 : 예상 됨 ')'22 행, 문자 60 (예 : QueryTables.Add 함수의 '연결'오른쪽)에 '예상') '오류가 발생했습니다'

배치 파일 :

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

두 번째 스크립트는 그 안에 엑셀 VBA 메소드를 호출하고, 비록 VBScript를하지 VBA입니다. 이 두 언어가 유사점을 공유하지만 근본적인 차이점도 있습니다.

  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) 

하고 문제가 사라집니다.