2016-11-07 39 views
0

我使用下面的代码:如何使用AppleScript的一个单一的Excel工作表保存为PDF

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的整个工作簿,而不是只有一张单!难道我做错了什么?我怎样才能解决这个问题?

+0

我认为你的答案是#2链接:[链接](http://stackoverflow.com/questions/37394771/applescript-excel-2016-save-as -pdf) – pbell

+0

我实际上使用这个帖子来写这个编码。我遇到的问题与他认为的不同...... –

+0

我自己测试了它,尽管“保存活动工作表”,它仍然保存了Excel 2011中的所有工作表。它似乎是Excel的错误!然后,我建议在保存之前删除您不想转换的所有工作表...然后只转换所需工作表。 ...并关闭工作簿而不保存。 – pbell

回答

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 
相关问题