如何使用 VBA 循环遍历 MS Word 中的每个字母?

发布于 2024-08-23 17:36:06 字数 279 浏览 3 评论 0原文

我有大约 100 个 Word 文档,其中包括外国名字的音译。这些文档的作者使用了一种名为e2的特殊字体,它有大约十几个特殊音译字符(所有这些都可以在Microsoft Sans Serif字体中找到)。

我想循环遍历文档的每个字母,每当 .Font = "e2" 我想循环遍历这十几个字母(很容易猜到它们是什么)并将它们替换为相当于 Microsoft Sans Serif。但我不知道如何循环遍历字母。这是否可行,就像循环遍历 Excel 电子表格中的单元格一样?

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).

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

肩上的翅膀 2024-08-30 17:36:06

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

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

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
甩你一脸翔 2024-08-30 17:36:06

更快:

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

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
书信已泛黄 2024-08-30 17:36:06

您还可以将其另存为 docx,在 zip 文件中打开它,然后在 document.xml 和 fontTable.xml 中进行搜索/替换。

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

请你别敷衍 2024-08-30 17:36:06

使用 mswords 格式查找 &代替。您将节省时间&大文件不会成为问题。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文