2 Replies Latest reply on Aug 25, 2011 1:50 AM by FrankB62

    How do I change text within a paragraph in VB-script?

    FrankB62 Level 1

      I am having some problems with changing the text inside a paragraph without affecting the styling of other paragraphs. It may be that I am using a wrong approach to changing existing text, I don't really know.


      As an example, I have the following two paragraphs inside a table cell:





      These two paragraphs have different paragraph styles applied to them.


      If I now change the contents of the first paragraph to a 1 digit number, the style of the second paragraph changes to the same style as the first one.


      Paragraph1.Contents = "4" & Chr(13)


      The text is correct after the change, but the styling on the second paragraph is wrong:





      I have also tried using Paragraph.Lines(1).Contents and Paragraph.Texts(1).Contents, but the result is the same.


      Is there any way of changing text inside a paragraph, which ensures that noting other than that text is affected?

        • 1. Re: How do I change text within a paragraph in VB-script?
          Ken Krugh Level 1

          Hi Frank,


          The problem is occurring because you're effectively removing the paragraph end by replacing the entire contents of the paragreaph. You want to use the Characters collection and the ItemByRange method so that you can leave the paragraph end untouched.


          I'm pretty sure it would look something like this though I haven't tested this:


          Set SChar = Paragraph1.Characters(1)

          Set EChar = Paragraph1.Characters(-2)     'giving you the 2nd to last character.

          Set TxtToDel = Paragraph1.Characters.ItemByRange(SChar, EChar).



          Then you can set Paragraph1.InsertionPoints(1).Contents to the new text.


          If the ItemByRange doesn't work you can always loop through the character collectoin and delete all but the last characters in the paragraph.


          Good luck!

          1 person found this helpful
          • 2. Re: How do I change text within a paragraph in VB-script?
            FrankB62 Level 1

            Thanks alot Ken.


            It doesn't quite work if I use your example as it is, because I can have paragraphs without any text in them, and also without a Chr(13) at the end (last paragraph within a table cell), but you surely guided me onto the right track.


            I ended up with the following code, which seems to work for all cases, including empty paragraphs, and paragraphs without the Chr(13):


            If Right(Paragraph.Contents, 1) = Chr(13) Then
                 Do Until Paragraph.Characters(1).Contents = Chr(13)
                 Paragraph.InsertionPoints(1).Contents = NewContents
                 Paragraph.Contents = New
            End If


            It may not be the most efficient way of doing it (deleting one character at a time), but it works.