如何搜索文本并检查VBA中的下划线字

问题描述:

如何搜索文本并在word文档中检查相同的文本以进行下划线。 任何人都可以帮我吗?如何搜索文本并检查VBA中的下划线字

Sub Underline() 
    Dim fnd As String 
    Dim n As Long 

    fnd = InputBox("Enter text to search" & vbCr & vbCr _ 
    & "Click OK to search the entire workbook for all instances of the search text.") 

    Dim x As Integer 

    x = 0 

    Do While x = 0 
     With Selection.Find 
      .ClearFormatting 
     End With 

     If fnd = False Then 
      x = 1 
      Exit Do 
     End If 
     Selection.Find.Execute 
     If .Underline = False Then 
      Selection.Comments.Add Range:=Selection.Range, Text:="pls underline text" 
      Selection.Find.Execute 
     End If 
    Loop 
End Sub 
+0

子下划线() 昏暗的测量值作为字符串 昏暗N当 测量值=的InputBox(“请输入要搜索的文本”&VBCR&VBCR _ 和“点击确定搜索整个工作簿的所有实例搜索文本。“) 昏暗x作为整数 x = 0的 做,当x = 0时 随着Selection.Find .ClearFormatting 结束随着 如果FND =假然后 X = 1 退出执行 结束如果 Selection.Find.Execute 如果.Underline = false,那么 Selection.Comments.Add范围:= Selection.Range,文本:= “请下划线的文本” Selection.Find.Execute 结束如果 循环 End Sub – user1553562 2012-07-31 05:46:24

+0

这不工作..你可以给我一个替代 – user1553562 2012-07-31 05:46:44

+0

@brettdj你可以建议我一种方法 – user1553562 2012-07-31 06:09:03

这是你在想什么?

Sub Sample() 
    Dim c As Range 
    Dim fnd As String 

    fnd = InputBox("Enter text to search" & vbCr & vbCr _ 
    & "Click OK to search the entire document for all instances of the search text.") 

    If fnd = "" Then Exit Sub 

    Set c = ActiveDocument.Content 

    c.Find.ClearFormatting 
    c.Find.Replacement.ClearFormatting 
    With c.Find 
     .Text = fnd 
     .Replacement.Text = "" 
     .Forward = True 
     .Wrap = wdFindStop 
    End With 

    c.Find.Execute 
    While c.Find.Found 
     If c.Font.Underline = wdUnderlineNone Then 
      c.Select 
      c.Comments.Add Range:=Selection.Range, Text:="pls underline text" 
     End If 
     c.Find.Execute 
    Wend 
End Sub 
+0

: - 这运行良好...但它也评论下划线文字....如何克服 – user1553562 2012-07-31 12:05:04

+0

我修改了代码。现在试试看:-) – 2012-07-31 12:09:30

+0

thnx sidd ...可以正常工作 – user1553562 2012-07-31 12:17:43