如何使用VBA遍历MS Word中的每个字母? [英] How can I loop through every letter in MS Word using VBA?

查看:522
本文介绍了如何使用VBA遍历MS Word中的每个字母?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我大约有100个Word文档,其中包括外国名字的音译.这些文档的作者使用了一种叫做 e2 的特殊字体,其中包含大约十二种特殊音译字符(所有这些字符都可以使用Microsoft Sans Serif字体获得).

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

我想遍历文档的每个字母,每当.Font = "e2"我想遍历十二个字母(很容易猜出它们是什么)并将其替换为Microsoft Sans Serif等效语言.但是我不知道如何通过字母循环.像在Excel电子表格中的单元格中循环一样吗?

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

推荐答案

这是一种实现方法,但是根据文档的大小,执行它可能需要很长时间.

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

这篇关于如何使用VBA遍历MS Word中的每个字母?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆