Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have to work with an old application that can only export XLS files and I write programs in .Net using the EPPlus library that is only capable of reading XLSX files.

What is the easiest way to batch convert from XLS to XLSX?

share|improve this question

3 Answers 3

I'd recommend using a macro to process the files within a folder to convert them from xls to xlsx. This code assumes that the files are all located within one folder and that all xls files need to be converted, but if you wanted to select individual files this code could be updated.

This code would need to be run from an Excel 2007 or Excel 2010 workbook.

Option Explicit

' Convert all xls files in selected folder to xlsx

Sub convertXLStoXLSX()

    Dim FSO As Scripting.FileSystemObject
    Dim strConversionPath As String
    Dim fFile As File
    Dim fFolder As Folder
    Dim wkbConvert As Workbook


    ' Open dialog and select folder
    With Application.FileDialog(msoFileDialogFolderPicker)
        .AllowMultiSelect = False
        .Show
        strConversionPath = .SelectedItems(1)
    End With

    Set FSO = New Scripting.FileSystemObject

    ' Check if the folder exists
    If FSO.FolderExists(strConversionPath) Then
        Set fFolder = FSO.GetFolder(strConversionPath)

        ' Loop through files, find the .xls files
        For Each fFile In fFolder.Files
            If Right(fFile.Name, 4) = ".xls" Then
                Application.DisplayAlerts = False
                wkbConvert = Workbooks.Open(fFile.Path)
                ' Save as XML workbook - if file contains macros change FileFormat:=52
                wkbConvert.SaveAs FSO.BuildPath(fFile.ParentFolder, Left(fFile.Name, Len(fFile.Name) - 4)) & ".xlsx", FileFormat:=51
                wkbConvert.Close SaveChanges:=False
                ' Delete original file
                fFile.Delete Force:=True
                Application.DisplayAlerts = True
            End If
        Next fFile

    End If

End Sub

If the files you are converting contain macros then you would need to update the 'FileFormat:=51' to read 'FileFormat:=52'. Or if you don't need to macro code in the converted files you could leave it alone and it will remove the macros when it converts it to the xlsx format.

share|improve this answer
    
What if I want to convert .CSV to .XLSX? –  Boris_yo Sep 22 at 8:43

Check out Office Migration Planning Manager.

The toolkit also contains the Office File Converter (OFC), which enables bulk document conversions from binary to OpenXML formats. (Technet)

Overview on Technet

Download Link

share|improve this answer
Sub SaveAllAsXLSX()
Dim strFilename As String
Dim strDocName As String
Dim strPath As String
Dim wbk  As Workbook
Dim fDialog As FileDialog
Dim intPos As Integer
Dim strPassword As String
Dim strWritePassword As String
Dim varA As String
Dim varB As String
Dim colFiles As New Collection
Dim vFile As Variant
Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
With fDialog
    .Title = "Select folder and click OK"
    .AllowMultiSelect = True
    .InitialView = msoFileDialogViewList
    If .Show  -1 Then
        MsgBox "Cancelled By User", , "List Folder Contents"
        Exit Sub
    End If
    strPath = fDialog.SelectedItems.Item(1)
    If Right(strPath, 1)  "\" Then strPath = strPath + "\"
End With
If Left(strPath, 1) = Chr(34) Then
    strPath = Mid(strPath, 2, Len(strPath) - 2)
End If
Set obj = CreateObject("Scripting.FileSystemObject")
RecursiveDir colFiles, strPath, "*.xls", True
For Each vFile In colFiles
        Debug.Print vFile
    strFilename = vFile
    varA = Right(strFilename, 3)
    If (varA = "xls" Or varA = "XLSX") Then
     Set wbk = Workbooks.Open(Filename:=strFilename)
       If wbk.HasVBProject Then
              wbk.SaveAs Filename:=strFilename & "m", FileFormat:=xlOpenXMLWorkbookMacroEnabled
            Else
               wbk.SaveAs Filename:=strFilename & "x", FileFormat:=xlOpenXMLWorkbook
            End If
            wbk.Close SaveChanges:=False
           obj.DeleteFile (strFilename)
    End If
Next vFile

End Sub
Public Function RecursiveDir(colFiles As Collection, _
                             strFolder As String, _
                             strFileSpec As String, _
                             bIncludeSubfolders As Boolean)

    Dim strTemp As String
    Dim colFolders As New Collection
    Dim vFolderName As Variant

    'Add files in strFolder matching strFileSpec to colFiles
    strFolder = TrailingSlash(strFolder)
    strTemp = Dir(strFolder & strFileSpec)
    Do While strTemp  vbNullString
        colFiles.Add strFolder & strTemp
        strTemp = Dir
    Loop

    If bIncludeSubfolders Then
        'Fill colFolders with list of subdirectories of strFolder
        strTemp = Dir(strFolder, vbDirectory)
        Do While strTemp  vbNullString
            If (strTemp  ".") And (strTemp  "..") Then
                If (GetAttr(strFolder & strTemp) And vbDirectory)  0 Then
                    colFolders.Add strTemp
                End If
            End If
            strTemp = Dir
        Loop

        'Call RecursiveDir for each subfolder in colFolders
        For Each vFolderName In colFolders
            Call RecursiveDir(colFiles, strFolder & vFolderName, strFileSpec, True)
        Next vFolderName
    End If

End Function
Public Function TrailingSlash(strFolder As String) As String
    If Len(strFolder) > 0 Then
        If Right(strFolder, 1) = "\" Then
            TrailingSlash = strFolder
        Else
            TrailingSlash = strFolder & "\"
        End If
    End If
End Function

share|improve this answer
    
Instead of just pasting a bunch of code, could you maybe also explain a little what it does, how to use it, and what the limitations are (if any)? That'd be more helpful for future visitors. –  slhck Nov 5 '13 at 11:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.