Get the range of each occurrence of a style

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!
 
K

Klaus Linke

No, unfortunately there's next to no macro support for multiple selections.
WordBasic.SelectSimilarFormatting (or as in your code Application.Run
"SelectSimilarFormatting") is pretty much as far as it goes.

:-(

What do you want to do once you have all the range.start and range.end?
If it's something that could be achieved by one or more "replace all"
replacements (normal or wildcard), that could speed things up.

Regards,
Klaus
 
R

Roy_Liu via OfficeKB.com

Hi, Klaus

Actually, I need to process the text between Externals.

In my document, Externals are used as delimiters, So i need to get the range
of each External, and then calculate the range of text between Externals.










Klaus said:
No, unfortunately there's next to no macro support for multiple selections.
WordBasic.SelectSimilarFormatting (or as in your code Application.Run
"SelectSimilarFormatting") is pretty much as far as it goes.

:-(

What do you want to do once you have all the range.start and range.end?
If it's something that could be achieved by one or more "replace all"
replacements (normal or wildcard), that could speed things up.

Regards,
Klaus
Hi, Everyone
[quoted text clipped - 53 lines]
Any help will be appreciated!
 
K

Klaus Linke

Roy_Liu via OfficeKB.com said:
Actually, I need to process the text between Externals.
In my document, Externals are used as delimiters, So i need to get the
range
of each External, and then calculate the range of text between Externals.


I've got a language problem:
What's an "external"? Some sort of character/bracket?

And where does the formatting/style come in, which you want to find the
range(s) for originally?


Regards,
Klaus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top