取决于单词是否粗体,更改单词样式的速度很慢 [英] Changing style of words, depending on whether the word is bold or not, is slow

查看:60
本文介绍了取决于单词是否粗体,更改单词样式的速度很慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改活动文档中几个单词的样式.

I´d like to change the style of several words within the active document.

  • 每个单词可能会出现多次.
  • 有些单词为粗体,有些则为非粗体.

单词存储在数组Arr()中.

如果找到的单词X为粗体,则将样式替换为StyleA,如果其为粗体,则将其更改为StyleB.

If the word X found is unbold, replace the style to StyleA and if it is bold change it to StyleB.

以下代码有两个问题.

1)具有粗体和非粗体格式的单词更改为StyleA.
2)执行时间慢.我测试了从1到5的循环,花了将近一分钟.

1) Words with bold and not bold format change to StyleA.
2) The execution time is slow. I tested the loop from 1 to 5 and it took almost a minute.

Sub ReplaceStyle()
    Dim Arr(1 to 200)

    Arr(1) = "Word1"
    Arr(2) = "Word2"
    .
    .
    .
    Arr(200) = "Word200"

    For i = 1 To Ubound(Arr)
        With Selection.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Text = Arr(i)
            .Replacement.Text = ""

            If Selection.Font.Bold = False Then
                .Replacement.Style = ActiveDocument.Styles("StyleA")
            Else
                .Replacement.Style = ActiveDocument.Styles("StyleB")
            End If

            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            .Execute Replace:=wdReplaceAll
        End With
    Next

End Sub

推荐答案

问题中代码的逻辑错误. Find需要在搜索词中实际上位于 ,代码才能测试其是否为粗体.

The logic of the code in the question is faulty. Find needs to have actually located the search term before code can test whether it's bold or not bold.

可能有两种基本方法

  1. 搜索词,找到后执行测试并应用样式
  2. 每个词搜索两次,一次为粗体,一次为非粗体

您需要进行测试,但是根据经验,我相信第二种方法会更快,因为它可以使用ReplaceAll.

You'd need to test, but based on experience I believe the second approach would be faster as it can use ReplaceAll.

以下代码根据问题中的代码演示了原理.请注意,它使用Range对象,而不是Selection对象,因为它通常更有效.

The code below demonstrates the principle, based on the code in the question. Note that it uses a Range object, rather than Selection as this is generally more efficient.

Sub FindReplaceFormattingVariations()
    Dim rng As Word.Range
    Dim searchTerm As String
    Dim Arr(1 to 200)

    Arr(1) = "Word1"
    Arr(2) = "Word2"
    .
    .
    .
    Arr(200) = "Word200"

For i = 1 To Ubound(Arr) 
    searchTerm = Arr(i)
    Set rng = ActiveDocument.content
    With rng.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = searchTerm
        .Font.Bold = True
        .Replacement.Style = ActiveDocument.Styles("StyleA")
        .Execute Replace:=wdReplaceAll
    End With

    With rng.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = searchTerm
        .Font.Bold = False
        .Replacement.Style = ActiveDocument.Styles("StyleB")
        .Execute Replace:=wdReplaceAll
    End With
Next    
End Sub

这篇关于取决于单词是否粗体,更改单词样式的速度很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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