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

时间:2012-07-31 05:37:13

标签: vba ms-word word-vba

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

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

2 个答案:

答案 0 :(得分:2)

这是你在尝试的吗?

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

答案 1 :(得分:-1)

在Siddharth Rout的代码中注释以下行

c.Comments.Add范围:=选择范围,文字:=“ pls下划线文字”