2012-07-31 30 views
-1

如何搜索文本並在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

回答

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 
+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