David C. Nelson
David C. Nelson

Reputation: 60

How can I create a new Excel 2011 spreadsheet and save it to a particular directory/filename using Applescript

I am trying to write an Applescript program to create a spreadsheet, add data to it, and save the spreadsheet in a particular folder. I have code to create, fill-in, and save the spreadsheet, but I'm not able to specify the folder where the new file is to be saved.

The file is saved in the last folder where I saved a spreadsheet. If I include the full path to the folder where I want it saved("/Users/david/Documents/test/foo.xls"), Excel 2011 creates a file named ":Users:david:Documents:test:foo.xls" in the folder where I had previously manually saved a spreadsheet. So for example, if I manually open Excel and create a spreadsheet and save it in a subfolder of my Documents folder named important, and now run the following script, the file will be created in the important folder, not the test folder.

tell application "Microsoft Excel"
    set wb to make new workbook
    tell active sheet
        -- Lots of code to fill in the data
    end tell
    set fn to "/Users/david/Documents/test/foo.xls"
    save workbook as wb filename fn file format Excel98to2004 file format overwrite yes
    close active workbook saving no
end tell

From various posts I've read, it appears one should be able to specify the full path, but the questions usually deal with previous versions of Excel. Also, I'm running Mountain Lion if that makes a difference.

Upvotes: 0

Views: 2374

Answers (1)

jackjr300
jackjr300

Reputation: 7191

The path type must be HFS instead of posix path.

Use this:

set fn to (posix file "/Users/david/Documents/test/foo.xls") as string

Or:

set fn to "diskName:Users:david:Documents:test:foo.xls"

Upvotes: 1

Related Questions