Share via

AppleScript "Save as PDF" fails

Anonymous
2015-09-21T09:12:41+00:00

This example is from Excel but the error occurs also in Word and PowerPoint although it's slightly different with Word.

My test script looks like this:

tell application "Microsoft Excel"

    tell active sheet

        save in "/Users/xxx/Desktop/test.xlsx"

        save in "/Users/xxx/Desktop/test.pdf" as PDF file format

    end tell

end tell

While the first statement (saving als xlsx) works fine. The second one (saving as PDF) fails with several errors:

Error while printing.

error "Microsoft Excel got an error: Parameter error." number -50

And the following entries in the system log:

21/09/2015 11:08:51.837 Microsoft Excel[494]: Printing failed because PMSessionBeginCGDocumentNoDialog() returned -61.

21/09/2015 11:08:51.888 sandboxd[230]: ([494]) Microsoft Excel(494) deny file-write-create /Users/xxx/Desktop/test.pdf

Does anybody hava an idea? The script worked perfect with Office 2011 but not with Office 2016

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2017-10-10T22:40:36+00:00

It is important to do this in AppleScript—not VBA—because I want to attach it to a recurring calendar event and have it triggered automatically. Using AppleScript, I could also set it up as a folder action and have it run when a file is dropped there, or any other number of ways to automate its use that would not be possible using VBA within Office.

I gave up trying to get Excel to behave as documented in its Dictionary. I instead solved this with Acrobat.

    --set up some file path locations in variables

    set sourceFile to "CincyData:Dropbox:PRODUCTION OFFICE:DriverSchedule.xlsx" as POSIX file

    set destFile to "CincyData:Dropbox:TOOLS:xFer:DriverSchedule.pdf"

    set printDest to "CincyData:Dropbox:TOOLS:Print-Office"

        tell application "Adobe Acrobat"

   set tempFile to open file sourceFile

        -- add a delay b/c Acrobat uses a web service to convert the file

   delay 7

   save active doc to destFile

   close active doc

    end tell

    -- Duplicate the new pdf to a folder that has a folder action set up to automatically print the file, then delete it.

    tell application "Finder"

duplicate destFile to printDest

    end tell

Was this answer helpful?

0 comments No comments

5 additional answers

Sort by: Most helpful
  1. Anonymous
    2017-10-09T00:15:40+00:00

    I was so hopeful this would solve this problem. At least then I could tell the Finder to move the resulting file for me. But the example code still results in [a different] error:

    error "Microsoft Excel got an error: Can’t make PDF file format into type type." number -1700 from PDF file format to type

    Has Microsoft completely broken save as PDF in AppleScript? I see lots of people reporting this issue. I see many fewer solutions offered and none of those have actually worked.

    If I correctly read above comments, it sounds like MS wants us to call AppleScript from within VBA? If true the main problem with that is that it defeats the biggest benefit of using AppleScript—the ability to create cross-application workflows. I don't want to have to open an Excel file and click a button to make a PDF. I want a calendar event to run a script that drops a PDF of that file in my inbox on a regular schedule.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2015-10-22T09:25:45+00:00

    After studying the Document "App Sandbox in Depth" it seems just consequent for me what Microsoft did with Office 2016 and AppleScript. Regarding this document sandboxed apps can just write automatically (triggered by AppleScript) within the path ~/Library/Containers/com.microsoft.excel/. So doing something like this

    tell application "Microsoft Excel"

        tell active sheet

            save in path to temporary items from user domain as PDF file format

        end tell

    end tell

    should work. Files can afterwards be found in ~/Library/Containers/com.microsoft.excel/Data/Library/Caches/TemporaryItems/

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2015-09-21T10:43:17+00:00

    Mmmm, I notice that it also blow in the script editor now with 2016.

    Another problem.

    Will try some things this week and if I got a solution I will post it here

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2015-09-21T10:27:43+00:00

    The MacScript function we use in 2011 to run the script is not working anymore with most scripts in 2016.

    I create a example for mailing in 2016 but not have try to save as pdf yet.

    In 2016 MS want us to use the AppleScriptTask command that is new in 2016.

    See how it is working on this page

    http://www.rondebruin.nl/mac/macmail/macmail2016.htm

    Will try to update my PDF page for the Mac also soon so it is also working in 2016

    Was this answer helpful?

    0 comments No comments