Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word Macro to Locate Fixed text and then "bold" it

Status
Not open for further replies.

jsteff

Technical User
May 22, 2003
109
0
0
US
I have a single doc that represents many invoices. I need to create a macro that will locate three different strings globally, like "Grand Total", "For Services Rendered" and "Due Date".

When the macro finds this text, I want it to make the text bold.

I have done "keystroke" macros before with the wizard but I don't have the experience to write the correct expressions.

Any ideas?
 




Hi,

"I have done "keystroke" macros before with the wizard but I don't have the experience to write the correct expressions."

Macro record the FIND then BOLD.

Post back if you need help.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Start Macro Recorder.
Open the Replace Dialog Ctrl+H).
In the Find box enter the text string.
In the Replace Box enter ^& and format it by pressing MORE and selecting Format, Font and clicking on Bold.
Click on ReplaceAll, OK.
Stop Macro Recorder.


Regards: tf1
 
Unfortunately, unless you are using Word 2007, the macro recorder will not record what you want.

For each change, it will record something like ..
Code:
[blue]    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "Grand Total"
        [green]' lots more stuff here[/green]
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
[/blue]

.. and you will need to add an extra line
Code:
[blue]    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    [red]Selection.Find.Replacement.Font.Bold = True[/red]
    With Selection.Find
        .Text = "Grand Total"
        [green]' lots more stuff here[/green]
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
[/blue]

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
My friends,
All of your suggestions made it all work perfectly!

Thank you.

One more issue: In the macro, once I "Find and Replace", how do I get the "bold" to run all the way to the END of the line???
 



Tony,

Recorded in Word 2007
Code:
Sub Macro1()
'
' Macro1 Macro
'
'
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = "good"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute[b]
    Selection.Font.Bold = wdToggle[/b]
End Sub
???

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 

My doc has many occurances of:

Total Due: $ 12,999.22

I never know how many digits are in the value. So the following macro will bold all occurances of "Total Due: " but will not go to the end of the line with "bold".


Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.Bold = True
With Selection.Find
.Text = "Total Due:"
.Replacement.Text = "^&"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Font.Bold = wdToggle
 
Skip,

I did say ... unless you are using 2007 ... but I see that you are doing a Find, then bolding the result rather than a replace with bold, so the (pre-2007) recorder error doesn't affect that.

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
Hi jsteff.

"I need to create a macro that will locate three different strings globally, like "Grand Total", "For Services Rendered" and "Due Date".

When the macro finds this text, I want it to make the text bold."

1. Please try and be more detailed and explicit. The fact is (apparently), you do NOT want to find the text and make the text bold. You want to find the text, and make everything after the text (to the end of the paragraph I assume) bold. This is not the same as just finding specific text and making only it bold. According to what you originally asked about, it would be:

Total Due: $ 12,999.22

Although you did not mention Total Due - "I need to create a macro that will locate three different strings globally, like "Grand Total", "For Services Rendered" and "Due Date".

But you actually really want:

Total Due: $ 12,999.22

This is quite a different thing, although it should not be all that difficult.

2. As this is a VBA issue, again, further questions should be posted to the VBA forum.

IF your line (for example) Total Due: $ 12,999.22 is terminated by a paragraph mark, then it is fairly easy to do what you want.
Code:
Sub MakeItBold()
Dim r As Range
Dim var
Dim mySearch()
mySearch = Array("Due Date:", "Grand Total:", _
   "For Services Rendered:", _
   "Total Due:")

For var = 0 To UBound(mySearch)
   Set r = ActiveDocument.Range
   With r.Find
      Do While .Execute(FindText:=mySearch(var), _
            Forward:=True) = True
         With r
            .MoveEndUntil Cset:=vbCrLf
            .Font.Bold = True
            .Collapse 0
         End With
      Loop
   End With
Next var
End Sub

What this does:

1. makes an array of your search phrases - mySearch
2. for each item of the array, it makes a Range object of the document
3. searches for the string
4. if found, makes the current range that found text extended to just before a paragraph mark
5. bolds the range
6. collapses the range to the end
7. looks for the next instance of the search string
8. repeats 3 - 7 for the next string in the array.


faq219-2884

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top