How to replace whole sentence in Word VBA RRS feed

  • Question

  • Hi i am try to read each sentence of the document with this code:

    For Each sentence In ActiveDocument.StoryRanges 

    i try to make replace the sentence to another text, i try find and replace but it will get an error 5854 String parameter to long

    Any one know how to change the sentence I get from this code above to another text 

    Saturday, July 22, 2017 5:07 PM

All replies

  • Can't you use something like this?

        With ActiveDocument.Content.Find
            .Text = "This is the sentence to look for."
            .Replacement.Text = "This is the replacement sentence."
            .Execute Replace:=wdReplaceAll
        End With

    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Saturday, July 22, 2017 7:00 PM
  • It will get an error because excess 250 characters :(
    Sunday, July 23, 2017 12:55 AM
  • What EXACTLY is it that you are doing. Even if you declare the variable Sentence that code sequence is going to produce some odd results. You might be better with e.g. as follows, and you should also be aware that Word's idea of what constitutes a sentence and your own may not coincide.

    Dim oSentence As Variant
        For Each oSentence In ActiveDocument.Range.Sentences
            MsgBox oSentence
        Next oSentence

    Graham Mayor - Word MVP

    Sunday, July 23, 2017 4:23 AM