Applescript / Excel将图表保存为图像

时间:2016-03-28 17:27:17

标签: excel image charts applescript applescript-excel

我正在尝试将名为图表的图表中的第一个图表保存为桌面的PNG。我在“另存为图片”行中收到以下错误:

错误“Microsoft Excel出错:参数错误。”数字-50

tell application "Microsoft Excel"
    set theChart to first chart object of sheet "Charts"
    set filePath to "Macintosh HD:Users:User:Desktop:Chart.png" as text
    save as picture theChart picture type save as PNG file file name filePath
end tell

我也试过告诉图表本身并收到同样的错误:

tell theChart
    save as picture picture type save as PNG file file name filePath
end tell

从字典和其他已知示例中,语法似乎是正确的,但任何帮助都将非常感激。

1 个答案:

答案 0 :(得分:0)

使用脚本时,

Microsoft Office 2016 对安全性非常严格。

  • 如果文件已存在,则错误编号为-50。
  • 如果最近文件列表中此文件夹中没有文件 Excel ,然后错误编号-50。

解决方案,请使用:

set myFolder to path to desktop folder
tell application "Microsoft Excel"
    alias myFolder --  first,  need this line to grant access to the folder before saving the PNG file , otherwise --> Parameter error.  number -50
    my deleteIfExists(myFolder, "Chart.png") -- second,  if the file already exists, then delete the file,  otherwise --> Parameter error.  number -50

    set theChart to first chart object of sheet "Charts"
    save as picture theChart picture type save as PNG file file name ((myFolder as string) & "Chart.png")
end tell

on deleteIfExists(f, tName) -- move the file to the trash
    tell application "Finder" to tell item tName of folder f to if exists then delete
end deleteIfExists