【问题标题】:Find String in Word and Bold Selection在单词和粗体选择中查找字符串
【发布时间】:2019-04-12 10:08:18
【问题描述】:

上下文

我有一个 Word 文档,我想对其进行检查以确保它符合标准格式。该文档用 1 行项目符号填充。我想解析每一行并检查与单词“Testing”匹配的每个单词,以确保它是粗体。

问题

我能够解析每一行并检查该行是否包含该单词,但不确定如何检查是否只有找到的选择是粗体的。

当前代码

Sub checkWords()

Dim singleLine As Paragraph      'bullet point document so just went by each paragraph
Dim lineText As String
Dim pos As Integer

  For Each singleLine In ActiveDocument.Paragraphs
    pos = InStr(singleLine, "Testing")

    If pos <> 0 Then
        MsgBox ("Testing InStr")
    End If


'PSEUDOCODE
'If singleLine.Range.Font.Bold <> True Then
'   Do This
'End If

Next singleLine

End Sub

【问题讨论】:

    标签: vba replace ms-word


    【解决方案1】:

    您最初的问题说您想“解析每一行并检查与单词“Testing”匹配的每个单词,以确保它是粗体的”。这并不意味着涉及任何决策。允许用户选择更复杂:

    Sub Demo()
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "Testing"
        .Replacement.Text = ""
        .Forward = True
        .Format = False
        .Wrap = wdFindStop
        .MatchWildcards = True
        .Execute
      End With
      Do While .Find.Found
        .Select
        Select Case MsgBox("Bold this instance?", vbYesNoCancel)
          Case vbCancel: Exit Sub
          Case vbNo: .Font.Bold = False
          Case vbYes: .Font.Bold = True
        End Select
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
    End Sub
    

    以上内容可让您取消已加粗的内容。要仅处理非粗体内容,您可以使用:

    Sub Demo()
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "Testing"
        .Replacement.Text = ""
        .Forward = True
        .Format = True
        .Font.Bold = False
        .Wrap = wdFindStop
        .MatchWildcards = True
        .Execute
      End With
      Do While .Find.Found
        .Select
        Select Case MsgBox("Bold this instance?", vbYesNoCancel)
          Case vbCancel: Exit Sub
          Case vbYes: .Font.Bold = True
        End Select
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
    End Sub
    

    【讨论】:

    • 是的,很抱歉最初的混乱!这个答案很完美!
    • 有没有隔离.Find.Found 来检查它的属性?比如If .Font.Bold = True Then在提示MsgBox之前预检查是否已经加粗了?
    • @Yahtzee 你可能可以在select case 之前检查If Selection.Font.Bold = True Then,但我不是通过机器来仔细检查
    • 您还可以添加一个小变量来计算该实例是否为粗体,以便您可以报告
    • @Marcucciboy2 成功了!我将End If 放在.Find.Execute 之后,这会使程序进入无响应状态。小错误,谢谢!
    【解决方案2】:

    试试这个代码:

    Sub checkWords()
       Dim singleLine As Paragraph
       Dim rng As Range
       Dim pos As Integer
    
       For Each singleLine In ActiveDocument.Paragraphs
          Set rng = singleLine.Range
          pos = InStr(1, rng.Text, "Testing")
    
          If pos <> 0 Then
             rng.Start = pos
             rng.End = pos + Len("Testing")
    
             If rng.Font.Bold Then
                MsgBox "This is bold"
             End If
          End If
       Next
    End Sub
    

    【讨论】:

      【解决方案3】:

      你让这件事变得比需要的困难得多;一个简单的 Find/Replace 就可以完成这项工作,Find/Replace 宏也是如此:

      Sub Demo()
      Application.ScreenUpdating = False
      With ActiveDocument.Range
        With .Find
          .ClearFormatting
          .Replacement.ClearFormatting
          .Replacement.Font.Bold = True
          .Text = "Testing"
          .Replacement.Text = "^&"
          .Forward = True
          .Format = True
          .Wrap = wdFindContinue
          .MatchWildcards = True
          .Execute Replace:=wdReplaceAll
        End With
      End With
      Application.ScreenUpdating = True
      End Sub
      

      【讨论】:

      • 我同意,这会起作用,但这会自动加粗单词!我可能不够清楚,会编辑我的问题,但我想提供一份找到的实例的报告,让用户选择是否加粗。可以在这种情况下应用吗?
      • 类似你在这里使用的东西可能也可以工作stackoverflow.com/questions/51989997/…
      • @Yahtzee 除非您以交互方式运行流程,而不是使用缺乏上下文的“报告”,否则很难看出如何知道哪些实例要加粗或不加粗。
      猜你喜欢
      • 1970-01-01
      • 2015-12-05
      • 2019-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多