win32ole word find replace

J

jhn Vln

Hi everyone,

I'm trying to use ruby to "find and replace" feature in Word and
although i know the word is in the file it says it does not find it.
What the name of god am i doing wrong here(please see code below).

Any input is greatly appreciated. Thank you,

require 'win32ole'
word = WIN32OLE.new('Word.Application')
word.Visible = true
doc = word.documents.open("C:/test_file.doc")
word.selection.wholestory
find = word.Selection.Find
find.text = "is" # simplest test string ever
if word.selection.find.found
p "Found"
else
p "Crapppppppp"
end
 
D

David Mullet

jhn said:
Hi everyone,

I'm trying to use ruby to "find and replace" feature in Word and
although i know the word is in the file it says it does not find it.
What the name of god am i doing wrong here(please see code below).

Any input is greatly appreciated. Thank you,

require 'win32ole'
word = WIN32OLE.new('Word.Application')
word.Visible = true
doc = word.documents.open("C:/test_file.doc")
word.selection.wholestory
find = word.Selection.Find
find.text = "is" # simplest test string ever
if word.selection.find.found
p "Found"
else
p "Crapppppppp"
end

You're very close, but you need to call the Execute() method on the Find
object:

find = word.Selection.Find
find.Execute()
if word.selection.find.found
p "Found"
else
p "Crapppppppp"
end

David

http://rubyonwindows.blogspot.com/2007/11/find-replace-with-ms-word.html
http://rubyonwindows.blogspot.com/search/label/word
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top