ActiveDocument Question

J

jerem

Can anyone tell me how to set the oRng below

Dim oRng As Range
Set oRng = ActiveDocument.Content

to just the selected portion (that is, a portion that I've manually
selected) of the active document rather than having the entire document
designated as the range (if that's what Content means). So, I want to
replace .Content with whatever keyword means "just the user's selected
portion of the document". Went through the entire intellisense list to try
to find that, but didn't see anything there usable.

Would be nice to have a VBA dictionary out on the web - sort of like
webster's online dictionary.

Thanks for your help.
 
G

Gordon Bentley-Mix

jerem,

If I understand your questions correctly, then I believe what you are
looking for is Selection.Range.
--
Cheers!

Gordon Bentley-Mix
Word MVP

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
J

jerem

Perfect. Thank you.


Gordon Bentley-Mix said:
jerem,

If I understand your questions correctly, then I believe what you are
looking for is Selection.Range.
--
Cheers!

Gordon Bentley-Mix
Word MVP

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
J

jerem

Ooops - I must have spoken too soon. I changed Set oRng =
ActiveDocument.Content to Set oRng = Selection.Range and it worked once -
that is, it only underlined those I wanted underlined and left those with a
similar format untouched. When I ran the macro again it was no longer
discertionary. It changed them all. So, maybe it will help to see the code:

Dim oRng As Range
Set oRng = ActiveDocument.Content
With oRng.Find
.ClearFormatting
.Text = "\([^034^0147]<*>[^034^0148]\)"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
Do While .Execute
With oRng
.MoveEnd Unit:=wdCharacter, Count:=-2
.MoveStart Unit:=wdCharacter, Count:=2
.Font.Underline = True
.Collapse wdCollapseEnd
End With
Loop
End With
End Sub

I like this code but it runs rampant. I want to put a leash on this by
controlling what it changes. So, I want to select a portion of the document,
then run the macro and have the macro only underline the instances contained
in the selected text only, not run amok.

Thanks for your help.
 
T

Tony Jollans

When you do repeated Finds in this way, Word does not remember the original
Range. It is, therefore, down to you to remember it and terminate the loop
as soon as it goes outside that Range. This is one way:

Dim oRng As Range, oRng2 As Range
Set oRng = Selection.Range 'ActiveDocument.Content
Set oRng2 = oRng.Duplicate
With oRng.Find
.ClearFormatting
.Text = "\([^034^0147]<*>[^034^0148]\)"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
Do While .Execute
If Not oRng.InRange(oRng2) Then Exit Do
With oRng
.MoveEnd Unit:=wdCharacter, Count:=-2
.MoveStart Unit:=wdCharacter, Count:=2
.Font.Underline = True
.Collapse wdCollapseEnd
End With
Loop
End With

--
Enjoy,
Tony

www.WordArticles.com

jerem said:
Ooops - I must have spoken too soon. I changed Set oRng =
ActiveDocument.Content to Set oRng = Selection.Range and it worked once -
that is, it only underlined those I wanted underlined and left those with
a
similar format untouched. When I ran the macro again it was no longer
discertionary. It changed them all. So, maybe it will help to see the
code:

Dim oRng As Range
Set oRng = ActiveDocument.Content
With oRng.Find
.ClearFormatting
.Text = "\([^034^0147]<*>[^034^0148]\)"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
Do While .Execute
With oRng
.MoveEnd Unit:=wdCharacter, Count:=-2
.MoveStart Unit:=wdCharacter, Count:=2
.Font.Underline = True
.Collapse wdCollapseEnd
End With
Loop
End With
End Sub

I like this code but it runs rampant. I want to put a leash on this by
controlling what it changes. So, I want to select a portion of the
document,
then run the macro and have the macro only underline the instances
contained
in the selected text only, not run amok.

Thanks for your help.




Gordon Bentley-Mix said:
jerem,

If I understand your questions correctly, then I believe what you are
looking for is Selection.Range.
--
Cheers!

Gordon Bentley-Mix
Word MVP

Uninvited email contact will be marked as SPAM and ignored. Please post
all
follow-ups to the newsgroup.
 
J

jerem

Did the trick, thanks for your help.

Tony Jollans said:
When you do repeated Finds in this way, Word does not remember the original
Range. It is, therefore, down to you to remember it and terminate the loop
as soon as it goes outside that Range. This is one way:

Dim oRng As Range, oRng2 As Range
Set oRng = Selection.Range 'ActiveDocument.Content
Set oRng2 = oRng.Duplicate
With oRng.Find
.ClearFormatting
.Text = "\([^034^0147]<*>[^034^0148]\)"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
Do While .Execute
If Not oRng.InRange(oRng2) Then Exit Do
With oRng
.MoveEnd Unit:=wdCharacter, Count:=-2
.MoveStart Unit:=wdCharacter, Count:=2
.Font.Underline = True
.Collapse wdCollapseEnd
End With
Loop
End With

--
Enjoy,
Tony

www.WordArticles.com

jerem said:
Ooops - I must have spoken too soon. I changed Set oRng =
ActiveDocument.Content to Set oRng = Selection.Range and it worked once -
that is, it only underlined those I wanted underlined and left those with
a
similar format untouched. When I ran the macro again it was no longer
discertionary. It changed them all. So, maybe it will help to see the
code:

Dim oRng As Range
Set oRng = ActiveDocument.Content
With oRng.Find
.ClearFormatting
.Text = "\([^034^0147]<*>[^034^0148]\)"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
Do While .Execute
With oRng
.MoveEnd Unit:=wdCharacter, Count:=-2
.MoveStart Unit:=wdCharacter, Count:=2
.Font.Underline = True
.Collapse wdCollapseEnd
End With
Loop
End With
End Sub

I like this code but it runs rampant. I want to put a leash on this by
controlling what it changes. So, I want to select a portion of the
document,
then run the macro and have the macro only underline the instances
contained
in the selected text only, not run amok.

Thanks for your help.




Gordon Bentley-Mix said:
jerem,

If I understand your questions correctly, then I believe what you are
looking for is Selection.Range.
--
Cheers!

Gordon Bentley-Mix
Word MVP

Uninvited email contact will be marked as SPAM and ignored. Please post
all
follow-ups to the newsgroup.


:

Can anyone tell me how to set the oRng below

Dim oRng As Range
Set oRng = ActiveDocument.Content

to just the selected portion (that is, a portion that I've manually
selected) of the active document rather than having the entire document
designated as the range (if that's what Content means). So, I want to
replace .Content with whatever keyword means "just the user's selected
portion of the document". Went through the entire intellisense list to
try
to find that, but didn't see anything there usable.

Would be nice to have a VBA dictionary out on the web - sort of like
webster's online dictionary.

Thanks for your help.
 

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