1

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

enter image description here

braX
  • 11,506
  • 5
  • 20
  • 33
Keri
  • 37
  • 6
  • 1
    You declared `Counter` as type integer but used the undeclared variable `i` instead? Not the issue, but just more reason you should use `Option Explicit` on the top of **every module**. – K.Dᴀᴠɪs Oct 24 '18 at 19:54
  • 1
    `If foo = "abc*"` will be true if `foo` is **equal to** the string literal `"abc*"`. Equality/comparison operator `=` doesn't do wildcards. – Mathieu Guindon Oct 24 '18 at 20:05
  • Do these font names actually contain asterisks - `*`? Note that it would probably be faster and more efficient to use Word's built-in Find/Replace functionality. – Cindy Meister Oct 25 '18 at 14:06

1 Answers1

3

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 use Long - unless dealing with legacy applications that would overflow with a Long 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.

K.Dᴀᴠɪs
  • 9,945
  • 11
  • 33
  • 43
  • What do I add/adjust here in order to loop the font change through the entire document without a pop-up prompt for each change? – Keri Oct 26 '18 at 16:06
  • If you are wanting to keep the same method that you are using then I believe you should be able to do something like `For i = 1 to doc.Range.Characters.Count` instead of `... To 6` – K.Dᴀᴠɪs Oct 26 '18 at 16:53
  • Going back to the original scenario. I have table data in excel where 1 column specifies the string, and another specifies the font face for the string. My end goal is to populate the table-boxes in the word file with the string in the font face specified. – Keri Oct 27 '18 at 21:03