My script shuts down when opening a corrupt MsWord file
I want to trap unopenable files and continue with next file
How do I error trap this error before the MsWord error shuts downs the script???
MsWord error says: The open XLM file ooo30075.docx cannot be open because there are problems with the content or the file name might contain invalid characters (for example, /)
No error detail available
It then forces me to hit Ok on two dialog boxes to continue.
Help!
Thanks!
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application "Finder"
try
set filelist to files of folder POSIX file "/Users/xxxx/Documents/DataRecoverydocs/Document/docx/" as alias list
set num_Of_files to get count of filelist
on error error_message number error_number
display dialog " Could NOT get files from folder into a file list " & return & error_message & " error num. " & error_number giving up after 2
endscript()
end try
end tell
------------------- This section begins opening each file in the filelist --------------
repeat with f in filelist --repeats till eof
delay 1
tell application "Microsoft Word"
activate
try
set currentfilename to ""
open f
delay 1
on error error_message number error_number
display dialog " Could NOT open the file in Word! " & return & error_message & " error num. " & error_number giving up after 2
tell application "Microsoft Word" to close documents saving no
endscript()
end try
-- ??????? how to know if file opened, and did not have corrupt name... before MSWord shuts down the script with its error message???
-- ??? the error trap on open f did not work ????
set theActiveDoc to the active document
set theContent to content of text object of active document as string
try
set thefirstline to paragraph 1 of theContent --This is the heading or (name) of the recovered document
------- The MSWord error stops the progress of script because it was a corrupted file and couldnt open file --how can i trap this error???
on error error_message number error_number
display dialog " Filename is corrupted--Word can not open file! " & return & error_message & " error num. " & error_number giving up after 2
tell application "Microsoft Word" to close documents saving no
endscript()
--display dialog "first line " & thefirstline giving up after 2
end try
end tell
end repeat
tell me
activate
set ScreenUpdating to true
display dialog "Script is Finished....Closing Script" giving up after 1
tell application "Microsoft Word" to close documents saving no
delay 2
tell application "Microsoft Word" to quit
endscript()
end tell
on endscript()
say "The script is stoping"
error number -128
end endscript