我有这个 Ruby 脚本可以将我的 Word 文档转换为 .txt 格式。它工作正常,可以相互转换多个文件,但有时会出现错误。我如何让脚本继续下一个文件,跳过给出错误的文件?请不要对使用其他技术提出任何建议,我对这个很满意。
require 'win32ole'
wdFormatDOSText = 4
word = WIN32OLE.new('Word.Application')
word.visible = false
begin
ARGV.each do|file|
myFile = word.documents.open(file)
new_file = file.rpartition('.').first + ".txt"
word.ActiveDocument.SaveAs new_file, wdFormatDOSText
puts new_file
word.activedocument.close(false) # no save dialog, just close it
end
rescue WIN32OLERuntimeError => e
puts e.message
puts e.backtrace.inspect
sleep 30
ensure
word.quit
end
sleep 3