Thursday, December 19, 2013

Function to convert the txt file to Csv file

Function Convert_TextFile_To_CSVFile (strfilepath) Dim strfilepathTxt Dim strfilepathCsv Dim oExcel Dim oBook Dim objClipboard Dim fso Dim oSheet Convert_TextFile_To_CSVFile = -1 If strfilepath <> "" And Len(strfilepath) > 0 Then strfilepathTxt = strfilepath strfilepathCsv = replace(strfilepath , "txt", "csv") Else Convert_TextFile_To_CSVFile = -1 End If Set objClipboard = CreateObject("Mercury.Clipboard") objClipboard.Clear Set fso = createobject("scripting.filesystemobject") Set notepad = fso.opentextfile(strfilepathTxt,1) objClipboard.SetText notepad.readall 'Start a new workbook in Excel. Set oExcel = CreateObject("Excel.Application") oExcel.Visible = True Set oBook = oExcel.Workbooks.Add 'Add data to cells of the first worksheet in the new workbook. Set oSheet = oBook.Worksheets(1) oSheet.paste() oExcel.DisplayAlerts = False 'Save the Workbook and quit Excel. oBook.SaveAs strfilepathCsv,-4143 oExcel.DisplayAlerts = False oExcel.Quit() Set oSheet = Nothing Set oBook = Nothing Set oExcel = Nothing Set objClipboard = Nothing Set fso = Nothing If Err.number = 0 Then Convert_TextFile_To_CSVFile = 0 End If End Function

No comments:

Post a Comment