How can I scroll through each letter in MS Word using VBA?

I have about 100 Word documents that include transliteration of foreign names. The author of these documents used the special e2 font , which contains about a dozen special transliteration characters (all of them are available in the Microsoft Sans Serif font).

I would like to skip every letter of the document, and whenever .Font = "e2", I would like to skip a dozen letters (it is easy to guess that they are) and replace them with the equivalent of Microsoft Sans Serif. But I can’t understand how you can loop letters. Is it doable like looping through cells in an Excel spreadsheet?

+3
source share
4 answers

This may be one way to do this, but depending on the size of the document, execution can take a long time.

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
+4
source

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

0
source

Much 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
0
source

use the mswords format for search and replace. You will save time and a large file will not bother.

-1
source

Source: https://habr.com/ru/post/1735796/


All Articles