简体   繁体   中英

Applescript: Open Excel File and Save in a Different Format (same name and path)

I have a folder of .xls files of which some are genuine excel files and some are excel xml files (but still with the .xls extension). To work with the files in a separate program, I'm trying to convert them all to a consistent format (Excel 98-2004) and want to use an applescript to do this while retaining the original file names and directory structure.

As of now, I have a working loop and list of files, and am accomplishing everything but the save function:

set file_Name to "/Users/me/path/to/data.xls"

tell application "Microsoft Excel"
    activate
    open file_Name
    save workbook as workbook (active workbook) filename file_Name file format (Excel98to2004 file format) with overwrite
    close workbooks
    quit
end tell

When I run this code, I get the following replies:

tell application "Microsoft Excel"
    activate
    open "/Users/drewmcdonald/Desktop/Madhupur_10February2014.xls"
    get active workbook
        --> active workbook
    save workbook as workbook (active workbook) filename "/Users/drewmcdonald/Desktop/test.xlsx"
        --> missing value
    close every workbook
    quit
end tell

The spreadsheets are successfully opening and closing, but the file format is not changing and I don't know what to make of the "missing value" error. Any idea how to get this to work? Thanks!

EDIT: Just noticed that the file format parameter is missing from the reply text, so I'm guessing the problem is in there.

Got it.

I needed to set a workbook name variable to get full name of active workbook before trying to save it. This is because the workbook name that Excel assigns as it opens sheets is somewhat inconsistent. Final code looks like this:

set file_Name to "/Users/me/path/to/data.xls"

tell application "Microsoft Excel"
    activate
    open file_Name
    set wkbk_name to get full name of active workbook
    save workbook as workbook wkbk_name filename file_Name file format Excel98to2004 file format with overwrite
    close workbooks
    quit
end tell

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM