none
When looping through each word in a sentence, having a problem with wrd.InsertAfter RRS feed

  • Question

  • Hello, i'm looping through each word in a sentence, within a paragraph and  when i encounter a GREEN(4) highlighted word, i want to append the highlight number value(4) to the end of the word ie if the word "Budget" is highlighted in GREEN, i would like the word to be changed to "Budget_4". But what appears to be happening is that when vba appends the "_4" to the the end of the "Budget" word in goes into what appears to be an endless loop or endless appending of "_4" to another "_4" and so on, in other words, it appears to be dynamically adjusting its view of the wrd range scope to include the new "_4" over and over again.

    So basically i'm trying to change a GREEN highlighted "Budget" word value to "Budget_4" and the move to the next word. 

    Sub ForEachWordLoop()
       Dim singleLine As Paragraph
       Dim sLines() As String
       Dim wrd As Range

       For Each singleLine In ActiveDocument.Paragraphs
           
            For Each wrd In singleLine.Range.Words
               
                If wrd.HighlightColorIndex > 0 Then
                   wrd.InsertAfter ("_" + CStr(wrd.HighlightColorIndex))
                End If
            Next
     
        Next singleLine

    End Sub

    So might there be a way to tell vba that i'm done with this particular word and move to the "next" word?

    Thanks for any insights!

    bk

    Budget

    Total Advances

    1,6248.43

    Budget

    Total Expenses

    503.22


    • Edited by bobk5444 Friday, April 26, 2013 5:48 PM
    Friday, April 26, 2013 5:35 PM

Answers

  • How about

    Sub FindHighlight()
        Dim lngHighlight As Long
        Application.ScreenUpdating = False
        Selection.HomeKey Unit:=wdStory
        With Selection.Find
            .ClearFormatting
            .Text = ""
            .Highlight = True
            .Forward = True
            .Wrap = wdFindStop
            Do While .Execute
                lngHighlight = Selection.Range.HighlightColorIndex
                Selection.Collapse Direction:=wdCollapseEnd
                Selection.TypeText "_" & lngHighlight
            Loop
        End With
        Application.ScreenUpdating = True
    End Sub

    (Note: this is Word VBA, you may have to modify it slightly for use from VB.Net or similar)

    Regards, Hans Vogelaar

    • Marked as answer by bobk5444 Friday, April 26, 2013 9:45 PM
    • Unmarked as answer by bobk5444 Friday, April 26, 2013 9:45 PM
    • Marked as answer by Damon ZhengModerator Monday, April 29, 2013 6:27 AM
    Friday, April 26, 2013 8:02 PM

All replies

  • How about

    Sub FindHighlight()
        Dim lngHighlight As Long
        Application.ScreenUpdating = False
        Selection.HomeKey Unit:=wdStory
        With Selection.Find
            .ClearFormatting
            .Text = ""
            .Highlight = True
            .Forward = True
            .Wrap = wdFindStop
            Do While .Execute
                lngHighlight = Selection.Range.HighlightColorIndex
                Selection.Collapse Direction:=wdCollapseEnd
                Selection.TypeText "_" & lngHighlight
            Loop
        End With
        Application.ScreenUpdating = True
    End Sub

    (Note: this is Word VBA, you may have to modify it slightly for use from VB.Net or similar)

    Regards, Hans Vogelaar

    • Marked as answer by bobk5444 Friday, April 26, 2013 9:45 PM
    • Unmarked as answer by bobk5444 Friday, April 26, 2013 9:45 PM
    • Marked as answer by Damon ZhengModerator Monday, April 29, 2013 6:27 AM
    Friday, April 26, 2013 8:02 PM
  • Great thanks again very much Hans for you excellent help, works great!

    Have a nice weekend Hans!:)

    Friday, April 26, 2013 9:44 PM