【问题标题】:Microsoft Word - Can't export PDF file using applescriptMicrosoft Word - 无法使用 applescript 导出 PDF 文件
【发布时间】:2020-04-21 10:50:27
【问题描述】:

我尝试将 docx 转换为 pdf 并通过从原始文件名中获取一些字母来重命名文件。

在 Automator 中,我使用了“Get Specified Finder Items”和“Run AppleScript”。

我执行并在 Microsoft Word 中收到一条弹出消息

“无法导出文件。出了点问题,您的文件现在无法导出。请稍后再试。”

任何人有任何想法有什么问题吗?请帮帮我。 谢谢。

  • Microsoft Word:16.35 (20030802)
  • 自动机:2.10 (492)
  • macOS:10.15.4
on run {input, parameters}
    repeat with aFile in input
        tell application "Microsoft Word"
            launch
            try
                open aFile
                tell document 1

                    set fileName to aFile as text
                    set newName to "p" & (text 14 thru 15 of fileName) & "-" & (text 11 thru 12 of fileName) & "-" & (text 8 thru 9 of fileName)

                    set pdfOutput to (newName & ".pdf")
                    save as file name pdfOutput file format format PDF


                end tell
                close document 1 saving yes
            on error
                try
                    close document 1 saving no
                end try
            end try
        end tell
    end repeat
    return input
end run

【问题讨论】:

    标签: ms-word applescript automator


    【解决方案1】:

    如果我理解了你的意图,问题就出在这行:

    set fileName to aFile as text
    

    你应该改成

    set fileName to the name as text
    

    如果没有进一步的实验,我无法确定,部分原因是它完全取决于您的路径名称是什么样的,但在您拥有的代码中

    set fileName to aFile as text
    

    表示fileName包含文档路径+文件的完整AFS路径名,用“:”分隔,表达式如

    text 14 thru 15 of fileName
    

    正在从 path 中选择字符,可能包括“:”字符,这可能会导致您看到的问题,而我认为您的意图是从 文件名中选择字符。

    (另外,我还没有研究文件名不包含您的代码当前引用的 8 个或更多字符的情况。)

    【讨论】:

    • 感谢您的回复。我对applescript很陌生。我不知道 aFile 包含完整的 AFS 路径名。我只想获取不包含文档路径的文件名。我的文件名如下所示:xxxxxx_YY-MM-DD_xxxx.docx 并尝试重命名为:pDD-MM-YY.pdf(DD、MM、YY 是数字。)我尝试按照您的建议更改代码。 set fileName to the name as text 也试试这个。 set fileName to document name as text 但是没有导出pdf文件。有什么我忽略的吗?
    • @Anonymademoiselle 如果您不提供路径名,则 .pdf 可能会保存在意外文件夹中。例如。如果您的用户名是“用户名”,它可能位于 /Users/username/Library/Containers/com.microsoft.Word/Data/Documents 中。所以进去看看吧。 (在 Finder 中,打开 Go 菜单时按住 Alt(选项),然后单击 Library 选项以打开 Library 文件夹 - 否则 Finder 会隐藏 Library。
    【解决方案2】:

    如果您按原样运行脚本,PDF 将最终保存在 /Users/<your_name>/Library/Containers/com.microsoft.Word/Data/Documents/ 之类的文件夹中(就像 @slightly-snarky 评论的那样)。

    试试这个方法:

    on run {input, parameters}
        repeat with aFile in input
            tell application "Finder" to set theFolder to (folder of aFile) as text -- !!!
            tell application "Microsoft Word"
                launch
                try
                    open aFile
                    tell document 1
    
                        set fileName to aFile as text
                        set loc to location of aFile
                        set newName to "p" & (text 14 thru 15 of fileName) & "-" & (text 11 thru 12 of fileName) & "-" & (text 8 thru 9 of fileName)
    
                        set pdfOutput to (theFolder & newName & ".pdf") -- !!!
                        save as file name pdfOutput file format format PDF
    
    
                    end tell
                    close document 1 saving yes
                on error
                    try
                        close document 1 saving no
                    end try
                end try
            end tell
        end repeat
        return input
    end run
    

    更改在以-- !!! 为 cmets 的行中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-04
      • 1970-01-01
      相关资源
      最近更新 更多