Find String and Format

Y

ybazizi

I have a document that is formatted as such:

level1\level2a
level1\level2b '(change level1 text to gray)
level1\level2c '(change level1 text to gray)
level1a\level2a
level1a\level2b '(change level1a text to gray)
level1a\level2c '(change level1a text to gray)
level1a\level2d '(change level1a text to gray)
 
D

Dave Lett

Hi,

I think you are looking for something like the following:

Dim iPar As Integer
Dim oRng1 As Range
Dim oRng2 As Range
Dim iMarker As Integer

For iPar = 1 To ActiveDocument.Paragraphs.Count - 1
''' create a range for each paragraph to compare
Set oRng1 = ActiveDocument.Paragraphs(iPar).Range
Set oRng2 = ActiveDocument.Paragraphs(iPar + 1).Range
iMarker = InStr(1, oRng1.Text, "\") - 1
''' move the end of the range to just before the backwards slash
oRng1.End = oRng1.Start + iMarker
iMarker = InStr(1, oRng2.Text, "\") - 1
oRng2.End = oRng2.Start + iMarker
''' compare the text of the two ranges
If oRng1.Text = oRng2.Text Then
oRng2.Font.Color = wdColorGray15
End If
Next iPar

HTH,
Dave
 
H

Helmut Weber

Hi,

how about this one, just in principle:

Sub Test1004()
Dim rDcm As Word.Range
Dim rTmp As Word.Range
Dim lCnt As Long
Dim sTmp1 As String
Dim sTmp2 As String
Set rDcm = ActiveDocument.Range
Set rTmp = ActiveDocument.Range
With rDcm.Paragraphs
For lCnt = 1 To rDcm.Paragraphs.Count - 1
sTmp1 = .Item(1).Range.Text
sTmp1 = Left(sTmp1, InStr(sTmp1, "\"))
sTmp2 = .Item(2).Range.Text
sTmp2 = Left(sTmp2, InStr(sTmp2, "\"))
If sTmp1 = sTmp2 Then
rTmp.Start = .Item(2).Range.Start
rTmp.End = rTmp.Start + InStr(sTmp2, "\")
rTmp.Font.Color = wdColorBlue
rTmp.Font.Bold = True
End If
rDcm.Start = .Item(2).Range.Start
Next
End With
End Sub

Lots of ways of better coding possible.

HTH, anyway
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top