kristof
kristof

Reputation: 53824

How to automate converting Excel xls files to Excel xml format?

I have about 200 Excel files that are in standard Excel 2003 format.

I need them all to be saved as Excel xml - basically the same as opening each file and choosing Save As... and then choosing Save as type: XML Spreadsheet

Would you know any simple way of automating that task?

Upvotes: 5

Views: 29663

Answers (6)

Kishore Relangi
Kishore Relangi

Reputation: 2034

Const xlXLSX = 51

REM 51 = xlOpenXMLWorkbook (without macro's in 2007-2013, xlsx)
REM 52 = xlOpenXMLWorkbookMacroEnabled (with or without macro's in 2007-2013, xlsm)
REM 50 = xlExcel12 (Excel Binary Workbook in 2007-2013 with or without macro's, xlsb)
REM 56 = xlExcel8 (97-2003 format in Excel 2007-2013, xls)

dim args
dim file
dim sFile
set args=wscript.arguments

dim wshell
Set wshell = CreateObject("WScript.Shell")

Set objExcel = CreateObject("Excel.Application")

Set objWorkbook = objExcel.Workbooks.Open( wshell.CurrentDirectory&"\"&args(0))

objExcel.DisplayAlerts = FALSE

objExcel.Visible = FALSE

objWorkbook.SaveAs wshell.CurrentDirectory&"\"&args(1), xlXLSX

objExcel.Quit

Wscript.Quit

Upvotes: 0

Robert Mearns
Robert Mearns

Reputation: 11986

Here is a routine that will convert all files in a single directory that have a .xls extension.

It takes a straight forward approach. Any VBA code in a workbook is stripped out, the workbook is not saved with a .xlsm extension. Any incompatability warning are not dislayed, instead the changes are automatically accepted.

Sub Convert_xls_Files()

Dim strFile As String
Dim strPath As String

    With Application
        .EnableEvents = False
        .DisplayAlerts = False
        .ScreenUpdating = False
    End With
'Turn off events, alerts & screen updating

        strPath = "C:\temp\excel\"
        strFile = Dir(strPath & "*.xls")
'Change the path as required

    Do While strFile <> ""
        Workbooks.Open (strPath & strFile)
        strFile = Mid(strFile, 1, Len(strFile) - 4) & ".xlsx"
        ActiveWorkbook.SaveAs Filename:=strPath & strFile, FileFormat:=xlOpenXMLWorkbook
        ActiveWorkbook.Close True
        strFile = Dir
    Loop
'Opens the Workbook, set the file name, save in new format and close workbook

    With Application
        .EnableEvents = True
        .DisplayAlerts = True
        .ScreenUpdating = True
    End With
'Turn on events, alerts & screen updating

End Sub

Upvotes: 8

BKimmel
BKimmel

Reputation: 611

Sounds like a job for my favorite-most-underrated language of all time: VBScript!!

Put this in a text file, and make the extension ".vbs":

set xlapp = CreateObject("Excel.Application")
set fso = CreateObject("scripting.filesystemobject")
set myfolder = fso.GetFolder("YOURFOLDERPATHHERE")
set myfiles = myfolder.Files
for each f in myfiles
   set mybook = xlapp.Workbooks.Open(f.Path)
   mybook.SaveAs f.Name & ".xml", 47
   mybook.Close
next

I haven't tested this, but it should work

Upvotes: 1

Duncan Smart
Duncan Smart

Reputation: 32058

Open them all up, and then press ALT+F11 to get to macro editor and enter something like:

Sub SaveAllAsXml()
    Dim wbk As Workbook
    For Each wbk In Application.Workbooks
        wbk.SaveAs FileFormat:=XlFileFormat.xlXMLSpreadsheet
    Next
End Sub

And then press F5 to run it. May need some tweaking as I haven't tested it.

Upvotes: 3

iafonov
iafonov

Reputation: 5192

The simplest way is to record macro for one file and then manually edit macros to do such actions for files in folder using loop. In macro you can use standart VB functions to get all files in directory and to filter them. You can look http://www.xtremevbtalk.com/archive/index.php/t-247211.html for additional information.

Upvotes: 0

Lou Franco
Lou Franco

Reputation: 89152

You could adapt the code I posted here:

http://www.atalasoft.com/cs/blogs/loufranco/archive/2008/04/01/loading-office-documents-in-net.aspx

It shows how to save as PDF (Word is shown in the blog, but if you download the solution, it has code for Excel and PPT).

You need to find the function for saving as the new format instead of exporting (probably the easiest way is to record a macro of yourself doing it in Excel and then looking at the code).

Upvotes: 3

Related Questions