运行 突出显示特定样式的宏
Running highlight macro in specific styles
下面的代码试图突出显示 MS word 文件中的小写单词。但是我只需要在 3 种特定样式(标题级别 1、标题级别 2 和标题级别 3)中 运行。关于如何将此 运行 也用于其他两种特定样式的任何帮助?提前谢谢你。
Option Explicit
Dim myDoc As Document: Set myDoc = ActiveDocument
Dim myPara As Word.Paragraph
For Each myPara In myDoc.StoryRanges.Item(wdMainTextStory).Paragraphs
If myPara.Style.NameLocal = "Heading Level 1" Then
TitleParagraph myPara
End If
Next
End Sub
Public Sub TitleParagraph(ByVal ipPara As Word.Paragraph)
Dim myText As Range
For Each myText In ipPara.Range.Words
If LCase$(myText.Text) = myText.Text Then
myText.Words.Item(1).HighlightColorIndex = wdTurquoise
End If
Next
End Sub
与我在评论中发布的 instr 方法相比,一种更具可读性和可维护性的方法是
Option Explicit
' In the declarations section of the module
Public Type State
' You can also put other private to module variables here
Level1 As String 'Not very informative names
level2 As String
level3 As String
End Type
Private s As State
'in an initialisation routine
Public Sub init()
s.Level1 = myDoc.Styles(WdBuiltinStyle.wdStyleHeading1).NameLocal
s.level2 = myDoc.Styles(WdBuiltinStyle.wdStyleHeading2).NameLocal
s.level3 = myDoc.Styles(WdBuiltinStyle.wdStyleHeading3).NameLocal
End Sub
Sub ttest()
Dim myDoc As Document
Set myDoc = ActiveDocument
Dim myPara As Word.Range
For Each myPara In myDoc.StoryRanges.Item(wdMainTextStory).Paragraphs
If IsTitleParagraphHeading(myPara.Style.NameLocal) Then
TitleParagraph myPara
End If
Next
End Sub
Public Function IsTitleParagraphHeading(ByVal ipStyleName As String) As Boolean
IsTitleParagraphHeading = True
Select Case ipStyleName
Case s.Level1: Exit Function
Case s.level2: Exit Function
Case s.level3: Exit Function
End Select
IsTitleParagraphHeading = True
End Function
以下是更有效的解决问题的方法:
Sub Demo()
Application.ScreenUpdating = False
Dim i As Long, h As Long
h = Options.DefaultHighlightColorIndex
Options.DefaultHighlightColorIndex = wdTurquoise
With ActiveDocument.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = True
.Forward = True
.MatchWildcards = True
.Text = "<[a-z]@>"
.Replacement.Text = "^&"
.Replacement.Highlight = True
.Wrap = wdFindContinue
For i = 1 To 3
.Style = "Heading " & i
.Execute Replace:=wdReplaceAll
Next
End With
Options.DefaultHighlightColorIndex = h
Application.ScreenUpdating = True
End Sub
下面的代码试图突出显示 MS word 文件中的小写单词。但是我只需要在 3 种特定样式(标题级别 1、标题级别 2 和标题级别 3)中 运行。关于如何将此 运行 也用于其他两种特定样式的任何帮助?提前谢谢你。
Option Explicit
Dim myDoc As Document: Set myDoc = ActiveDocument
Dim myPara As Word.Paragraph
For Each myPara In myDoc.StoryRanges.Item(wdMainTextStory).Paragraphs
If myPara.Style.NameLocal = "Heading Level 1" Then
TitleParagraph myPara
End If
Next
End Sub
Public Sub TitleParagraph(ByVal ipPara As Word.Paragraph)
Dim myText As Range
For Each myText In ipPara.Range.Words
If LCase$(myText.Text) = myText.Text Then
myText.Words.Item(1).HighlightColorIndex = wdTurquoise
End If
Next
End Sub
与我在评论中发布的 instr 方法相比,一种更具可读性和可维护性的方法是
Option Explicit
' In the declarations section of the module
Public Type State
' You can also put other private to module variables here
Level1 As String 'Not very informative names
level2 As String
level3 As String
End Type
Private s As State
'in an initialisation routine
Public Sub init()
s.Level1 = myDoc.Styles(WdBuiltinStyle.wdStyleHeading1).NameLocal
s.level2 = myDoc.Styles(WdBuiltinStyle.wdStyleHeading2).NameLocal
s.level3 = myDoc.Styles(WdBuiltinStyle.wdStyleHeading3).NameLocal
End Sub
Sub ttest()
Dim myDoc As Document
Set myDoc = ActiveDocument
Dim myPara As Word.Range
For Each myPara In myDoc.StoryRanges.Item(wdMainTextStory).Paragraphs
If IsTitleParagraphHeading(myPara.Style.NameLocal) Then
TitleParagraph myPara
End If
Next
End Sub
Public Function IsTitleParagraphHeading(ByVal ipStyleName As String) As Boolean
IsTitleParagraphHeading = True
Select Case ipStyleName
Case s.Level1: Exit Function
Case s.level2: Exit Function
Case s.level3: Exit Function
End Select
IsTitleParagraphHeading = True
End Function
以下是更有效的解决问题的方法:
Sub Demo()
Application.ScreenUpdating = False
Dim i As Long, h As Long
h = Options.DefaultHighlightColorIndex
Options.DefaultHighlightColorIndex = wdTurquoise
With ActiveDocument.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = True
.Forward = True
.MatchWildcards = True
.Text = "<[a-z]@>"
.Replacement.Text = "^&"
.Replacement.Highlight = True
.Wrap = wdFindContinue
For i = 1 To 3
.Style = "Heading " & i
.Execute Replace:=wdReplaceAll
Next
End With
Options.DefaultHighlightColorIndex = h
Application.ScreenUpdating = True
End Sub