Results 1 to 3 of 3
  1. #1
    behnam is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    72

    How to convert Excel file type from csv to xls

    Hi,



    Can anyone help me with the vba code to change the file type of all files (two excel files called first.csv and second.csv) in a folder from csv to xls. I tried simply changing the extension for the filename name but this dosent work. Thanks.

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    no, the code must open it in Excel, then SAVE AS to the xls format.
    NOTE : you must add the Excel object library, in VBE menu,Tools, References.

    Code:
       'import all files in folder
    '------------------
    Public Sub ImportAllXLFilesAndSheets(ByVal pvDir)
    '------------------
    Dim vFil, vTargT
    Dim i As Integer
    Dim sTbl As String, sSql As String
    Dim tdf As TableDef
    Dim colSheets As New Collection
    Dim vSheet, vQry, oFolder, oFile
    
    sTbl = "xlFile"    'linked xl file to import
    
    On Error GoTo errImp
    Set Db = CurrentDb
    
    Select Case True
     Case pvDir = ""
        MsgBox "No source folder given", vbCritical, "Error"
     
    Case Else
        DoCmd.Hourglass True
        DoCmd.SetWarnings False
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set oFolder = fso.GetFolder(pvDir)
        For Each oFile In oFolder.Files
            If InStr(oFile, ".csv") > 0 Then         'only csv files
                vFil = pvDir & oFile.Name
                         'open the wb
                SaveCsvAsWB vFil
            End If
    
        Next  'file
    End Select
    
    Set colSheets = Nothing
    Set oFile = Nothing
    Set oFolder = Nothing
    DoCmd.Hourglass False
    DoCmd.SetWarnings True
    Exit Sub
    errImp:
    MsgBox Err.Description, vbCritical, "ImportAllXLFilesAndSheets()" & Err
    Exit Sub
    Resume
    End Sub
    
    '-----------------
    Private Sub SaveCsvAsWB(ByVal pvFile)
    '-----------------
    Dim col As New Collection
    Dim xl As Excel.Application
    
      '=========== YOU MUST HAVE 'EXCEL OBJECT LIBRARY' loaded via VBE menu, TOOLS, REFERENCES
    Set xl = CreateObject("excel.application")
    With xl
        .Workbooks.Open pvFile
        .ActiveWorkbook.SaveAs pvFile & ".xls", xlExcel8
        .ActiveWorkbook.Close False
    End With
    Set xl = Nothing
    End Sub

  3. #3
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Quote Originally Posted by behnam View Post

    ...two excel files called first.csv and second.csv...
    Also, note that these files are not 'Excel files,' hence the need to save them as Excel files!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 9
    Last Post: 11-20-2013, 03:16 PM
  2. Replies: 5
    Last Post: 08-13-2012, 01:25 AM
  3. Convert access file to exe file
    By Mina Garas in forum Access
    Replies: 2
    Last Post: 12-30-2011, 10:11 AM
  4. Replies: 5
    Last Post: 12-14-2011, 08:24 AM
  5. convert property TYPE to ENUM
    By ajetrumpet in forum Programming
    Replies: 4
    Last Post: 09-10-2010, 11:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums