【问题标题】:How can I loop through every letter in MS Word using VBA?如何使用 VBA 遍历 MS Word 中的每个字母?
【发布时间】:2020-07-04 06:48:49
【问题描述】:

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

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

【问题讨论】:

    标签: vba ms-word


    【解决方案1】:

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

    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
    

    【讨论】:

      【解决方案2】:

      更快:

      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
      

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          【解决方案4】:

          使用 mswords 格式查找和替换。您将节省时间且无需担心大文件。

          【讨论】:

            猜你喜欢
            • 2015-11-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-05-17
            • 1970-01-01
            相关资源
            最近更新 更多