1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| Option Explicit
Dim strXlsFile,objExl,strSrcFullPath,strSrcFileName,objTmpArray,strDelimiter,strTargetDir,strExtension
Const xlDelimited = 1
Const xlTextQualifierDoubleQuote = 1 'Double quotation mark (").
Const xlTextQualifierNone = -4142 'No delimiter.
Const xlTextQualifierSingleQuote = 2 'Single quotation mark (').
strSrcFullPath = "c:\tmp\work\test.txt"
strTargetDir = "c:\tmp\work"
strDelimiter = ","
objTmpArray = Split(strSrcFullPath,"\")
strSrcFileName = objTmpArray(UBound(objTmpArray))
If( Right(strTargetDir,1) <> "\" ) Then
strTargetDir = strTargetDir & "\"
End If
objTmpArray = Split(strSrcFileName,".")
strExtension = objTmpArray(UBound(objTmpArray))
strSrcFileName = Replace(strSrcFileName,"."&strExtension,"")
strXlsFile = strTargetDir &strSrcFileName &".xls"
Set objExl = CreateObject("Excel.Application")
objExl.Visible = False
objExl.CutCopyMode = False
objExl.Workbooks.OpenText strSrcFullPath,,,xlDelimited,xlTextQualifierDoubleQuote,,,,,,True,strDelimiter
objExl.Cells.Select
objExl.Selection.Columns.AutoFit
objExl.Range("A1").Select
objExl.ActiveWorkbook.SaveAs strXlsFile, -4143
objExl.ActiveWorkbook.Close True
objExl.Application.Quit
Set objExl = Nothing |
Partager