Finding multiple occurrences of character in word document

T

TazCoder

I have been writing a large macro and the final step is to find
multiple occurrences' of a specific character in one sentence. The
algorithm must:
Search every sentence in the word document, find any sentence that has
more then two (commas, or any other specific character), and highlight
the two of them.

I know the basic idea behind the algorithm and I know how to implement
it in C/C++/Java but not in VBA. I need to load each sentence
(searching for the period) into a string. In this string look for a
comma, remember the index and increment the counter by one, continue to
search for another comma. If another one is not found I am done, if
another one is found, mark that position, and highlight the two
positions.

The problem I have is with the actual coding. How can I go about doing
this?
How can I load each specific sentence into a string? (note the
sentences only end in periods, which makes is slightly less
complicated.)
How can I find the position of each comma within that string?

Once I have those two problems solved, the rest is easy.

Thanks.
 
G

Greg

TazCoder,

Ugly and with limited testing, but this might work:

Sub Test()
Dim i As Long, j As Long
Dim oRng As Word.Range
Dim oTmpRng1 As Word.Range
Dim oTmpRng() As Word.Range
For i = 1 To ActiveDocument.Sentences.Count
j = 0
Set oRng = ActiveDocument.Sentences(i)
Set oTmpRng1 = oRng.Duplicate
oRng.Font.Color = wdColorBrightGreen
With oRng.Find
.Text = ","
.Font.Color = wdColorBrightGreen
While .Execute
j = j + 1
ReDim Preserve oTmpRng(j)
Set oTmpRng(j) = oRng.Duplicate
Wend
End With
If j > 1 Then
For j = 1 To UBound(oTmpRng)
oTmpRng(j).HighlightColorIndex = wdYellow
Next j
End If
oTmpRng1.Font.Color = wdColorAutomatic
Set oRng = Nothing
Next i
End Sub
 
K

Klaus Linke

Hi TazCoder,

A simple and fast way would likely be a wildcard search for (,){2}

The macro recorder should give you the code.

If you want to also find triple, quadruple... occurrences, the wildcard
search for that would be (,){2,}
(if the field separator in your Windows installation is a comma).

Greetings,
Klaus
 
G

Greg Maxey

Klaus,

I might have it wrong, but I think he is looking for two commas occurring
anywhere in the sentence and not just adjacent to each other.
 
K

Klaus Linke

Oh yes. Sorry, didn't read carefully.

You could also loop the sentences (like you did, or using "For Each"), read
the text into a string, and do a Split with the comma as a delimiter.
The upper bound would give you the number of commas.

Dim rngSentence As Range
For Each rngSentence In ActiveDocument.Sentences
Debug.Print UBound(Split(rngSentence.Text, ","))
Next rngSentence


Klaus
 
G

Greg Maxey

TazCoder,

Another option:

Sub MarkDups()
Dim oTmpRng() As Word.Range
Dim oPara As Paragraph
Dim i As Integer
Dim FindStr As String
FindStr = LCase(InputBox("Type character to check"))
Dim z As Long
Dim oChar As Word.Range
For z = 1 To ActiveDocument.Sentences.Count
i = 0
For Each oChar In ActiveDocument.Sentences(z).Characters
If FindStr = LCase(oChar) Then
i = i + 1
ReDim Preserve oTmpRng(i)
Set oTmpRng(i) = oChar
End If
Next oChar
If i > 1 Then
For i = 1 To UBound(oTmpRng)
oTmpRng(i).HighlightColorIndex = wdYellow
Next
End If
Next z
End Sub
 
G

Greg Maxey

TazCoder,

After getting the range sorted out with help from Klaus, you might try:

Sub Test2()
'Best way so far.
Dim i As Long, j As Long, k As Long
Dim oRng As Word.Range
Dim oRngDup As Word.Range
Dim oTmpRng() As Word.Range
For i = 1 To ActiveDocument.Sentences.Count
j = 0
Set oRng = ActiveDocument.Sentences(i)
Set oRngDup = oRng.Duplicate
With oRng.Find
.Text = ","
While .Execute
j = j + 1
ReDim Preserve oTmpRng(j)
Set oTmpRng(j) = oRng.Duplicate
k = oRng.End
oRng.Start = k + 1
oRng.End = oRngDup.End
Wend
End With
If j > 1 Then
For j = 1 To UBound(oTmpRng)
oTmpRng(j).HighlightColorIndex = wdYellow
Next j
End If
Set oRng = Nothing
Next i
End Sub
 
T

TazCoder

This is perfect, thanks a lot for the help Thanks again!
(Google groups rules!)
TazCoder
 
T

TazCoder

This is perfect, thanks a lot for the help Thanks again!
(Google groups rules!)
TazCoder
 
T

Tony Jollans

You can still use a wildcard search

For Each S In ActiveDocument.Sentences
With S.Find
.Text = ",*,"
.MatchWildcards = True
If .Execute Then
ActiveDocument.Range(S.Start, S.Start +
1).HighlightColorIndex = 5
ActiveDocument.Range(S.End - 1, S.End).HighlightColorIndex =
5
End If
End With
Next

I'm not quite sure what sort of 'highlighting' is wanted - the above takes
it literally
 

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