Text selection...

S

Sasa

I was thinking about this thing: Is there a way to select
some text betwen some specified characters? What I mean is
described below...

For example: I got line in document containing text:
.....blah blah blah and "some text" blah blah.....

What I want to select is only this "some text" text marked
by quotes but without quotes.
Is this possible? Is there some code to do it?

Thanks for any clues.
Regards.

Sasa
 
J

Jean-Guy Marcil

Hi Sasa,

Try something like
'_______________________________________
Dim FoundText As Range

With Selection.Find
.Text = """*"""
'or
' .Text = "«*»"
'depending on the type of quotes you are using.
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute

Set FoundText = Selection.Range
FoundText.SetRange FoundText.Start + 1, _
FoundText.End - 1
FoundText.Select
'_______________________________________

But without knowing more about your specifics, it is difficult to give a
more precise answer.

HTH
 
H

Helmut Weber

Hi Sasa,
just one possibility, but a fast one:
Selection.WholeStory ' for testing only
Selection.Collapse ' for testing only
Dim r As Range
Set r = ActiveDocument.Range
With r.Find
.Text = """*"""
.MatchWildcards = True
If .Execute Then
r.Start = r.Start + 1
r.End = r.End - 1
r.Select
End If
End With
End Sub
Some options of "find" are left out for brevity.
Greetings from Bavaria, Germany
Helmut Weber
"red.sys" & chr$(64) & "t-online.de"
Word XP, NT 4.0
 
S

Sasa

The text inside the quotes is always different. Some times
it has only few characters and some times few sentences.
The only thing which is always the same are these ""
quotes.

What do you think? Can something be done here?

Regards.
Sasa
 
J

Jean-Guy Marcil

Hi Sasa,

Yes, something can be one.
Have you tried either Helmut's or my code? I think either of them does
exactly what you need.

Try the code and report back if you run into other unforseen problems.
 
S

Sasa

Works perfect. Thanks. Both of you.
One more thing. What if text is between some other symbols
like <, >, or #? What should be wrote instead of """*"""?

Best regards.
Sasa
 
J

Jean-Guy Marcil

Hi Sasa,

Try the following, just uncomment the ".Text" line you need as a search
criteria. Notice that I added a little something at the end to in case the
the string is not found.
Because we are using the wild card "*" to signify "any text" (thus the "
..MatchWildcards = True" line) we have to be careful when using the
delimiting charcters in the search string. "#" is not a wildcard or a
special character, so you can use "#*#". OTOH (On the other hand), "<" or
">" are special characters, so you cannot use "<*>" (which roughly means
begining and end of any word..., it does not make sense!), so you have to
use "\<*\>". Use "\" to precede any special character when you want to use
them as actual text whenever doing a search involving special characters

'_______________________________________
Dim FoundText As Range

With Selection.Find
' .Text = "#*#"
' .Text = "\<*\>"
' .Text = """*"""
'or
' .Text = "«*»"
'depending on the type of quotes you are using.
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute

If Selection.Find.Found Then
Set FoundText = Selection.Range
FoundText.SetRange FoundText.Start + 1, _
FoundText.End - 1
FoundText.Select
End If
'_______________________________________

HTH
 

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