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.
Greg - 03 Apr 2006 21:21 GMT
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
TazCoder - 04 Apr 2006 04:40 GMT
This is perfect, thanks a lot for the help Thanks again!
(Google groups rules!)
TazCoder
TazCoder - 04 Apr 2006 04:40 GMT
This is perfect, thanks a lot for the help Thanks again!
(Google groups rules!)
TazCoder
Klaus Linke - 03 Apr 2006 21:46 GMT
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
>I have been writing a large macro and the final step is to find
> multiple occurrences' of a specific character in one sentence. The
[quoted text clipped - 21 lines]
>
> Thanks.
Greg Maxey - 03 Apr 2006 23:15 GMT
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.

Signature
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
> Hi TazCoder,
>
[quoted text clipped - 34 lines]
>>
>> Thanks.
Klaus Linke - 03 Apr 2006 23:35 GMT
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
> Klaus,
>
[quoted text clipped - 39 lines]
>>>
>>> Thanks.
Tony Jollans - 04 Apr 2006 09:57 GMT
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
--
Enjoy,
Tony
> Klaus,
>
[quoted text clipped - 45 lines]
> >>
> >> Thanks.
Greg Maxey - 03 Apr 2006 23:38 GMT
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

Signature
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
> I have been writing a large macro and the final step is to find
> multiple occurrences' of a specific character in one sentence. The
[quoted text clipped - 21 lines]
>
> Thanks.
Greg Maxey - 04 Apr 2006 00:19 GMT
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

Signature
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
> I have been writing a large macro and the final step is to find
> multiple occurrences' of a specific character in one sentence. The
[quoted text clipped - 21 lines]
>
> Thanks.