Reputation: 37
Why does this VBA code not work? These documents used a special font called rage, wait, arial. I would like to loop through every letter of the document and whenever the .Font = "*rage*"
I would like to loop through the dozen letters. (My stroke - patience.)
Sub grandma()
Dim doc As Document
Set doc = ActiveDocument
Dim Counter As Integer
For i = 1 To 6
doc.Range.Characters.Count
If doc.Range.Characters(i).Font.Name = "*rage*" Then
doc.Range.Characters(i).Font.Name = "Waiting for the Sunrise"
End If
If doc.Range.Characters(i).Font.Name = "*wait*" Then
doc.Range.Characters(i).Font.Name = "Rage Italic"
End If
If doc.Range.Characters(i).Font.Name = "*arial*" Then
doc.Range.Characters(i).Font.Name = "Arial Nova Cond Light"
End If
j = i
MsgBox "Hi! " & i
Next i
End Sub
Upvotes: 1
Views: 577
Reputation: 10139
Use the Like
operator instead of =
when using wildcards.
Option Explicit
Sub grandma()
Dim doc As Document
Set doc = ActiveDocument
Dim i As Integer
For i = 1 To 6
With doc.Range.Characters(i).Font
Select Case True
Case .Name like "*rage*"
.Name = "Waiting for the Sunrise"
Case .Name Like "*wait*"
.Name = "Rage Italic"
Case .Name Like "*arial*"
.Name = "Arial Nova Cond Light"
End Select
End With
MsgBox "Hi! " & i
Next i
End Sub
Side Note: It is my personal preference to avoid using the data type
Integer
and instead useLong
- unless dealing with legacy applications that would overflow with aLong
or for some reason I don't want to give up those two extra bytes of memory (sarcasm). Especially when dealing with individual word document characters - which a document can contain many many thousands of characters, you can overflow an Integer quickly.Take a look at Why Use Integer Instead of Long? for more information.
Upvotes: 3