PDA

View Full Version : [SOLVED:] Question on highlighting word before & after "and"



Programmer_n
07-31-2016, 08:17 PM
The need to highlight (Yellow color) the word that occurs before & after and. The keyword here is and.

Like Profit and loss, income and expense,banking and finance.

Ideally, there would be single line space between the word that occur before & after and.

It is good, if exceptions like Banking, finance and investment are also accounted for, with interruption by comma.

gmayor
07-31-2016, 10:02 PM
The following should work - but what you consider a 'word' and what Word considers a 'word' may not always coincide

Dim oRng As Range
Set oRng = ActiveDocument.Range
With oRng.Find
Do While .Execute(FindText:="and", MatchWholeWord:=True)
oRng.Start = oRng.Previous.Words(1).Start
oRng.End = oRng.Next.Words(1).Next.Words(1).End
oRng.MoveEndWhile Chr(32), wdBackward
oRng.HighlightColorIndex = wdYellow
oRng.Collapse 0
Loop
End With

Programmer_n
07-31-2016, 11:18 PM
Works like charm.

Thanks for the code.

Programmer_n
07-31-2016, 11:33 PM
The following should work - but what you consider a 'word' and what Word considers a 'word' may not always coincide

Dim oRng As Range
Set oRng = ActiveDocument.Range
With oRng.Find
Do While .Execute(FindText:="and", MatchWholeWord:=True)
oRng.Start = oRng.Previous.Words(1).Start
oRng.End = oRng.Next.Words(1).Next.Words(1).End
oRng.MoveEndWhile Chr(32), wdBackward
oRng.HighlightColorIndex = wdYellow
oRng.Collapse 0
Loop
End With

The code works, As a follow up question...
I am studying introductory phrases of a paragraph.
I would like to ask how to highlight(yellow), the first few words of a paragraph(phrase) till comma?

gmayor
08-01-2016, 04:47 AM
You would need something like the following where here oRng is the paragraph containing the cursor.

Dim oRng As Range
Set oRng = Selection.Paragraphs(1).Range
If InStr(1, oRng.Text, ",") > 0 Then
oRng.Collapse 1
oRng.MoveEndUntil ","
oRng.HighlightColorIndex = wdYellow
Else
MsgBox "No comma in the paragraph"
End If

Programmer_n
08-01-2016, 06:43 AM
You would need something like the following where here oRng is the paragraph containing the cursor.

Dim oRng As Range
Set oRng = Selection.Paragraphs(1).Range
If InStr(1, oRng.Text, ",") > 0 Then
oRng.Collapse 1
oRng.MoveEndUntil ","
oRng.HighlightColorIndex = wdYellow
Else
MsgBox "No comma in the paragraph"
End If

Thanks, you exactly replied to my question. But my bad, i didn't ask my question properly at first place. I want to check and highlight many paragraphs across the document, just as you did a while loop, is it possible to do a while loop for this? I tried it myself but unsure of do statement. Please help. Mine didn't work.

Sub text()
Dim oRng As Range
Set oRng = ActiveDocument.Range
With oRng.Find
Do While .Execute(InStr(1, oRng.text, ",") > 0)
oRng.Collapse 1
oRng.MoveEndUntil ","
oRng.HighlightColorIndex = wdYellow
Loop
End With
End Sub

gmaxey
08-01-2016, 07:42 AM
I think Graham has retired for the evening where he lives.


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oPar As Paragraph
Dim oRng As Range
Set oPar = ActiveDocument.Paragraphs(1)
Do
If InStr(1, oPar.Range.Text, ",") > 0 Then
Set oRng = oPar.Range
With oRng
.Collapse 1
.MoveEndUntil ","
.HighlightColorIndex = wdYellow
End With
End If
Set oPar = oPar.Next
Loop Until oPar Is Nothing
lbl_Exit:
Exit Sub
End Sub

gmaxey
08-01-2016, 08:56 AM
Perhaps he had a lousy speech writer, but John F. Kennedy (among others) once used the conjunction "And" to start a sentence.
"Let all our neighbors know that we shall join with them to oppose aggression or subversion anywhere in the Americas. And let every other power know that this hemisphere intends to remain the master of its own house."~ John F. Kennedy.

Heady times indeed.

This version should handle those situations and provide for a series up to three:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = " and <*>"
.MatchCase = False
.MatchWildcards = True
While .Execute
Do
If Not oRng.Characters.First = ActiveDocument.Characters.First Then
Select Case Asc(oRng.Characters.First.Previous)
Case 9, 11, 13, 32, 160: Exit Do
Case Else: oRng.MoveStart wdCharacter, -1
End Select
Else
Exit Do
End If
Loop
If Not oRng.Start < 3 Then
If oRng.Characters.First.Previous = " " And oRng.Characters.First.Previous.Previous = "," Then
oRng.MoveStart wdCharacter, -2
Do
If Not oRng.Characters.First = ActiveDocument.Characters.First Then
Select Case Asc(oRng.Characters.First.Previous)
Case 9, 11, 13, 32, 160: Exit Do
Case Else: oRng.MoveStart wdCharacter, -1
End Select
Else
Exit Do
End If
Loop
End If
End If
oRng.HighlightColorIndex = wdYellow
oRng.Collapse 0
Wend
End With
End Sub

Programmer_n
08-01-2016, 05:20 PM
Perhaps he had a lousy speech writer, but John F. Kennedy (among others) once used the conjunction "And" to start a sentence.
"Letall our neighbors know that we shall join with them to oppose aggression orsubversion anywhere in the Americas. Andlet every other power know that this hemisphere intends to remain the master of its own house."~ John F. Kennedy.

Heady times indeed.

This version should handle those situations and provide for a series up to three:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = " and <*>"
.MatchCase = False
.MatchWildcards = True
While .Execute
Do
If Not oRng.Characters.First = ActiveDocument.Characters.First Then
Select Case Asc(oRng.Characters.First.Previous)
Case 9, 11, 13, 32, 160: Exit Do
Case Else: oRng.MoveStart wdCharacter, -1
End Select
Else
Exit Do
End If
Loop
If Not oRng.Start < 3 Then
If oRng.Characters.First.Previous = " " And oRng.Characters.First.Previous.Previous = "," Then
oRng.MoveStart wdCharacter, -2
Do
If Not oRng.Characters.First = ActiveDocument.Characters.First Then
Select Case Asc(oRng.Characters.First.Previous)
Case 9, 11, 13, 32, 160: Exit Do
Case Else: oRng.MoveStart wdCharacter, -1
End Select
Else
Exit Do
End If
Loop
End If
End If
oRng.HighlightColorIndex = wdYellow
oRng.Collapse 0
Wend
End With
End Sub





Thanks. Exactly, solves the problem.