Results 1 to 14 of 14

Thread: Convert excel file to csv format

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2005
    Posts
    12

    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

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Convert excel file to csv format

    reference the MS Excel Object library

    simple example to loop through a directory and convert...
    VB Code:
    1. Dim xls As Excel.Application
    2. Private Sub Form_Load()
    3. Set xls = New Excel.Application
    4. xls.Visible = True 'comment this out if you dont want excel to show
    5. tmp = Dir("C:\path\*.xls")
    6. Do While tmp > ""
    7.     xls.Workbooks.Open "C:\path\" & tmp
    8.     xls.ActiveWorkbook.SaveAs FileName:=Replace("C:\path\" & tmp, ".xls", ".csv", , , vbTextCompare), FileFormat:=xlCSVMSDOS, CreateBackup:=False
    9.     xls.Workbooks.Close
    10.     tmp = Dir
    11. Loop
    12. xls.Quit
    13. End Sub
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3
    Lively Member
    Join Date
    Jul 2006
    Posts
    102

    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:
    1. Private Sub cmdXlsCsv_Click()
    2.     Dim xls As Excel.Application
    3.     Dim tmp As String
    4.     Set xls = New Excel.Application
    5.     'xls.Visible = True 'comment this out if you dont want excel to show
    6.     tmp = Dir("C:\cmo\*.xls")
    7.     Do While tmp > ""
    8.     xls.Workbooks.Open "C:\cmo\" & tmp
    9.     xls.ActiveWorkbook.SaveAs FileName:=Replace("C:\cmo\" & tmp, ".xls", ".csv", , , vbTextCompare), FileFormat:=xlCSVMSDOS, CreateBackup:=False
    10.     xls.Workbooks.Close
    11.     tmp = Dir
    12.     Loop
    13.     xls.Quit
    14. End Sub
    Last edited by swtransaction; Aug 2nd, 2006 at 04:31 AM.

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Convert excel file to csv format

    You need to add the argument to not save changes since your using .SaveAs
    VB Code:
    1. 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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  5. #5
    Lively Member
    Join Date
    Jul 2006
    Posts
    102

    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:
    1. 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:


  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Convert excel file to csv format

    VB Code:
    1. Private Sub cmdXlsCsv_Click()
    2.     Dim xls As Excel.Application
    3.     Dim oWB As Excel.Workbook
    4.     Dim tmp As String
    5.     Set xls = New Excel.Application
    6.     'xls.Visible = True 'comment this out if you dont want excel to show
    7.     tmp = Dir("C:\cmo\*.xls")
    8.     Do While tmp > ""
    9.         Set oWB = xls.Workbooks.Open("C:\cmo\" & tmp)
    10.         oWB.SaveAs FileName:=Replace("C:\cmo\" & tmp, ".xls", ".csv", , , vbTextCompare), FileFormat:=xlCSVMSDOS, CreateBackup:=False)
    11.         oWB.Close SaveChanges:=False
    12.         tmp = Dir
    13.         Set oWB = Nothing
    14.     Loop
    15.     xls.Quit
    16.     Set xls = Nothing
    17. 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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  7. #7
    Lively Member
    Join Date
    Jul 2006
    Posts
    102

    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:
    1. Private Sub cmdXlsCsv_Click()
    2.  
    3.     Screen.MousePointer = vbHourglass
    4.    
    5.     Dim xls As Excel.Application
    6.     Dim oWB As Excel.Workbook
    7.     Dim tmp As String
    8.     Set xls = New Excel.Application
    9.     tmp = Dir("C:\cmo\*.xls")
    10.     Do While tmp > ""
    11.         Set oWB = xls.Workbooks.Open("C:\cmo\" & tmp)
    12.         oWB.SaveAs FileName:=Replace _
    13.         ("C:\cmo\" & tmp, ".xls", ".csv", , , vbTextCompare), _
    14.         FileFormat:=xlCSVMSDOS, CreateBackup:=False
    15.         oWB.Close SaveChanges:=False
    16.         tmp = Dir
    17.         Set oWB = Nothing
    18.     Loop
    19.     xls.Quit
    20.     Set xls = Nothing
    21.    
    22.     Screen.MousePointer = vbDefault
    23. End Sub

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  9. #9
    Lively Member
    Join Date
    Jul 2006
    Posts
    102

    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:
    1. Private Sub cmdXlsCsv_Click()
    2.  
    3.     On Error GoTo MyError
    4.    
    5.     cmdXlsCsv.Enabled = False
    6.  
    7.     Screen.MousePointer = vbHourglass
    8.    
    9.     Dim xls As Excel.Application
    10.     Dim oWB As Excel.Workbook
    11.     Dim tmp As String
    12.     Set xls = New Excel.Application
    13.     tmp = Dir("C:\cmo\*.xls")
    14.     Do While tmp > ""
    15.         Set oWB = xls.Workbooks.Open("C:\cmo\" & tmp)
    16.         oWB.SaveAs FileName:=Replace _
    17.         ("C:\cmo\" & tmp, ".xls", ".csv", , , vbTextCompare), _
    18.         FileFormat:=xlCSVMSDOS, CreateBackup:=False
    19.         oWB.Close SaveChanges:=False
    20.         tmp = Dir
    21.         Set oWB = Nothing
    22.     Loop
    23.     xls.Quit
    24.     Set xls = Nothing
    25.    
    26.     Screen.MousePointer = vbDefault
    27.  
    28.     cmdMove.Enabled = True
    29.    
    30. MyError:
    31.  
    32.    If Err.Number = 70 Then
    33.    MsgBox "Csv File Exixts", vbOKOnly + vbExclamation
    34.    End If
    35.  
    36. End Sub

  10. #10
    Lively Member
    Join Date
    Jul 2006
    Posts
    102

    Re: Convert excel file to csv format

    This seems to get it. Needed to turn off Excel and the VB Glass

    VB Code:
    1. MyError:
    2.  
    3.    If Err.Number = 1004 Then
    4.    MsgBox "Csv File Exixts", vbOKOnly + vbExclamation
    5.    xls.Quit
    6.    Set xls = Nothing
    7.    Screen.MousePointer = vbDefault
    8.    End If

  11. #11
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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:
    1. 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

  12. #12
    Lively Member
    Join Date
    Jul 2006
    Posts
    102

    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.

  13. #13
    Lively Member
    Join Date
    Jul 2006
    Posts
    102

    Re: Convert excel file to csv format

    I figured it out:

    Columns("A:Z").Select
    Selection.NumberFormat = "General"

  14. #14
    New Member
    Join Date
    Aug 2006
    Posts
    9

    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
  •  



Click Here to Expand Forum to Full Width