tags:

views:

2470

answers:

5

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?

+2  A: 

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).

Lou Franco
Thank you Lou for both the answer and for correcting the file extension name in the question. I may look at you suggestion when I need more comprehensive solution. As for the moment I am OK with VBA
kristof
A: 

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.

+2  A: 

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.

Duncan Smart
Thanks, it was helpful
kristof
A: 

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

BKimmel
+3  A: 

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
Robert Mearns
Thanks Robert, it worked very well. The only thing i changed was FileFormat:=XlFileFormat.xlXMLSpreadsheet ( I am using Excel 2003)
kristof
Can the code above be written in php?
chupinette