# Win32ole word find replace

**URL:** <https://www.ruby-forum.com/t/win32ole-word-find-replace/177590>\
**Category:** Ruby\
**Created:** [October 20, 2009, 5:38pm UTC](https://www.ruby-forum.com/t/win32ole-word-find-replace/177590 "2009-10-20T17:38:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimache](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c67d28/32.png) [@jimache](https://www.ruby-forum.com/u/jimache)\
**Post date:** [October 20, 2009, 5:38pm UTC](https://www.ruby-forum.com/t/win32ole-word-find-replace/177590/1 "2009-10-20T17:38:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jimache](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c67d28/32.png) [@jimache](https://www.ruby-forum.com/u/jimache)\
**Post date:** [October 21, 2009, 3:51am UTC](https://www.ruby-forum.com/t/win32ole-word-find-replace/177590/2 "2009-10-21T03:51:49Z")

</div>

jhn Vln wrote:

> 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

> **[Find & Replace with MS Word](http://rubyonwindows.blogspot.com/2007/11/find-replace-with-ms-word.html)**
>
> While browsing through some DZone Ruby Snippets , I came across this nice little example of using Ruby to automate Find & Replace in Micros...

> **[Ruby on Windows](http://rubyonwindows.blogspot.com/search/label/word)**
>
> On using the Ruby programming language on the Microsoft Windows platform
