Option Explicit
' Converts the first worksheet in an Excel workbook into
' a separate tab-delimited file. Pass this script an Excel
' file name and optionally the desired file name of the 
' output file. If no output file is specified, "ExcelToTab.tab"
' will be used.
Main

Sub Main()
Dim xl 'As Excel.Application
Dim ws 'As Excel.Worksheet
Dim wb 'As Excel.Workbook
Dim strExcelFileName 'As String
Dim strTabFileName 'As String
Const xlTextWindows = 20
'Const xlCSV = 6
'Const xlTextMac = 19
'Const xlTextMSDOS = 21
'Const xlCSVWindows = 23
'Const xlCSVMSDOS = 24
'Const xlTextPrinter = 36
'Const xlCurrentPlatformText = -4158

	If Wscript.Arguments.Count = 0 Then 
		CreateObject("Wscript.Shell").Popup "Pass this script the name of a local Excel workbook and (optionally) the name of the desired tab-delimited output file.", 10
		Wscript.Quit 1
	End If
	If Wscript.Arguments.Count > 2 Then 
		CreateObject("Wscript.Shell").Popup "You're only supposed to pass a maximum of two file names to this script. You probably need to quote your file names.", 10
		Wscript.Quit 1
	End If
	If Wscript.Arguments.Count = 1 Then
		strExcelFileName = Wscript.Arguments(0)
		strTabFileName = FileNameLikeMine("tab")
	End If
	If Wscript.Arguments.Count = 2 Then
		strExcelFileName = Wscript.Arguments(0)
		strTabFileName = Wscript.Arguments(1)
	End If
	If Lcase(Right(strExcelFileName, 4)) <> ".xls" Then
		CreateObject("Wscript.Shell").Popup "The first argument is supposed to be an Excel file name, and it wasn't. Maybe you need to quote the file name?", 10
		Wscript.Quit 1
	End If
	If Instr(strExcelFileName, ":\") = 0 Then strExcelFileName = FileNameInThisDir(strExcelFileName)
	If Instr(strTabFileName, ":\") = 0 Then strTabFileName = FileNameInThisDir(strTabFileName)

	Set xl = CreateObject("Excel.Application")
	Set wb = xl.Workbooks.Open(strExcelFileName, False, True, , , , True)
	Set ws = wb.Worksheets(1)
	ws.SaveAs strExcelFileName & ".tab", xlTextWindows
	wb.Close False
End Sub

Function FileNameInThisDir(strFileName) 'As String
'Returns the complete path and file name to a file in
'the script directory. For example, "trans.log" might
'return "C:\Program Files\Scripts\Database\trans.log"
'if the script was in the "C:\Program Files\Scripts\Database"
'directory.
Dim fs 'As Object
	Set fs = Wscript.CreateObject("Scripting.FileSystemObject")
	 FileNameInThisDir = fs.GetAbsolutePathName(fs.BuildPath(Wscript.ScriptFullName, "..\" & strFileName))
	''''''''''Clean up
	Set fs = Nothing
End Function

Function FileNameLikeMine(strFileExtension) 'As String
'Returns a file name the same as the script name except
'for the file extension. 
Dim fs 'As Object
Dim strExtension 'As String
	Set fs = Wscript.CreateObject("Scripting.FileSystemObject")
	strExtension = strFileExtension
	If Len(strExtension) < 1 Then strExtension = "txt"
	If strExtension = "." Then strExtension = "txt"
	If Left(strExtension,1) = "." Then strExtension = Mid(strExtension, 2)
	FileNameLikeMine = Left(Wscript.ScriptFullName, InstrRev(Wscript.ScriptFullName, ".")) & strExtension
	''''''''''Clean up
	Set fs = Nothing
End Function
