Bold a list of words in a given document

P

Prashanth KR

Hi,

I have a list of words in a doc say List.doc where I have listed a number of
words one below the other like:

Microsoft Word
SAP
E-business Suite
Salesforce.com

Iam currently using the below mentioned macro:

Sub Mymacro()

Dim sCheckDoc As String
Dim docRef As Document
Dim docCurrent As Document
Dim wrdRef As Object

sCheckDoc = "D:\List.doc"
Set docCurrent = Selection.Document
Set docRef = Documents.Open(sCheckDoc)
docCurrent.Activate

With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Font.Bold = True
.Replacement.Text = "^&"
.Forward = True
.Format = True
.MatchWholeWord = True
.MatchCase = False
.MatchWildcards = False
End With

For Each wrdRef In docRef.Words
If Asc(Left(wrdRef, 1)) > 32 Then
With Selection.Find
.Wrap = wdFindContinue
.Text = wrdRef
.Execute Replace:=wdReplaceAll
End With
End If
Next wrdRef

docRef.Close
docCurrent.Activate
End Sub

The problem here is that when I run this macro in another document having
these terms it does runs but BOLDS the letters "Microsoft", "Word", "E",
"Business", "Suite", "Salesforce" ".com" separately along with the full
letters i.e., Microsoft Word, E-business Suite, Salesforce.com.

I want only the full letters to be bolded. Kindly help me out in this.

Thanks,
Prashanth KR.
 
J

Jay Freedman

Your problem comes from using "For Each wrdRef In docRef.Words" in your
loop. In Word's object model, each series of characters delimited by a
space, punctuation, or a paragraph mark is a separate word in the
docRef.Words collection. That's why you're getting the results you see. For
this example, "E" is a word, so your macro seeks and bolds every occurrence
of "E".

I'll assume that in List.doc every line ends with a paragraph mark, so each
term you want to search for is a separate paragraph. Then your macro should
be changed as follows:

Dim wrdRef As String
Dim wrdPara As Paragraph
....
For Each wrdPara In docRef.Paragraphs
wrdRef = wrdPara.Range.Text
' remove the paragraph mark:
wrdRef = Left(wrdRef, Len(wrdRef) - 1)
If Asc(Left(wrdRef, 1)) > 32 Then
With Selection.Find
.Wrap = wdFindContinue
.Text = wrdRef
.Execute Replace:=wdReplaceAll
End With
End If
Next wrdPara

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
F

fumei via OfficeKB.com

Not quite following this. What do you mean by "full letters"?

"separately along with the full letters"

When using Words in Word, you have to be careful. Take for example:

Salesforce.com

Selecting that WITHOUT an ending paragraph mark would give a word count = 3.
Three words: Salesforce, ., com

If you include the paragraph mark, the word count = 4. Four "words":
Salesforce, ., com, paragraph mark.

So, again, I am not quite following what "full letters" means.

Of "Microsoft Word", what, exactly do you want bolded?
 
P

Prashanth KR

Hey Jay,

It was wonderful to see your reply and it worked like a miracle. Thank you
very much for your timely help.

Regards,
Prashanth KR.
 

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