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)
.
.
.
I want to format the level1 text (set it to a light color gray) on each line
if the level1 text on the line above it is the same. The first instance of
the level1 text would remain colored black - all subsequent instance would be
gray. I'm new to VBA, and here is my first stab. I'm just not sure where to
go from here.
Sub FormatStrings()
Dim oPar As Paragraph
Dim sString1 As String
Dim sString2 As String
sString1 = ""
For Each oPar In ActiveDocument.Paragraphs
sString2 = oPar.Range.Sentences(1)
sString2 = Left(sString2, InStr(0, sString2, "\"))
If sString2 = sString1 Then
'Set the format of only the text before the \
Else
'Do nothing, go to next line
End If
Next oPar
End Sub
Thanks
Dave Lett - 31 Jan 2006 13:27 GMT
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
> I have a document that is formatted as such:
>
[quoted text clipped - 35 lines]
>
> Thanks
ybazizi - 31 Jan 2006 16:21 GMT
Thanks Dave - that was exactly what I was looking for!
> Hi,
>
[quoted text clipped - 62 lines]
> >
> > Thanks
Helmut Weber - 31 Jan 2006 13:30 GMT
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
--
Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000