'Convert "\QuickTX\lbArgs.txt" to Excel

On Error Resume Next
Set objExcel = CreateObject("Excel.Application")
If Err.Number <> 0 Then
	Wscript.Echo "You must have Excel installed to perform this operation."
Else
	Set fso = CreateObject("Scripting.FileSystemObject")
	myTxtFile = ""
	myXlsFile = ""
	' Input via Arguments
	For I = 0 to WScript.Arguments.Count - 1
		If fso.FileExists(WScript.Arguments.Item(I)) Then
			Select Case UCase(Right(WScript.Arguments.Item(I),4))
				Case ".TXT"
					myTxtFile = WScript.Arguments.Item(I)
				Case ".XLS"
					myXlsFile = WScript.Arguments.Item(I)
			End Select
		End If
	Next
	'Input via Explorer
	If myTxtFile = "" Then 
		Set ObjFSO = CreateObject("UserAccounts.CommonDialog")
		ObjFSO.Filter = "Text File|*.txt"
		ObjFSO.FilterIndex = 3
		ObjFSO.ShowOpen
		myTxtFile = ObjFSO.FileName
	End If
	If myTxtFile <> "" Then
		If myXlsFile = "" Then 
			Set ObjFSO = CreateObject("UserAccounts.CommonDialog")
			ObjFSO.Filter = "Excel File|*.xls"
			ObjFSO.FilterIndex = 3
			ObjFSO.ShowOpen
			myXlsFile = ObjFSO.FileName
		End If
	End If
	'Call the Convert Procedure
	If myTxtFile <> "" and myXlsFile <> ""  Then
		Call DoConvert(myTxtFile,myXlsFile)
	End If 	
End If

Sub DoConvert(txtFile,xlsFile)
	Set inFile = fso.OpenTextFile(txtFile, 1)
	col = 1
	row = 1
	'Create Excel file & Workbooks
	objexcel.Visible = False
	Set objWorkbook  = objExcel.Workbooks.Open(xlsFile)
	'Get the last Row
	Do Until objExcel.Cells(row,1).Value = ""
		row = row + 1
	Loop
	objexcel.Cells(row,1).Value = "Quote" & Row-1
	'Get all the info in Row 1 
	Do Until objExcel.Cells(1,col).Value = ""
		Redim Preserve dHead(col)
		dHead(col) = Ucase(objExcel.Cells(1,col).Value)
		col = col + 1
	Loop
	'Loop file & copy info to Excel
	Do until inFile.AtEndOfStream
		dLine = inFile.ReadLine
		'Get info from line, Separator is the = 
		EqualPos = InStr(dLine,"=")
		dIni = Ucase(Mid(dLine, 1, EqualPos-1))
		dEnd = Mid(dLine, EqualPos+1, Len(dLine) - EqualPos + 1)
		'Handle Special Cases
		Select Case Left(dIni,7)
        	Case "DPRIORL" 'Add and "A"
				dIni = Left(dIni,7) & "A" & Mid(dIni,8,Len(dIni)) 
			Case "VIN_NUM" 'Remove the "_1"
				dIni = Left(dIni,10) & Right(dIni,1)
			Case "VBI_LIM" 'Remove the "_1"
				dIni = Left(dIni,7)
			Case "VPD_LIM" 'Remove the "_1"
				dIni = Left(dIni,7)
			Case "VPIP_LI" 'Remove the "_1"
				dIni = Left(dIni,8)
			Case "VUM_LIM" 'Remove the "_1"
				dIni = Left(dIni,7)
			Case "VUMPD_L" 'Remove the "_1"
				dIni = Left(dIni,9)
			Case "VMED_LI" 'Remove the "_1"
				dIni = Left(dIni,8)
			Case "VFAKEOP"
				dIni = "VOPTIONS_" & Right(dIni,1)
			Case "VOPTION" 'Not needed
				dIni = ""
		End Select
		'Check if it exists in the headers
		For I = 1 to col - 1
			If dIni = dHead(I) Then 
				objexcel.Cells(row,I).Value = dEnd
				Exit For
			End If
		Next
	Loop
	'AutoFit all columns
	objexcel.Cells.Select
    objexcel.Cells.EntireColumn.AutoFit
    objexcel.Range("A1").Select
	'Save Excel File & Close Excel Object
	'objexcel.ActiveWorkbook.SaveAs(txtFile & ".xls")
	objexcel.ActiveWorkbook.Save 
	objExcel.Workbooks(1).Close
	objexcel.Quit
End Sub

