Applescript: Open Excel File and Save in a Differe

2019-09-03 19:40发布

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.

1条回答
戒情不戒烟
2楼-- · 2019-09-03 20:04

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
查看更多
登录 后发表回答