R
Roy_Liu
Hi, Everyone
I'm writing a code to get the range of each occurrence of a custom style, say
"External", I use a Find loop to search through the document, the code is
like this:
====================================================================
Sub myFind()
Dim aRange As Range
Dim aStart As Long, aEnd As Long
Dim aa, bb
Application.ScreenUpdating = False
aa = Timer
Set aRange = ActiveDocument.Range(0, 0)
With aRange.Find
.Format = True
.Style = "External"
.Text = ""
Do While .Execute
aStart = aRange.Start
aEnd = aRange.End
Loop
End With
bb = Timer
MsgBox bb - aa & "Ok"
End Sub
=========================================================================
This Sub runs well, but then i found the following code occasionally:
=========================================================================
Sub myFind()
Dim aa, bb
aa = Timer
With Selection
.HomeKey wdStory
With .Find
.ClearFormatting
.Style = "External"
.Format = True
End With
.Find.Execute
End With
Application.Run "SelectSimilarFormatting"
bb = Timer
MsgBox bb - aa & "Ok"
End Sub
=========================================================================
It's neat and fast than the first one. Then, I wonder if there is a way to
get the range of each occurrence of "External"?
Any help will be appreciated!
I'm writing a code to get the range of each occurrence of a custom style, say
"External", I use a Find loop to search through the document, the code is
like this:
====================================================================
Sub myFind()
Dim aRange As Range
Dim aStart As Long, aEnd As Long
Dim aa, bb
Application.ScreenUpdating = False
aa = Timer
Set aRange = ActiveDocument.Range(0, 0)
With aRange.Find
.Format = True
.Style = "External"
.Text = ""
Do While .Execute
aStart = aRange.Start
aEnd = aRange.End
Loop
End With
bb = Timer
MsgBox bb - aa & "Ok"
End Sub
=========================================================================
This Sub runs well, but then i found the following code occasionally:
=========================================================================
Sub myFind()
Dim aa, bb
aa = Timer
With Selection
.HomeKey wdStory
With .Find
.ClearFormatting
.Style = "External"
.Format = True
End With
.Find.Execute
End With
Application.Run "SelectSimilarFormatting"
bb = Timer
MsgBox bb - aa & "Ok"
End Sub
=========================================================================
It's neat and fast than the first one. Then, I wonder if there is a way to
get the range of each occurrence of "External"?
Any help will be appreciated!