我需要做一个宏来找到一个特定风格的所有出现(他们都是标题,共享相同的风格),然后在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发布于 2015-11-04 21:13:30
有两种方法。我将对这两种方法进行简要描述。
Selection.Find ... End With结构中的几乎所有东西。然后,您可以删除不需要的重复代码。这将大大缩短您的代码。使用选择对象时,所做的设置会影响“查找”对话框。因此它们会一直存在,直到您(或其他代码)更改它们。这就是为什么这对你来说是有效的。
例如,您可以传递要搜索的样式名称和高亮颜色。方法签名将类似于:
Sub FindAndHighlight(TextToFind as String, HighlightColor as Long)你可以这样称呼它:
FindAndHighlight "Rashi Char", wdGreen这将有助于缩短您的代码(并使其更容易阅读),因为它将所有重复的操作放在一个位置,因此您只需编写一次。
发布于 2015-11-06 14:44:44
您想要使用的是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进行了测试,它工作得很好。
https://stackoverflow.com/questions/33410057
复制相似问题