尝试为重复的段落编写 VBA 宏
Trying to Write a VBA Macro for Duplicated Paragraphs
我正在尝试编辑宏以识别 Word 文档中的重复段落。此代码很好地识别了第二个实例,但我试图让它以不同的颜色突出显示第一个实例,但我无法终生管理它。
Sub highdupParagraphs()
Dim p As Paragraph
Dim d As New Scripting.Dictionary
Dim t As Variant
Dim i As Integer
Dim StartTime As Single
StartTime = Timer
' collect duplicates
For Each p In ActiveDocument.Paragraphs
t = p.Range.Text
If t <> vbCr Then
If Not d.Exists(t) Then d.Add t, New Scripting.Dictionary
d(t).Add d(t).Count + 1, p
End If
Next
' highlight
For Each t In d
For i = 2 To d(t).Count
d(t)(i).Range.HighlightColorIndex = wdPink
Next
Next
Application.ScreenUpdating = True
MsgBox "This code ran successfully in " & Round(Timer - StartTime, 2) & " seconds", vbInformation
End Sub
在一个循环中:
Sub highdupParagraphs()
Dim p As Paragraph
Dim d As New Scripting.Dictionary
Dim t As Variant, StartTime As Single
StartTime = Timer
For Each p In ActiveDocument.Paragraphs
t = p.Range.Text
If t <> vbCr Then
If Not d.Exists(t) Then
d.Add t, p 'store the first instance
Else
If Not d(t) Is Nothing Then
'color first instance and unset it
d(t).Range.HighlightColorIndex = wdYellow
Set d(t) = Nothing
End If
p.Range.HighlightColorIndex = wdPink
End If
End If
Next
MsgBox "This code ran successfully in " & Round(Timer - StartTime, 2) & " seconds", vbInformation
End Sub
我正在尝试编辑宏以识别 Word 文档中的重复段落。此代码很好地识别了第二个实例,但我试图让它以不同的颜色突出显示第一个实例,但我无法终生管理它。
Sub highdupParagraphs()
Dim p As Paragraph
Dim d As New Scripting.Dictionary
Dim t As Variant
Dim i As Integer
Dim StartTime As Single
StartTime = Timer
' collect duplicates
For Each p In ActiveDocument.Paragraphs
t = p.Range.Text
If t <> vbCr Then
If Not d.Exists(t) Then d.Add t, New Scripting.Dictionary
d(t).Add d(t).Count + 1, p
End If
Next
' highlight
For Each t In d
For i = 2 To d(t).Count
d(t)(i).Range.HighlightColorIndex = wdPink
Next
Next
Application.ScreenUpdating = True
MsgBox "This code ran successfully in " & Round(Timer - StartTime, 2) & " seconds", vbInformation
End Sub
在一个循环中:
Sub highdupParagraphs()
Dim p As Paragraph
Dim d As New Scripting.Dictionary
Dim t As Variant, StartTime As Single
StartTime = Timer
For Each p In ActiveDocument.Paragraphs
t = p.Range.Text
If t <> vbCr Then
If Not d.Exists(t) Then
d.Add t, p 'store the first instance
Else
If Not d(t) Is Nothing Then
'color first instance and unset it
d(t).Range.HighlightColorIndex = wdYellow
Set d(t) = Nothing
End If
p.Range.HighlightColorIndex = wdPink
End If
End If
Next
MsgBox "This code ran successfully in " & Round(Timer - StartTime, 2) & " seconds", vbInformation
End Sub