|
-
Oct 13th, 2005, 02:25 PM
#1
Thread Starter
New Member
Convert excel file to csv format
Hello VB gurus,
I have unique problem with saving excel file in CSV format. I want to do in batch process.
Steps : Read excel file as input and create another csv format file.
Please help me wth my problem
Thanks in Advance
Raj
-
Oct 13th, 2005, 02:39 PM
#2
Re: Convert excel file to csv format
reference the MS Excel Object library
simple example to loop through a directory and convert...
VB Code:
Dim xls As Excel.Application
Private Sub Form_Load()
Set xls = New Excel.Application
xls.Visible = True 'comment this out if you dont want excel to show
tmp = Dir("C:\path\*.xls")
Do While tmp > ""
xls.Workbooks.Open "C:\path\" & tmp
xls.ActiveWorkbook.SaveAs FileName:=Replace("C:\path\" & tmp, ".xls", ".csv", , , vbTextCompare), FileFormat:=xlCSVMSDOS, CreateBackup:=False
xls.Workbooks.Close
tmp = Dir
Loop
xls.Quit
End Sub
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Aug 1st, 2006, 09:46 PM
#3
Lively Member
Re: Convert excel file to csv format
I found this thread. I'm trying to batch convert several XLS files into CSV files. This works but the problem is:
I get a message box that asks me if I want to save the changes (the CSV file was already created and so its asking me to save over it).
Question:
How can I get this to batch process without the message box popping up?
I also found this thread, post #5 has some info but couldn't figure this out.
http://www.vbforums.com/showthread.php?t=391665
Thanks!!!
1. I referenced the MS Excel Object library.
2. Have multiple XLS files in a directory called c:\cmo\
3. Modified the code a bit to make it work:
VB Code:
Private Sub cmdXlsCsv_Click()
Dim xls As Excel.Application
Dim tmp As String
Set xls = New Excel.Application
'xls.Visible = True 'comment this out if you dont want excel to show
tmp = Dir("C:\cmo\*.xls")
Do While tmp > ""
xls.Workbooks.Open "C:\cmo\" & tmp
xls.ActiveWorkbook.SaveAs FileName:=Replace("C:\cmo\" & tmp, ".xls", ".csv", , , vbTextCompare), FileFormat:=xlCSVMSDOS, CreateBackup:=False
xls.Workbooks.Close
tmp = Dir
Loop
xls.Quit
End Sub
Last edited by swtransaction; Aug 2nd, 2006 at 04:31 AM.
-
Aug 2nd, 2006, 01:02 AM
#4
Re: Convert excel file to csv format
You need to add the argument to not save changes since your using .SaveAs
VB Code:
xls.Workbooks.Close SaveChanges:=False
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Aug 2nd, 2006, 04:29 AM
#5
Lively Member
Re: Convert excel file to csv format
I'm getting a VB message, "Compile error: Wrong number of arguements or invalid property assignment" xls.Workbooks.Close
I tried this:
VB Code:
xls.Workbooks.Application.ActiveWorkbook.Close SaveChanges:=False
It seems to work but seems like there could be a shorter way to write that, looks funky (does work though)
original code with error:
-
Aug 2nd, 2006, 11:45 AM
#6
Re: Convert excel file to csv format
VB Code:
Private Sub cmdXlsCsv_Click()
Dim xls As Excel.Application
Dim oWB As Excel.Workbook
Dim tmp As String
Set xls = New Excel.Application
'xls.Visible = True 'comment this out if you dont want excel to show
tmp = Dir("C:\cmo\*.xls")
Do While tmp > ""
Set oWB = xls.Workbooks.Open("C:\cmo\" & tmp)
oWB.SaveAs FileName:=Replace("C:\cmo\" & tmp, ".xls", ".csv", , , vbTextCompare), FileFormat:=xlCSVMSDOS, CreateBackup:=False)
oWB.Close SaveChanges:=False
tmp = Dir
Set oWB = Nothing
Loop
xls.Quit
Set xls = Nothing
End Sub
This should do it.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Aug 2nd, 2006, 05:38 PM
#7
Lively Member
Re: Convert excel file to csv format
It works great, Thanks !!! ... had to get rid of the ) after CreateBackup:=False) ...
I added an hour glass also since it is a batch routine.
VB Code:
Private Sub cmdXlsCsv_Click()
Screen.MousePointer = vbHourglass
Dim xls As Excel.Application
Dim oWB As Excel.Workbook
Dim tmp As String
Set xls = New Excel.Application
tmp = Dir("C:\cmo\*.xls")
Do While tmp > ""
Set oWB = xls.Workbooks.Open("C:\cmo\" & tmp)
oWB.SaveAs FileName:=Replace _
("C:\cmo\" & tmp, ".xls", ".csv", , , vbTextCompare), _
FileFormat:=xlCSVMSDOS, CreateBackup:=False
oWB.Close SaveChanges:=False
tmp = Dir
Set oWB = Nothing
Loop
xls.Quit
Set xls = Nothing
Screen.MousePointer = vbDefault
End Sub
-
Aug 2nd, 2006, 05:47 PM
#8
Re: Convert excel file to csv format
You might also want to add a DoEvents in the loop to free the CPU to do other things so it doesnt seem like its frozen during large batches.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Aug 3rd, 2006, 10:27 PM
#9
Lively Member
Re: Convert excel file to csv format
If the same file is converted from XLS to CSV twice, then the message box come up file already exists. (this would happen because program is run twice, which shouldn't happen ... but ...) So even though it should never happen, I figure I need some error handling in this.
If the user hits the save button all is find, cancel errors the vb program out.
I tried this error code but the program acts like it is trying to do something, but it just sits there.
VB Code:
Private Sub cmdXlsCsv_Click()
On Error GoTo MyError
cmdXlsCsv.Enabled = False
Screen.MousePointer = vbHourglass
Dim xls As Excel.Application
Dim oWB As Excel.Workbook
Dim tmp As String
Set xls = New Excel.Application
tmp = Dir("C:\cmo\*.xls")
Do While tmp > ""
Set oWB = xls.Workbooks.Open("C:\cmo\" & tmp)
oWB.SaveAs FileName:=Replace _
("C:\cmo\" & tmp, ".xls", ".csv", , , vbTextCompare), _
FileFormat:=xlCSVMSDOS, CreateBackup:=False
oWB.Close SaveChanges:=False
tmp = Dir
Set oWB = Nothing
Loop
xls.Quit
Set xls = Nothing
Screen.MousePointer = vbDefault
cmdMove.Enabled = True
MyError:
If Err.Number = 70 Then
MsgBox "Csv File Exixts", vbOKOnly + vbExclamation
End If
End Sub
-
Aug 3rd, 2006, 10:36 PM
#10
Lively Member
Re: Convert excel file to csv format
This seems to get it. Needed to turn off Excel and the VB Glass
VB Code:
MyError:
If Err.Number = 1004 Then
MsgBox "Csv File Exixts", vbOKOnly + vbExclamation
xls.Quit
Set xls = Nothing
Screen.MousePointer = vbDefault
End If
-
Aug 3rd, 2006, 11:02 PM
#11
Re: Convert excel file to csv format
problem with that, you have not put resume next in your error handler, so no other files will be processed after the error
my choice would be to test if the file exists before save
VB Code:
if dir(filename) = "" then 'save as code, where filename is the csv filename
Last edited by westconn1; Aug 3rd, 2006 at 11:06 PM.
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 13th, 2006, 04:45 PM
#12
Lively Member
Re: Convert excel file to csv format
One other thing I'd like to do if possible is when the spreadsheet file is opened, before it is saved as a CSV, is to remove the formatting ... that would help immensely.
I've looked all over the place to try to figure it out but can't seem to get it.
Thanks.
-
Aug 13th, 2006, 07:00 PM
#13
Lively Member
Re: Convert excel file to csv format
I figured it out:
Columns("A:Z").Select
Selection.NumberFormat = "General"
-
Aug 18th, 2006, 10:06 PM
#14
New Member
Re: Convert excel file to csv format
Hi guys
I also have a question on the VB codes above.
The codes can work well,but they can only convert one sheet to csv file,
for example, if the excel spreadsheet has 2 or more sheet(sheet1 sheet2 sheet3...) it can only convet sheet1 or sheet2.. to csv file..
I am a new comer to VB, could you help me?
Thank you
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|