/ Forums / Advansys Formativ / Creating Solutions with Formativ / Search and Replace in MS Word

  • Creator
    Topic
  • #3982
    Anonymous

    Hi!
    could someone give me a simple example, how to search & replace in a word dokument?

    i can open the right word document, but i get an error, when i want to replace..

    [snippet]
    function OpenWord()

    OpenWord = TRUE
    On error resume next
    Set iWord = GetObject(“Word.Application”)
    if IsEmpty(iWord) then
    Set iWord = CreateObject(“Word.Application”)
    if IsEmpty(iWord) then
    Call MsgBox(“text”, vbCritical, CAPTION)
    OpenWord = FALSE
    end If
    end if
    iWord.visible = true
    iword.Documents.open(DOCPATH)
    s = iword.ActiveDocument
    iword.documents.item(1).Content.Find.Execute FindText=”%1″, ReplaceWith=”Argl” ,Replace=WdReplace.wdReplaceAll
    iword.documents.item(1).save
    iword.documents.item(1).close

    end function
    [/snippet]

    the error i get is following:

    Wrong number of arguments, or invalid Property(association)

    (as usual its a translation ;)

    Thx in Advance
    Greez Tom

  • Author
    Replies
  • #6774
    Support 1
    Participant

    I believe that using the selection object (.ActiveDocument) in Word is unreliable. Also, neither VBScript nor Formativ know the Word constant wdReplaceAll.

    The following code sample should be a useful alternative.

    Advansys Support

    Const wdReplaceAll = 2
     
    '-------------------------------------------------------------------------------
    ' Mainline processing
    '-------------------------------------------------------------------------------
    Sub Main(Client, GWEvent)
     
      dim oRange
      dim oWordObj
     
      On error resume next
     
      Set oWordObj = CreateObject("Word.Application")
     
      if oWordObj is nothing then
        exit sub
      end if
     
      oWordObj.Documents.open("c:temptest.doc")
      oWordObj.visible = true
     
      Set oRange = oWordObj.Documents(1).Content
     
      if oRange is nothing then
        exit sub
      end if
     
      Call oRange.Find.Execute("%1",FALSE,TRUE, FALSE,,,TRUE,FALSE,FALSE,"Argl",wdReplaceAll)
     
      set oRange = nothing
      set oWordObj = nothing
     
    End Sub
    
    #6772
    Anonymous

    That code works fine for me! Thanks a lot!

    P.S.: As usual workin late at night 😉

    #6773
    Support 1
    Participant

    Glad to hear you’ve been successful. We are happy to help.

    Advansys Support

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.