title: Word VBA: iterating through characters incredibly slow
body: <p>I have a macro that changes single quotes in front of a number to an apostrophe (or close single curly quote).  Typically when you type something like "the '80s" in word, the apostrophe in front of the "8" faces the wrong way.  The macro below works, but it is incredibly slow (like 10 seconds per page).  In a regular language (even an interpreted one), this would be a fast procedure.  Any insights why it takes so long in VBA on Word 2007?  Or if someone has some find+replace skills that can do this without iterating, please let me know.</p>
  
  <pre><code>Sub FixNumericalReverseQuotes()
      Dim char As Range
      Debug.Print "starting " + CStr(Now)
      With Selection
          total = .Characters.Count
          ' Will be looking ahead one character, so we need at least 2 in the selection
          If total &lt; 2 Then
              Return
          End If
          For x = 1 To total - 1
              a_code = Asc(.Characters(x))
              b_code = Asc(.Characters(x + 1))
  
              ' We want to convert a single quote in front of a number to an apostrophe
              ' Trying to use all numerical comparisons to speed this up
              If (a_code = 145 Or a_code = 39) And b_code &gt;= 48 And b_code &lt;= 57 Then
                  .Characters(x) = Chr(146)
              End If 
          Next x
      End With
      Debug.Print "ending " + CStr(Now)
  End Sub
  </code></pre>