如何使用applescript将单个Excel工作表另存为PDF

时间:2016-11-07 23:09:56

标签: excel applescript pdf-generation

我使用以下代码:

set PDFName to --desired path and name of PDF file to save
set excelName to --desired path and name of Excel file to open

tell application "/Applications/Microsoft Excel.app"
    set isRun to running
    set wkbk1 to open workbook workbook file name excelName

    --Problem
    save (sheet "Invoice" of wkbk1) in PDFName as PDF file format
    --/Problem

    close wkbk1 saving no
    if not isRun then quit
end tell

我最终将整个工作簿转换为60页长PDF,而不是仅仅一张!难道我做错了什么?我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我尝试仅在新工作表中复制/粘贴值,但我失败了。粘贴特殊...没有。

相反,我提取了第一个主文档的工作表并将其复制到一个新的临时文档中,然后我将临时文档保存为PDF并关闭新的临时文档和主文档。

它适用于下面的脚本:

set PDFPath to "Users:imac27:Desktop:Test22.PDF" -- destination of the PDF

tell application "Microsoft Excel"
activate
set SourceDoc to front document
set NameWB to name of front document -- get name of the document (for close)
tell SourceDoc to set SourceSheet to active sheet -- get sheet to be extracted
set newDoc to make new workbook -- create new workbook
tell newDoc
    set FirstSheet to first sheet of newDoc
    copy worksheet SourceSheet after FirstSheet -- copy sheet to be extracted in new doc
end tell
set display alerts to false -- to avoid warning "are you sure...?)
delete FirstSheet -- delete defautl sheet of new doc to keep only the copied sheet
save active sheet in PDFPath as PDF file format -- save temp workbook as PDf
close active workbook saving no -- close temp workbook
close workbook NameWB saving no -- close main document
end tell
相关问题