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

  • Creator
  • #3982

      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..

      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
      s = iword.ActiveDocument
      iword.documents.item(1).Content.Find.Execute FindText=”%1″, ReplaceWith=”Argl” ,Replace=WdReplace.wdReplaceAll

      end function

      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
    • #6774
      Support 1

        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.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

          That code works fine for me! Thanks a lot!

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

          Support 1

            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.