word macro to hightlight text

H

Helmut Weber

Hi Marco,
I was wondering when you have a lot of words to search on. Can you give
them different colours?

For i = 0 To UBound(arrKeyWords)
If arrKeyWords(i) <> "" Then
ColorWords Trim(arrKeyWords(i)), wdColorBlack, wdYellow
End If
Next i

For 3 alternating colors, like this:

For i = 1 To 3
If i Mod 2 = 0 Then ' even numbers
'ColorWords Trim(arrKeyWords(i)), wdColorBlack, wdYellow
MsgBox "Yellow"
ElseIf i Mod 3 = 0 Then ' numbers that can be divided
' by 3 without remainder
'ColorWords Trim(arrKeyWords(i)), wdColorBlack, wdRed
MsgBox "red"
Else
'ColorWords Trim(arrKeyWords(i)), wdColorBlack, wdBlue
MsgBox "blue"
End If
Next

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
V

vonclausowitz

How about an array:

Sub GetStartedColoring()

Dim strMyDocuments
Dim arrKeyWords As Variant
Dim arrSplit As Variant
Dim i, j As Long

Dim vColours As Variant
vColours = Array(7, 3, 4, 6, 5, 10) 'yellow, turquoise, brightgreen,
red, pink, teal
Dim MyHightlightColour As Long

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
strMyDocuments = objShell.SpecialFolders("MyDocuments")

If Right(strMyDocuments, 1) <> "\" Then
strMyDocuments = strMyDocuments & "\"
End If

If objFSO.FileExists(strMyDocuments & "ColorKeyWords.txt") Then
arrKeyWords = InitFile(strMyDocuments & _
"ColorKeyWords.txt", ";")

For i = 0 To UBound(arrKeyWords)
If arrKeyWords(i) <> "" Then
If j > 5 Then j = 1
MyHightlightColour = vColours(j)
ColorWords Trim(arrKeyWords(i)), wdColorBlack,
MyHightlightColour
j = j + 1
End If
Next i

Selection.HomeKey Unit:=wdStory, Extend:=False
Else
MsgBox "Could not find " & _
strMyDocuments & "ColorKeyWords.txt . Exiting Macro."
End If

End Sub

Greetings
Marco
 
H

Helmut Weber

Hi Marco,
How about an array:

of course you my use an array, there are lots of possible ways.

If your array is zero based, then however,
yellow is used only once, IMHO.

Inititialize your variables, that is,
assign a value to them before the first use.

And don't use variant.
Sooner or later you'll run into trouble.

If you have to setup a program again
like the one we spoke about here,
you may have a go at avoiding wildcards altogether.

Which appraoch would be more suitable,
depends on the task and on the data to be processed.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
V

vonclausowitz

You mean I have to initialize this earlier: ?

MyHightlightColour = vColours(j)

You mean using Dim vColours As Long
vColours = Array(7, 3, 4, 6, 5, 10) instead?

And yes you are right about wildcards but what if I have docs with a
lot of different spellings and still I want to find all these words.
Then I would need a lot of entries.
For example:

kadafi
kaddafi
kadaffi
kaddaffi
khadafi
khaddafi
khadaffi
khaddaffy
etc....
How you wanna do that without wildcards?
 
H

Helmut Weber

Hi Marco,
as far as I remember, "j" wasn't initialized.
You mean using Dim vColours As Long
dim vColours(1 to 6) as long

And yes you are right about wildcards but what if I have docs with a
lot of different spellings and still I want to find all these words.
kadafi
kaddafi
kadaffi
kaddaffi
khadafi
khaddafi
khadaffi
khaddaffy

What do they have in common?
Starting with "k",
"daf" it seems somewhere later.
Ending in "i" or "y".

Pseudy code:
dim oWrd as object
For each oWrd in activedocument.words
if oWrd.range.first = "k" and
oWrd.range.last = i or oWrd.range.last = "y" and
instr(oWrd, "daf") = 1 then ...
next

Lot's of tedious coding, but almost without bugs,
and you have full control. In the end, wildcards
do nothing but just this, it's only that you never
can be sure, what they really do.

You are searching for a string that is similar to another string.
Google for Levenshtein or Levenstein distance.

I have my own algorithm for calculating similarity.
But that would mean 1000 code lines more.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
H

Helmut Weber

Hi Marco,

just to give you an idea of what you are trying,
as it is so exiting for me.
There is a set of strings:
kadafi
kaddafi
kadaffi
kaddaffi
khadafi
khaddafi
khadaffi
khaddaffy
etc....

Excluding the etc., as it is complicated enough
with a well definded set.

What can you say about all(!) items in the set?
All start with k.
All contain k, a, d, and f.
All contain daf.
Never f before a.
Never d before a.
Never f before d.

Are these all simple statements that can be made on all of the items?
Whereby simply means "no counting", "no comparison", no "or", no "if".

What can you say simply about some (more than 1) items in the set?
Whereby simply means "no counting", "no comparison", no "or", no "if".
Some contain kh.
Some contain ffi.
Some contain add...

Ad nauseam...

The end of all wildcard searches,
but opening the doors to neurolinguistics...

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
K

Klaus Linke

Helmut Weber said:
Hi Marco

try:

[a-z]@uze[a-z]{1;}

That seems to be one of the reasons,
why some people mistrust wildcard searches.


Well, they distrust them because they are tricky beasts.

Take Doug's expression
[a-z]{1;}tt[a-z]{1;}

{} tries to match as much as it can. So
[a-z]{1;}
would already match the whole of
attraction
attention
inattention

"tt" in the search expression, and the rest of the expression, never match
anything, so the match fails.

The following would have matched the three words:
[a-su-z]{1;}tt[a-z]{1;}

Regards,
Klaus
 

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