How can I loop through every letter in MS Word using VBA?

余生长醉 提交于 2019-12-05 12:32:17
Todd Main

This would be one way to do it, but depending on the size of the document, it may take a long time to execute.

Sub ChangeFonts()
Dim doc As Document
Set doc = ActiveDocument

For i = 1 To doc.Range.Characters.Count
    If doc.Range.Characters(i).Font.Name = "e2" Then
        doc.Range.Characters(i).Font.Name = "Microsoft Sans Serif"
    End If
Next

End Sub

You could also save it as docx, open it in a zip file and do a search/replace inside document.xml and fontTable.xml.

Way faster:

Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Font.Name = "e2"
    .Replacement.Font.Name = "Microsoft Sans Serif"
    .Forward = True
    .Format = True
    .Wrap = wdFindContinue
    .Execute Replace:=wdReplaceAll
  End With
End With
Application.ScreenUpdating = True
End Sub

use the mswords format find & replace. You will save time & large file won't be a concern.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!