Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / November 2007

Tip: Looking for answers? Try searching our database.

Bold a list of words in a given document

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Prashanth KR - 28 Nov 2007 16:28 GMT
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.
Jay Freedman - 28 Nov 2007 18:45 GMT
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

Signature

Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

> Hi,
>
[quoted text clipped - 56 lines]
> Thanks,
> Prashanth KR.
fumei - 28 Nov 2007 18:53 GMT
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?

>Hi,
>
[quoted text clipped - 55 lines]
>Thanks,
>Prashanth KR.
Prashanth KR - 29 Nov 2007 07:57 GMT
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.

> Hi,
>
[quoted text clipped - 55 lines]
> Thanks,
> Prashanth KR.

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.