2017-03-06 33 views
0

我試圖遍歷所有這些都沒有一個表中的段落並突出顯示文本,例如:查找和選擇內更換是更換整個文檔

'Iterate All Paragraphs 
Dim p 
objWord.Options.DefaultHighlightColorIndex = finalColor 
For Each p In objDoc.Paragraphs 
    p.Range.Select 
    If Not objWord.Selection.Information(wdWithInTable) Then 
     With objWord.Selection.Range.Find 
      .ClearFormatting 
      .Highlight = False 
      .Forward = True 
      .Wrap = wdFindStop 
      .Replacement.ClearFormatting 
      .Replacement.Highlight = True 
      .Execute , , , , , , True, wdFindStop, , , wdReplaceAll 
     End With 
    End If 
Next 

條件objWord.Selection.Information(wdWithInTable)作品就好了,然而,即使在表格中,查找/執行也會替換整個文檔中的所有非高亮文本。

任何猜測爲什麼?

回答

1

我真的不認爲你應該爲此使用Find對象,但是如果你堅持,也許你應該將Selection.Range存儲在一個變量中,然後使用它的.Find屬性。

我會做這樣的事情:

Sub HighlightNonTableParagraphs() 
    Dim oDocument As Document 
    Dim oParagraph As Paragraph 
    Dim oRange As range 

    Set oDocument = ActiveDocument 

    For Each oParagraph In oDocument.Paragraphs 
     With oParagraph.range 
      If Not .Information(wdWithInTable) Then 
       .HighlightColorIndex = wdBlue 
      End If 
     End With 
    Next 
End Sub