I need to open a protected Microsoft Word document via AppleScript. I know the password, but i can't figure out how to code it with AppleScript.
I tried some variations of AppleScript code, without success.
set the myfile to ("/Users/alemac/Desktop/teste.docx")
tell application "Microsoft Word"
open myfile password document "123"
end tell
Word opens the file (like Finder does), and the password dialog opens waiting for password.
After running your version of the AppleScript code several times, the command open myfile password document
actually worked for me maybe 3 or 4 sporadic times. Eventually it stopped working and the password dialog box opened every time. After wracking my brain trying to figure out why this was happening, for nearly an hour, I reluctantly decided to use UI scripting to handle the password dialog box, in the event it reared its ugly head.
This AppleScript code works for me using the latest version of macOS Mojave and Microsoft Word version 16.24
set the myfile to (path to desktop as text) & "teste.docx"
set thePassword to 123
tell application "Microsoft Word"
activate
ignoring application responses
open myfile password document thePassword
delay 1
end ignoring
end tell
try
tell application "System Events"
repeat until exists of window "Password" of application process "Word"
delay 0.1
end repeat
click window "Password" of application process "Word"
delay 0.2
click text field 1 of window "Password" of application process "Word"
delay 0.2
keystroke thePassword
delay 0.2
keystroke return
end tell
end try
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.