I make a several page word document every week. I copy text from a PDF and paste it into a word document, I then format the text that I pasted.
This takes a long time and i would like to automate it.
I need a macro or some code to select specific text, then make that text bold. The specific text i need to bold is what i call a scrap code.
There are 60 different codes. For example «FIPS», or «LILL».
0m3r
12.2k15 gold badges33 silver badges70 bronze badges
asked Dec 16, 2010 at 2:51
Something like this:
Sub A()
'
' a Macro
'
'
Dim A(3) As String
A(1) = "code1"
A(2) = "code2"
A(3) = "code3"
For i = 1 To 3
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Replacement.Font.Bold = True
.Execute FindText:=A(i), ReplaceWith:=A(i), Format:=True, _
Replace:=wdReplaceAll
End With
Next i
End Sub
HTH!
Edit
To switch dollar amounts to bold
Sub a()
'
' a Macro
'
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.Bold = True
With Selection.Find
.Text = "$([0-9.,]{1,})"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
answered Dec 16, 2010 at 7:26
Dr. belisariusDr. belisarius
60.4k15 gold badges114 silver badges190 bronze badges
3
I would suggest recording a macro.
Then do all the modifications and formatting.
Finally, look at the code of the macro and see how it did it.
The one thing you need to figure out is how you logically want to locate the text you want to bold.
Is it a specific line? Is it at the beginning of a known word?
Once you have that answered, you can combine it with the code of the macro and automate the task.
answered Dec 16, 2010 at 3:02
BeemerGuyBeemerGuy
8,1192 gold badges35 silver badges46 bronze badges
1
This repository has been archived by the owner on Aug 31, 2018. It is now read-only.
Permalink
Cannot retrieve contributors at this time
title | keywords | f1_keywords | ms.prod | api_name | ms.assetid | ms.date |
---|---|---|---|---|---|---|
Font.Bold Property (Word) |
vbawd10.chm156369026 |
vbawd10.chm156369026 |
word |
Word.Font.Bold |
84e8d6b7-1be4-e1c5-c246-a6370011bc8b |
06/08/2017 |
Font.Bold Property (Word)
True if the font is formatted as bold. Read/write Long .
Syntax
expression . Bold
expression A variable that represents a Font object.
Remarks
Returns True , False or wdUndefined (a mixture of True and False ). Can be set to True , False , or wdToggle .
Example
This example makes the entire selection bold if part of the selection is formatted as bold.
If Selection.Type = wdSelectionNormal Then If Selection.Font.Bold = wdUndefined Then _ Selection.Font.Bold = True Else MsgBox "You need to select some text." End If
See also
Concepts
Font Object
Multiple objectsFont
Multiple objects
Contains font attributes (font name, font size, color, and so on) for an object.
Using the Font Object
Use the Font property to return the Font object. The following instruction applies bold formatting to the selection.
Selection.Font.Bold = True
The following example formats the first paragraph in the active document as 24point Arial and italic.
Set myRange = ActiveDocument.Paragraphs(1).Range
With myRange.Font
.Bold = True
.Name = "Arial"
.Size = 24
End With
The following example changes the formatting of the Heading 2 style in the active document to Arial and bold.
With ActiveDocument.Styles(wdStyleHeading2).Font
.Name = "Arial"
.Italic = True
End With
Remarks
You can use the New keyword to create a new, stand-alone Font object. The following example creates a Font object, sets some formatting properties, and then applies the Font object to the first paragraph in the active document.
Set myFont = New Font
myFont.Bold = True
myFont.Name = "Arial"
ActiveDocument.Paragraphs(1).Range.Font = myFont
You can also duplicate a Font object by using the Duplicate
property. The following example creates a new character style with the character formatting from the selection as well as italic formatting. The formatting of the selection isn’t changed.
Set aFont = Selection.Font.Duplicate
aFont.Italic = True
ActiveDocument.Styles.Add(Name:="Italics", _
Type:=wdStyleTypeCharacter).Font = aFont
In this section, I will show you on how to use vba code to change the format font.
In the following code, I discuss about the how to change the font format bold and italic while in th word document do not have the format of bold and Italic but it has the property of font name with bold or italic for instance Font Time New Roman-Bold. Firstly, in order to change the font format with this case, I need to detach the font name of each words or characters within the word document. And then create the function for finding and replacing with font format as we want to replace.
Detecting the font Propertry
To detect the font name properties in word document, I use vba code called: Range object and ActiveDocument.content.Paragraphs to find the fount properties in each word or characters.
ActiveDocument.Content.Paragraphs
And then I compare the text string in each words
InStr(1, CStr(fontList(k)), «ITALIC», vbTextCompare)
InStr(1, CStr(fontList(k)), «BOLD», vbTextCompare)
Replacing font Format
In order to change the font bold or italic, I use the following function which take a paramer called font name and use this font for replacing styles
Public SubChangeFontFormatItalic(fontname As String)
Selection.HomeKey
Selection.WholeStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Font.Name = fontname
Selection.Find.Replacement.Font.Italic = True
With Selection.Find
.Text = «^?»
.Replacement.Text = «»
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
‘Change Font Name with Bold to Font Time New Roamn with Bold format
Public SubChangeFontFormatBold(fontname As String)
Selection.HomeKey
Selection.WholeStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Font.Name = fontname
Selection.Find.Replacement.Font.Bold = True
With Selection.Find
.Text = «^?»
.Replacement.Text = «»
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
Full Code
Here is the full code of this tutorial section
Public Sub ChangeFormatFont
Dim sTmp As String
Dim i, j, k As Integer
Dim opara As Paragraph
Dim fontFont As Boolean
Dim fontCount As Integer
Dim fontList(200) As String
Dim fontname As String
Dim fontItalic As String
Dim fontBold As String
fontCount = 0
For Each opara In ActiveDocument.Content.Paragraphs
If opara.Range.Words.Count > 1 Then
For i = 1 To opara.Range.Words.Count
With opara.Range.Words(i)
fontname = .Font.Name
FoundFont = False
For k = 1 To fontCount
If fontList(k) = fontname Then FoundFont = True
Next k
If Not FoundFont Then
fontCount = fontCount + 1
fontList(fontCount) = fontname
End If
‘
‘ Debug.Print .Font.Name
End With
Next i
End If
Next opara
For k = 1 To fontCount
If InStr(1, CStr(fontList(k)), «ITALIC», vbTextCompare) Then
fontItalic = fontList(k)
ChangeFontFormatItalic (fontItalic)
End If
If InStr(1, CStr(fontList(k)), «BOLD», vbTextCompare) Then
fontBold = fontList(k)
ChangeFontFormatBold (fontBold)
End If
Next k
Debug.Print fontItalic
Debug.Print fontBold
Set opara = Nothing
‘MsgBox «Complete !!!»
End Sub
Public Sub ChangeFontFormatItalic(fontname As String)
Selection.HomeKey
Selection.WholeStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Font.Name = fontname
Selection.Find.Replacement.Font.Italic = True
With Selection.Find
.Text = «^?»
.Replacement.Text = «»
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
‘Change Font Name with Bold to Font Time New Roamn with Bold format
Public Sub ChangeFontFormatBold(fontname As String)
Selection.HomeKey
Selection.WholeStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Font.Name = fontname
Selection.Find.Replacement.Font.Bold = True
With Selection.Find
.Text = «^?»
.Replacement.Text = «»
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
That is all for this tutorial.
Please Note:
This article is written for users of the following Microsoft Word versions: 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365. If you are using an earlier version (Word 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Word, click here: Controlling the Bold Text Attribute.
Written by Allen Wyatt (last updated December 21, 2019)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365
Word allows a rich set of formatting attributes for text in a document. You can control the bold attribute for selected text in a VBA macro by using the Bold property. The syntax is as follows:
Selection.Font.Bold = toggle
where toggle is either False (turns off the bold attribute) or True (turns on the bold attribute). If you simply simply want to change the current setting of the bold attribute—bold text becomes non-bold and vice-versa, then you can use a statement such as the following:
Selection.Font.Bold = Not Selection.Font.Bold
The Font object doesn’t just belong to the Selection object; it can belong to a Range object, as well. This means that you can also modify the Bold property for a range of text as well as a selection.
If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I’ve prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.
WordTips is your source for cost-effective Microsoft Word training.
(Microsoft Word is the most popular word processing software in the world.)
This tip (11894) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365. You can find a version of this tip for the older menu interface of Word here: Controlling the Bold Text Attribute.
Author Bio
With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. Learn more about Allen…
MORE FROM ALLEN
Making Text Bold
Want a cool shortcut to make your text bold? Here’s a method that fits in wonderfully with how things are done in the …
Discover More
Preventing the Left Margin of a Footer from Moving
When you print a document, does the position of the page footer seem to move left and right? This could have to do with …
Discover More
Pulling All Fridays
It can be handy to know when specific weekdays occur within a range of dates. Figuring out this information, using …
Discover More
More WordTips (ribbon)
Passing Parameters to Functions
Functions can be used to perform repetitive tasks and return values to your main program. You can also pass values to a …
Discover More
Counting the Instances of a Text String
Sometimes it is helpful to know how often a particular phrase appears within a document. If you need to know such a …
Discover More
Determining How Many Windows are Open
You can open multiple documents at the same time in Word, and each document occupies its own document window. Here’s a …
Discover More