Tadej Gr
Tadej Gr

Reputation: 45

Remove highlighting in a specified range

I have highlighted paragraphs in a Word document, from which I have to remove highlighting from 3rd to 5th character of each paragraph.

By searching for highlighted ranges within Set r = ActiveDocument.Range in VBA the segments of text are perfectly found.

The error appears on the line r(Start:=r_start, End:=r_end).HighlightColorIndex = wdNoHighlight.

Compile error: Wrong number of arguments or invalid property assignment.

How danI correctly specify the subrange from 3rd to 5th character within the range r? Your help is appreciated.

Dim r as Range
Dim r_start As Integer
Dim r_end As Integer
r_start = 2
r_end = 5
Set r = ActiveDocument.Range
With r.Find
  .Highlight = True
  Do While .Execute(FindText:="", Forward:=True) = True
    if r.Characters.Count > 7 Then
       r(Start:=r_start, End:=r_end).HighlightColorIndex = wdNoHighlight
    End If
    r.Collapse 0
  Loop
End With

Upvotes: 0

Views: 760

Answers (2)

Cindy Meister
Cindy Meister

Reputation: 25663

The problem causing the error message is that only the Range method (as in Document.Range) takes arguments. The Range object, since it's not a method, can take no arguments. In order to set the Start and End of a Range object you need the properties of those names. So:

   r.Start = r.Start + r_start
   r.End = r.Start + r_end

Your code has a number of other problems which I encountered while testing. For example, if you set the Start position to r_start and the End position to r_end the Range r will be from the second to the fifth characters of the entire document, not the second to fifth characters of the Range r. That's why the two lines of code, above, have been changed from your original.

The next problem is that the code, as it stands, goes into an infinite loop since the search always begins from within the "found" highlighting. For that reason I've added a variable to capture the end point of the originally Found range and use that as the starting point for the Range to be searched in each loop. The end of the Range to search is set to the end of the document.

Here's my sample code:

Sub FindRemoveHighlighting()
    Dim r As Range, rDoc As Range
    Dim r_foundEnd As Long
    Dim r_start As Long
    Dim r_end As Long
    r_start = 2
    r_end = 5
    Set rDoc = ActiveDocument.content
    Set r = rDoc.Duplicate
    With r.Find
      .Highlight = True
      .Text = ""
      .Format = True
      .Format = True
      Do While .Execute() = True
        If r.Characters.Count > 7 Then
           rFoundEnd = r.End
           r.Start = r.Start + r_start
           r.End = r.Start + r_end
           r.HighlightColorIndex = wdNoHighlight
        End If
        r.Start = rFoundEnd
        r.End = rDoc.End
      Loop
    End With
End Sub

Upvotes: 1

Ibo
Ibo

Reputation: 4309

You were almost there, I modified the code and tested it and it worked perfectly on my end. It will find any highlighted range in the document and will remove the highlights from character 2 to character 5:

Sub GetHighlights()
    Dim rng1 As Range
    Dim rng2 As Range
    Dim r_start As Integer
    Dim r_end As Integer
    r_start = 2
    r_end = 5

    Set rng1 = ActiveDocument.Range
    With rng1.Find
      .Highlight = True
      Do While .Execute(FindText:="", Forward:=True) = True
        If rng1.Characters.Count > 7 Then
            Set rng2 = ActiveDocument.Range(Start:=rng1.Start + r_start, End:=rng1.Start + r_end)
            rng2.HighlightColorIndex = wdNoHighlight
        End If
        rng1.Collapse 0
      Loop
    End With
End Sub

Upvotes: 0

Related Questions