5,139
社区成员
发帖
与我相关
我的任务
分享
Sub SetStyle()
Selection.Find.ClearFormatting
With Selection.Find
.Text = "(\([0-9]{4}-[0-9]{2}-[0-9]{2}\))"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Do While Selection.Find.Execute
Selection.Style = ActiveDocument.Styles("日期格式")
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.Style = ActiveDocument.Styles("标题格式")
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.MoveDown Unit:=wdLine, Count:=1
Loop
End Sub
Selection.Find.ClearFormatting
With Selection.Find
.Text = "(\([0-9]{4}-[0-9]{2}-[0-9]{2}\))"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Do While Selection.Find.Execute
Selection.Style = ActiveDocument.Styles("日期格式")
Loop
End Sub
Sub FindAndApply()
Const StrPattern As String = "\d\d\d\d-[01]\d-[0-3]\d"
Const MyStyleDate As String = "我的日期样式", MyStyleHeading As String = "我的标题样式"
Dim rngToSearch As Range, reRegExp As Object, oMatches As Object
Dim lStart As Long, lEnd As Long, lCount As Long, lTmp As Long
Selection.GoTo What:=wdGoToPage, Which:=wdGoToFirst, Count:=1
Set rngToSearch = Selection.Range
rngToSearch.MoveEnd wdStory, 1
Set reRegExp = CreateObject("VBScript.RegExp")
lCount = 0
Do While (1)
reRegExp.Global = False
reRegExp.IgnoreCase = True
reRegExp.MultiLine = False
reRegExp.Pattern = StrPattern
Set oMatches = reRegExp.Execute(rngToSearch.Text)
If oMatches.Count = 0 Then Exit Do
lStart = rngToSearch.Start + oMatches(0).FirstIndex
lEnd = lStart + oMatches(0).Length
ThisDocument.Range(Start:=lStart, End:=lEnd).Select
Selection.Style = ThisDocument.Styles(MyStyleDate)
Selection.HomeKey Unit:=wdLine, Extend:=wdMove
lTmp = Selection.Start
Selection.GoTo What:=wdGoToLine, Which:=wdGoToPrevious, Count:=1
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
If Selection.Start < lTmp Then Selection.Style = ThisDocument.Styles(MyStyleHeading)
lCount = lCount + 1
Set rngToSearch = ThisDocument.Range(Start:=lEnd, End:=ThisDocument.Content.End)
Loop
MsgBox "共匹配了" & lCount & "次。已应用“" & MyStyleDate & "”和“" & MyStyleHeading & "”样式。", , "匹配结果"
Set rngToSearch = Nothing
Set reRegExp = Nothing
Set oMatches = Nothing
End Sub
Const MyStyleDate As String = "我的日期样式", MyStyleHeading As String = "我的标题样式"