按格式查找范围,然后更改该格式(不使用替换选项)

Find range by format and then change that format (not using the replace option)

这里是新手 Jan 的第一个问题

我正在尝试了解我的 vba 代码的行为。该代码正在搜索具有特定格式的 words/sentences 。找到后,我想通过修改返回的范围来调整这些 words/sentences。如果我这样调整范围 这不会产生任何问题(循环继续)。如果我删除返回的范围,循环也会继续。

Dim myFind As Find
Dim myRange As Range
        
Set myRange = Application.ActiveDocument.Content
Set myFind = myRange.Find
        
With myFind
    .ClearFormatting
    .Font.Underline = wdUnderlineDouble
    .Font.Italic = True
End With
        
Do While myFind.Execute = True
    myRange.Font.Bold = True
    myRange.Font.StrikeThrough = True 
Loop

但是,如果我调整搜索中使用的格式 (myRange.font.italic = false),则循环会在找到第一个后退出。

Dim myFind As Find
Dim myRange As Range

Set myRange = Application.ActiveDocument.Content
Set myFind = myRange.Find

With myFind
    .ClearFormatting
    .Font.Underline = wdUnderlineDouble
    .Font.Italic = True
End With

Do While myFind.Execute = True
    myRange.Font.Italic = False  
Loop

谁能帮我理解为什么会这样?如果我删除范围 (myRange.delete),循环将继续(这让我感到困惑)。看来我无法以这种方式撤消我正在搜索的内容的格式......但是删除范围没有问题。我试图查找有关此问题的文档,但找不到有关我的特定问题的信息。

我通过选择范围然后执行下一个查找然后修改选择来解决这个问题。这行得通......但我仍然想了解发生了什么。

P.S。这只是代码的一部分。该代码将用于创建一些修订。这就是我不使用 find/replacement 选项

的原因

谢谢!

一些额外的上下文

我正在处理大字文件,我想自动找到 'specially formatted text' 并自动修改此文本。最终我希望格式化的文本成为轨道 change/revision(这也是我不使用 find/replace 选项的原因)。但我首先想了解我的代码中发生了什么。我知道 .execute 正在返回一个 false,这是循环退出的原因。我不明白为什么 .execute 返回错误,而文档中还有其他 words/sentences 具有我正在搜索的格式

这可能应该是一个评论,但我太新无法添加评论。

我可以验证这是否发生在我的系统上。澄清一下,您要做的是更改目标的格式,以便在完成 double-strikethrough 和 bold?

后将其从搜索条件中删除

您的代码在第一次删除斜体后退出循环。这意味着它已经完成了查找。我无法解释这个。

当我将以下代码替换为更改斜体的代码时,找到的每个实例都会出现一个消息框。

`MsgBox "Still in Loop"`

我还是不清楚你为什么不使用替换功能。

================== 编辑 - 添加以回应评论 ==================

尝试以下操作。它多次检查文档,但似乎有效。

 `Sub replacer()
     ' 
     Dim myFind As Find
     Dim myRange As Range
     Set myRange = Application.ActiveDocument.Content
     Set myFind = myRange.Find
     With myFind
      .ClearFormatting
      .Font.Underline = wdUnderlineDouble
      .Font.Italic = True
     End With
     Do While myFind.Execute = True
      myRange.Font.Bold = True
      myRange.Font.StrikeThrough = True
     Loop
     '
 StartOver:
     Set myRange = Application.ActiveDocument.Content
     Set myFind = myRange.Find
     With myFind
      .ClearFormatting
      .Font.Underline = wdUnderlineDouble
      .Font.Italic = True
      .Font.StrikeThrough = True
      .Font.Bold = True
     End With
     Do While myFind.Execute = True
 '      MsgBox "Still in Loop"
       myRange.Font.Italic = False
       If myFind.Found = True Then GoTo StartOver
     Loop
 End Sub`

我仍然无法解释为什么会这样。

在VBA中使用查找时,最好设置.Wrap = wdFindStop以避免进入连续循环。

如果您不打算使用 Replace,将 Execute 的结果分配给一个变量也是一个好习惯(尽管有一个 Found 属性它被认为是不可靠的)。

对找到的范围执行操作,然后继续 Find 需要在循环中再次使用 Execute。还需要折叠找到的范围,以便 Find 可以继续过去。

当满足 Find 条件时,范围将重新定义为找到的范围。如果您更改该范围的属性,使它们不再满足 Find 条件,Do While myFind.Execute = True 评估为 False,结束循环。

以下例程适用于您在问题中提到的所有条件。

Sub FindFormatting()
  Dim findRange As Range
  Dim findSucess As Boolean
    
  Set findRange = ActiveDocument.Content
    
  With findRange.Find
    .ClearFormatting
    .Font.Underline = wdUnderlineDouble
    .Font.Italic = True
    .Wrap = wdFindStop
    findSucess = .Execute
    
    Do While .Found
      With findRange.Font
        .Italic = False
      End With
      'collapse range to continue
      findRange.Collapse wdCollapseEnd
      findSucess = .Execute
    Loop
  End With
End Sub