【发布时间】:2014-07-18 20:07:07
【问题描述】:
我正在尝试像这样替换文档中的文本:
require 'win32ole'
def replace_doc(doc, find, repl)
begin
word = WIN32OLE.new('Word.Application')
word.Visible = true
doc = word.Documents.Open(doc)
word.Selection.HomeKey(unit=6)
finder = word.Selection.Find
finder.Text = "[#{find}]"
while word.Selection.Find.Execute
word.Selection.TypeText(text=repl)
end
doc.SaveAs(doc)
doc.Close
rescue Exception => e
puts e.message
puts "Unable to edit file."
end
end
def main()
puts "File: "
doc = gets.chomp()
puts "Find: "
find = gets.chomp()
puts "Replace with: "
repl = gets.chomp()
replace_doc(doc, find, repl)
end
main()
我在 Windows XP 上运行 Ruby 2.0。 WINWORD.exe 进程启动(我在任务管理器中看到它),并且没有引发异常。但是,当我转到文档时,我希望替换的文本都没有——是。到底是怎么回事?我已经从here 复制了代码(除了一些东西)。
【问题讨论】:
标签: ruby windows ms-word win32ole