0
votes

I tried to convert docx to pdf and renamed the file by getting some letters from the original file name.

In Automator, I used "Get Specified Finder Items" and "Run AppleScript".

I executed and got a pop-up message in the Microsoft Word

"Can't export file. Something went wrong, and your file can't be exported now. Please try again later."

Anyone has any ideas what's wrong? Please help me out. Thanks.

  • Microsoft Word : 16.35 (20030802)
  • Automator : 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
2

2 Answers

0
votes

If I have understood your intent, the problem is this line:

set fileName to aFile as text

which you should change to

set fileName to the name as text

I cannot be sure without further experiment and partly because it depends on exactly what your path names look like, but in the code you have

set fileName to aFile as text

which means that fileName contains the full AFS pathname of the document path + file, with ":" separators, and that expressons such as

text 14 thru 15 of fileName

are picking characters from the path, perhaps including ":" characters, which may be causing the problem you see, whereas I think your intent is to pick characters from the file name.

(Also, I have not looked at the scenario where the file name does not contain the 8 or more characters your code currently references.)

0
votes

If you run your script as is, the PDF will end up in a folder like /Users/<your_name>/Library/Containers/com.microsoft.Word/Data/Documents/ (like @slightly-snarky commented).

Try it this way:

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

The changes are in the lines with -- !!! as comments