win32ole word find replace

Discussion in 'Ruby' started by jhn Vln, Oct 20, 2009.

  1. jhn Vln

    jhn Vln Guest

    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
    --
    Posted via http://www.ruby-forum.com/.
    jhn Vln, Oct 20, 2009
    #1
    1. Advertising

  2. jhn Vln

    David Mullet Guest

    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

    http://rubyonwindows.blogspot.com/2007/11/find-replace-with-ms-word.html
    http://rubyonwindows.blogspot.com/search/label/word
    --
    Posted via http://www.ruby-forum.com/.
    David Mullet, Oct 21, 2009
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James Vitale

    word automation find and replace

    James Vitale, Sep 16, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    5,465
    =?Utf-8?B?ZGhucml2ZXJzaWRl?=
    Sep 16, 2005
  2. rynox
    Replies:
    4
    Views:
    29,046
    Malte
    Nov 18, 2005
  3. Masaki Suketa
    Replies:
    4
    Views:
    370
    Dave Burt
    Mar 27, 2006
  4. zxem
    Replies:
    1
    Views:
    223
  5. Mohit Sindhwani
    Replies:
    5
    Views:
    183
    Mohit Sindhwani
    Oct 27, 2008
Loading...

Share This Page