locked
Saving a xsl as a csv file RRS feed

  • Question

  • Hi , 

    I am using the following code to save a xslx as a CSV file.

    Private Sub cmdXlsCsv_Click()
    
        Dim xls As Excel.Application
        Dim tmp As String
        Dim oWB As Excel.Workbook
        
        Set xls = New Excel.Application
        tmp = "C:\Users\cjack\Desktop\Copy of Book5.xlsx"
        
        Set oWB = xls.Workbooks.Open(tmp)
        oWB.SaveAs Filename:=Replace(tmp, ".xls", ".csv", , , vbTextCompare), FileFormat:=xlCSVMSDOS, CreateBackup:=False
        oWB.Close SaveChanges:=False
        xls.Quit
        
    End Sub
    
    

    However it is saving it as a csvx file instead of a csv file....

    Any help appreciated.


    Tuesday, May 1, 2018 1:19 PM

All replies

  • Try the following

    Dim tmp As String = "C:\Users\cjack\Desktop\Copy of Book5.xlsx"
    Dim otherFileName As String = IO.Path.ChangeExtension(tmp, ".csv")
    oWB.SaveAs(otherFileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlCSV)


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, May 1, 2018 2:06 PM
  • Please change Replace function like this:
    oWB.SaveAs Filename:=Replace(tmp, ".xlsx", ".csv", , , vbTextCompare), ....
    Regards,

    Ashidacchi -- http://hokusosha.com/

    Wednesday, May 2, 2018 12:50 AM
  • Hi JaK82,

    You can also try the following code:

     Dim app As New Excel.Application()
            Dim wb As Excel.Workbook = app.Workbooks.Open("D:\Testfile\test3.xlsx")
            wb.SaveAs("D:\Testfile\output.csv", Excel.XlFileFormat.xlCSVWindows)
            wb.Close(False)
            app.Quit()
            Console.WriteLine("Done!")

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Stanly Fan Friday, May 4, 2018 10:17 AM
    Wednesday, May 2, 2018 7:01 AM
  • Here is an alternative solution using Spire.XLS

    Imports Spire.Xls
    Namespace xls2csv
           Friend Class Program
                  Shared Sub Main(ByVal args() As String)
                         Dim workbook As New Workbook()
                         workbook.LoadFromFile("..\ExceltoCSV.xls ")
                         Dim sheet As Worksheet = workbook.Worksheets(0)
                         sheet.SaveToFile("sample.csv", ",", Encoding.UTF8)
                  End Sub
           End Class
    End Namespace

      
    Thursday, May 3, 2018 1:21 AM