我的目的是通過正則運算式或通配符在word檔案中查找作者和括號內的年份(四位數字)并格式化顏色(從黑色變為紅色)以通過選擇相似的文本來選擇它們我的示例更改斜體并且僅更改所有文本括號內:
Dim Rng As Range
Dim n As Long
Application.ScreenUpdating = False
n = Selection.End
With Selection.Find
.MatchWildcards = True
.ClearFormatting
.Wrap = wdFindStop
.text = "\(*\)"
Do While .Execute
Set Rng = Selection.Range
If Rng.Start > n Then Exit Do
Rng.MoveStart unit:=wdCharacter, count:=1
Rng.MoveEnd unit:=wdCharacter, count:=-1
Rng.Font.Italic = True
Loop
End With
Application.ScreenUpdating = True
End Sub
uj5u.com熱心網友回復:
以下代碼可能會為您指明正確的方向
Option Explicit
Sub Test()
SetAuthorTextColour WdColorIndex.wdRed
End Sub
Sub SetAuthorTextColour(ByVal ipColour As WdColorIndex)
Dim myText As Word.Range
With ActiveDocument.StoryRanges(wdMainTextStory)
Do
With .Find
.MatchWildcards = True
.Text = "([(])(*)([0123456789]{4,4})(*)([)])"
.ClearFormatting
.Format = True
.Wrap = wdFindStop
.Execute
End With
If .Find.Found Then
Set myText = .Duplicate
myText.MoveStart unit:=wdCharacter, Count:=1
myText.MoveEnd unit:=wdCharacter, Count:=-1
' you may wish to change to using colorindex as
' I think color is deprecated.
myText.Font.ColorIndex = ipColour
End If
Loop While .Find.Found
End With
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/519607.html
標籤:vbams-word参考
下一篇:根據文本是否為粗體對行進行分組
