In this article, I will discuss on how to detect the font size and change the font size to the size as we want to change with two examples of code. In the first example, it is about changing the font size within the defined range of font size and in the second one; it is about changing all font size to one defined size.
To work with the font size with vba, we need to use Word.Range to detect the font size and change the font size. Here are the two example of codes
Example 1: Changing font size withthe defined font size
SubStandardizeFontSize (oWord As Word.Range)
IfoWord.Font.Size < 16 And oWord.Font.Size > 12 Then
oWord.Font.Size = 16
ElseIfoWord.Font.Size < 12 And oWord.Font.Size > 11 Then
oWord.Font.Size = 12
ElseIfoWord.Font.Size <= 11 And oWord.Font.Size > 9 Then
oWord.Font.Size = 11
ElseIfoWord.Font.Size <= 9 Then
oWord.Font.Size = 10
ElseIfoWord.Font.Size = 9999999 Then'999999 - represents word with characters of different size
sFontSize = oWord.Characters(1).Font.Size
Forj = 1 To oWord.Characters.Count
IfoWord.Characters(j).Font.Size = sFontSize Then: oWord.Characters(j).Font.Size = 12
Else: oWord.Characters(j).Font.Size = 10
End If
Next
End If
EndSub
Example 2: Change font size to a 12 pt
PublicSub ChangeFontSize()
DimoParagraph As Paragraph
DimoRange As Range
Dimi, j As Integer
DimsFontSize As Single
ForEach oParagraph In Selection.Paragraphs
Set oRange = oParagraph.Range
Fori = 1 To oRange.Words.Count
IfoRange.Words(i).Font.Size = 9999999 Then'999999 - represents word with characters of different size
sFontSize = oRange.Words(i).Characters(1).Font.Size
Forj = 1 To oRange.Words(i).Characters.Count
IfoRange.Words(i).Characters(j).Font.Size = sFontSize Then
oRange.Words(i).Characters(j).Font.Size = 12
Else
oRange.Words(i).Characters(j).Font.Size = 12
End If
Next
End If
Next
Next
EndSub
How to test this Code
1. Open Ms Word
2. Open VB Editor or Alt+F11
3. Create new module
4. Copy and paste the Code
5. Go back to you word and go to Macro Dialog then select the Macro Name StandardizeFontSize and Click on Run
0 comments :
Post a Comment