我需要制作一个宏来查找特定样式的所有场景(它们都是共享相同样式的标题),然后以5种不同的颜色突出显示。我已经有一个代码来执行此操作,但我需要在文档末尾重复此操作。我知道代码非常粗糙,所以如果有人能帮助我缩短代码,并且效率更高,我也会感激。
Sub Highlight()
'
' highlight Macro
'
'
Selection.HomeKey Unit:=wdStory
Options.DefaultHighlightColorIndex = wdYellow
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Rashi Char")
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Highlight = True
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchKashida = False
.MatchDiacritics = False
.MatchAlefHamza = False
.MatchControl = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceOne
Options.DefaultHighlightColorIndex = wdBrightGreen
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Rashi Char")
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Highlight = True
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchKashida = False
.MatchDiacritics = False
.MatchAlefHamza = False
.MatchControl = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceOne
Options.DefaultHighlightColorIndex = wdTurquoise
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Rashi Char")
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Highlight = True
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchKashida = False
.MatchDiacritics = False
.MatchAlefHamza = False
.MatchControl = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceOne
Options.DefaultHighlightColorIndex = wdPink
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Rashi Char")
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Highlight = True
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchKashida = False
.MatchDiacritics = False
.MatchAlefHamza = False
.MatchControl = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceOne
Options.DefaultHighlightColorIndex = wdGreen
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Rashi Char")
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Highlight = True
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchKashida = False
.MatchDiacritics = False
.MatchAlefHamza = False
.MatchControl = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceOne
End Sub
答案 0 :(得分:0)
有两种方法。我将简要介绍一下。
Selection.Find ... End With
构造中的几乎所有内容。然后,您可以删除不需要的重复代码。这会缩短你的代码。使用Selection对象时,所做的设置会影响“查找”对话框。所以它们会一直存在,直到你(或其他代码)改变它们为止。这就是为什么这对你有用。
例如,您可以传递搜索的样式名称和突出显示颜色。方法签名类似于:
Sub FindAndHighlight(TextToFind as String, HighlightColor as Long)
你会这样称呼它:
FindAndHighlight "Rashi Char", wdGreen
这将有助于缩短您的代码(并使其更易于阅读),因为它会将所有操作重复放在一个位置,因此您只需编写一次。
答案 1 :(得分:0)
您要使用的是Do While
,请参阅下面的脚本。
Sub Highlight()
'
' highlight Macro
'
'
'Going to the top of the Document
Selection.HomeKey Unit:=wdStory
'Setting up your Selection.Find
With Selection.find
.text = ""
.Replacement.text = ""
.style = ActiveDocument.Styles("Rashi Char")
.Forward = True
.Wrap = wdFindAsk
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchKashida = False
.MatchDiacritics = False
.MatchAlefHamza = False
.MatchControl = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
'Setting your counter for selecting which colour for highlighting
d = 1
'Executing the search
Do While .Execute
'If d = 1 then its 1 of 5
If d = 1 Then
Selection.Range.HighlightColorIndex = wdYellow
'If d = 2 then its 2 of 5
ElseIf d = 2 Then
Selection.Range.HighlightColorIndex = wdBrightGreen
'If d = 3 then its 3 of 5
ElseIf d = 3 Then
Selection.Range.HighlightColorIndex = wdTurquoise
'If d = 4 then its 4 of 5
ElseIf d = 4 Then
Selection.Range.HighlightColorIndex = wdPink
'If d = 5 then its 5 of 5
ElseIf d = 5 Then
Selection.Range.HighlightColorIndex = wdGreen
End If
'Incrementing d
d = d + 1
'If d = 6 then you have completed the highlighting loop
'Then set d back to 1
If d = 6 Then d = 1
Loop
'Ending the Selectin.Find With
End With
End Sub
我使用Header 1
作为.style
进行了测试,效果非常好。