0

I'm trying to convert my excel file to csv using visual studio and I'm having trouble converting it. I have looped my code to go through .xls or .xlsx file in a folder and convert each one of them to csv. However, I'm having no results at all :(

Textbox1.Text is the folder selected and Textbox2.Text is the destination folder. Anyone can help me on this?

Here is my code:

Dim xls As Excel.Application Dim strFile As String, strPath As String xls = New Excel.Application strPath = TextBox1.Text strFile = Dir(strPath & "*.xls") While strFile <> "" xls.Workbooks.Open(strPath & strFile) xls.ActiveWorkbook.SaveAs(Filename:=Replace(TextBox2.Text & strFile, ".xls", ".csv"), FileFormat:=Microsoft.Office.Interop.Excel.XlFileFormat.xlTextMSDOS) xls.Workbooks.Application.ActiveWorkbook.Close(SaveChanges:=False) strFile = Dir() End While xls.Quit() 
4
  • What happens when you run it? Does it crash? Or there are no files? Commented May 23, 2017 at 2:26
  • There are no files... Commented May 23, 2017 at 2:29
  • Maybe put a break and check the values of strPath and strFile? Break inside the While to see if it enters it at all. Or try saving with hardcoded path to see if it works. Commented May 23, 2017 at 2:36
  • side note xls.Workbooks.Application is the same as xls Commented May 23, 2017 at 4:16

1 Answer 1

1

Put this inside a text file and save it as Excel2Csv.vbs. Save it inside a folder containing all your excel files. Then just simply drag your excel files onto this .vbs file.

'* Usage: Drop .xl* files on me to export each sheet as CSV '* Global Settings and Variables Dim gSkip Set args = Wscript.Arguments For Each sFilename In args iErr = ExportExcelFileToCSV(sFilename) ' 0 for normal success ' 404 for file not found ' 10 for file skipped (or user abort if script returns 10) Next WScript.Quit(0) Function ExportExcelFileToCSV(sFilename) '* Settings Dim oExcel, oFSO, oExcelFile Set oExcel = CreateObject("Excel.Application") Set oFSO = CreateObject("Scripting.FileSystemObject") iCSV_Format = 6 '* Set Up sExtension = oFSO.GetExtensionName(sFilename) if sExtension = "" then ExportExcelFileToCSV = 404 Exit Function end if sTest = Mid(sExtension,1,2) '* first 2 letters of the extension, vb's missing a Like operator if not (sTest = "xl") then if (PromptForSkip(sFilename,oExcel)) then ExportExcelFileToCSV = 10 Exit Function end if End If sAbsoluteSource = oFSO.GetAbsolutePathName(sFilename) sAbsoluteDestination = Replace(sAbsoluteSource,sExtension,"{sheet}.csv") '* Do Work Set oExcelFile = oExcel.Workbooks.Open(sAbsoluteSource) For Each oSheet in oExcelFile.Sheets sThisDestination = Replace(sAbsoluteDestination,"{sheet}",oSheet.Name) oExcelFile.Sheets(oSheet.Name).Select oExcelFile.SaveAs sThisDestination, iCSV_Format Next '* Take Down oExcelFile.Close False oExcel.Quit ExportExcelFileToCSV = 0 Exit Function End Function Function PromptForSkip(sFilename,oExcel) if not (VarType(gSkip) = vbEmpty) then PromptForSkip = gSkip Exit Function end if Dim oFSO Set oFSO = CreateObject("Scripting.FileSystemObject") sPrompt = vbCRLF & _ "A filename was received that doesn't appear to be an Excel Document." & vbCRLF & _ "Do you want to skip this and all other unrecognized files? (Will only prompt this once)" & vbCRLF & _ "" & vbCRLF & _ "Yes - Will skip all further files that don't have a .xl* extension" & vbCRLF & _ "No - Will pass the file to excel regardless of extension" & vbCRLF & _ "Cancel - Abort any further conversions and exit this script" & vbCRLF & _ "" & vbCRLF & _ "The unrecognized file was:" & vbCRLF & _ sFilename & vbCRLF & _ "" & vbCRLF & _ "The path returned by the system was:" & vbCRLF & _ oFSO.GetAbsolutePathName(sFilename) & vbCRLF sTitle = "Unrecognized File Type Encountered" sResponse = MsgBox (sPrompt,vbYesNoCancel,sTitle) Select Case sResponse Case vbYes gSkip = True Case vbNo gSkip = False Case vbCancel oExcel.Quit WScript.Quit(10) '* 10 Is the error code I use to indicate there was a user abort (1 because wasn't successful, + 0 because the user chose to exit) End Select PromptForSkip = gSkip Exit Function End Function 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.