A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Word » Formatting Long Documents
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Question about using Find with a Range object



 
 
Thread Tools Display Modes
  #1  
Old May 15th, 2005, 08:06 AM
Max Moor
external usenet poster
 
Posts: n/a
Default Question about using Find with a Range object

Hi All,
I'm trying to learn enough about Range objects and Find (et. al.)
to do some reformatting of a long document. I got the code below from
Jay, and it works like a charm, but I don't grasp how entirely.

I understand that if I use Find on a Range object, the range
object is redefined if the Find is successful. In the code below, the
oRg range is originally set to the whole document. When the Find
executes, it appears that the oRg Start and End are redefined to bound
just the reference field found.

I expected that only the start point of the range would be
redefined, but oIns is a duplicate of it. When oIns is collapsed to its
end, it's the end of the field found, not the end of the document.

So... if oRg has been redefined to bound only the found field,
how does Find get beyond that range to find the next matching field,
later in the document, on the next loop???

Also, given the code below, how can I access the text of the field
that is found? The variable strCode is set to the reference code
itself, with brackets and big numbers. I want to get the text that is
actually visible in the document. In my case, it's the name of a
header. Ho do I get to that?

Thanks, Max




Sub AddPageNumberRefs()
Dim oRg As Range
Dim oIns As Range
Dim strCode As String
Dim fldPg As Field

Set oRg = ActiveDocument.Range
oRg.TextRetrievalMode.IncludeFieldCodes = True
With oRg.Find
.Forward = True
.Format = True
.Style = ActiveDocument.Styles("Cross-reference Char")
.Text = "^d REF"
Do While .Execute
strCode = oRg.Fields(1).Code.Text
Set oIns = oRg.Duplicate
With oIns
.Collapse wdCollapseEnd
.Text = ", pg "
.Collapse wdCollapseEnd
Set fldPg = ActiveDocument.Fields.Add(Range:=oIns, Type:
=wdFieldEmpty)
fldPg.Code.Text = " PAGE" & LTrim(strCode)
fldPg.Update
End With
Loop
End With
End Sub
  #2  
Old May 15th, 2005, 08:20 AM
Shauna Kelly
external usenet poster
 
Posts: n/a
Default

Hi Max

When the Find
executes, it appears that the oRg Start and End are redefined to bound
just the reference field found.


Yes.

I expected that only the start point of the range would be
redefined, but oIns is a duplicate of it.


The line
Set oIns = oRg.Duplicate
duplicates the oRg range. It doesn't duplicate the content of the range
within the document. It creates another range object (olns) that points to
the same part of the content of the document that oRg points to. It is
necessary to do this for this code, because you need to muck around with the
result of the .Find.

how can I access the text of the field that is found?

Change the line
strCode = oRg.Fields(1).Code.Text
to
strCode = oRg.Fields(1).Result.Text


Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word


"Max Moor" wrote in message
6...
Hi All,
I'm trying to learn enough about Range objects and Find (et. al.)
to do some reformatting of a long document. I got the code below from
Jay, and it works like a charm, but I don't grasp how entirely.

I understand that if I use Find on a Range object, the range
object is redefined if the Find is successful. In the code below, the
oRg range is originally set to the whole document. When the Find
executes, it appears that the oRg Start and End are redefined to bound
just the reference field found.

I expected that only the start point of the range would be
redefined, but oIns is a duplicate of it. When oIns is collapsed to its
end, it's the end of the field found, not the end of the document.

So... if oRg has been redefined to bound only the found field,
how does Find get beyond that range to find the next matching field,
later in the document, on the next loop???

Also, given the code below, how can I access the text of the field
that is found? The variable strCode is set to the reference code
itself, with brackets and big numbers. I want to get the text that is
actually visible in the document. In my case, it's the name of a
header. Ho do I get to that?

Thanks, Max




Sub AddPageNumberRefs()
Dim oRg As Range
Dim oIns As Range
Dim strCode As String
Dim fldPg As Field

Set oRg = ActiveDocument.Range
oRg.TextRetrievalMode.IncludeFieldCodes = True
With oRg.Find
.Forward = True
.Format = True
.Style = ActiveDocument.Styles("Cross-reference Char")
.Text = "^d REF"
Do While .Execute
strCode = oRg.Fields(1).Code.Text
Set oIns = oRg.Duplicate
With oIns
.Collapse wdCollapseEnd
.Text = ", pg "
.Collapse wdCollapseEnd
Set fldPg = ActiveDocument.Fields.Add(Range:=oIns, Type:
=wdFieldEmpty)
fldPg.Code.Text = " PAGE" & LTrim(strCode)
fldPg.Update
End With
Loop
End With
End Sub



  #3  
Old May 15th, 2005, 08:35 AM
Jezebel
external usenet poster
 
Posts: n/a
Default

The search range is effectively a hidden property of the Find object. The
reference is created when the Find object is instantiated (ie, either the
Selection or the Range of which the object is a property), and is not then
changed. So while the range referenced by oRg is redefined with each
iteration, the base range through which Find is progressing, is not.

The displayed text of a field is its Result. So you could use

oRg.Fields(1).Result


Seperately, as a matter of good programming practice, when you declare
object variables you should specify the library they belong to --

Dim oRg As Word.Range
Dim fldPg As Word.Field

It costs nothing to do this, and it will save you grief when you start
referencing other libraries (like Excel) in your projects. There are many
libraries that have Range and Field objects, and they are not
interchangeable.




"Max Moor" wrote in message
6...
Hi All,
I'm trying to learn enough about Range objects and Find (et. al.)
to do some reformatting of a long document. I got the code below from
Jay, and it works like a charm, but I don't grasp how entirely.

I understand that if I use Find on a Range object, the range
object is redefined if the Find is successful. In the code below, the
oRg range is originally set to the whole document. When the Find
executes, it appears that the oRg Start and End are redefined to bound
just the reference field found.

I expected that only the start point of the range would be
redefined, but oIns is a duplicate of it. When oIns is collapsed to its
end, it's the end of the field found, not the end of the document.

So... if oRg has been redefined to bound only the found field,
how does Find get beyond that range to find the next matching field,
later in the document, on the next loop???

Also, given the code below, how can I access the text of the field
that is found? The variable strCode is set to the reference code
itself, with brackets and big numbers. I want to get the text that is
actually visible in the document. In my case, it's the name of a
header. Ho do I get to that?

Thanks, Max




Sub AddPageNumberRefs()
Dim oRg As Range
Dim oIns As Range
Dim strCode As String
Dim fldPg As Field

Set oRg = ActiveDocument.Range
oRg.TextRetrievalMode.IncludeFieldCodes = True
With oRg.Find
.Forward = True
.Format = True
.Style = ActiveDocument.Styles("Cross-reference Char")
.Text = "^d REF"
Do While .Execute
strCode = oRg.Fields(1).Code.Text
Set oIns = oRg.Duplicate
With oIns
.Collapse wdCollapseEnd
.Text = ", pg "
.Collapse wdCollapseEnd
Set fldPg = ActiveDocument.Fields.Add(Range:=oIns, Type:
=wdFieldEmpty)
fldPg.Code.Text = " PAGE" & LTrim(strCode)
fldPg.Update
End With
Loop
End With
End Sub



  #4  
Old May 15th, 2005, 08:38 AM
Max Moor
external usenet poster
 
Posts: n/a
Default

"Shauna Kelly" wrote in
:

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word



It absolutely does! I've been working with Access in VB for quite
a while. I'm new to doing it with word. It's like starting over.

Could you tell me about how the Find executes the second (or
later) loop? Once Find has been successful, oRg bounds the found field,
and no longer encompasses the whole of the document.

I would think the second execution of Find would be only on oRg's
new range (the field previously found). How can it find any later
fields in the document, beyond its new, redefined range? (I hope this
question makes sense. It's really bugging me.)

- Max


Sub AddPageNumberRefs()
Dim oRg As Range
Dim oIns As Range
Dim strCode As String
Dim fldPg As Field

Set oRg = ActiveDocument.Range
oRg.TextRetrievalMode.IncludeFieldCodes = True
With oRg.Find
.Forward = True
.Format = True
.Style = ActiveDocument.Styles("Cross-reference Char")
.Text = "^d REF"
Do While .Execute
strCode = oRg.Fields(1).Code.Text
Set oIns = oRg.Duplicate
With oIns
.Collapse wdCollapseEnd
.Text = ", pg "
.Collapse wdCollapseEnd
Set fldPg = ActiveDocument.Fields.Add(Range:=oIns,
Type:
=wdFieldEmpty)
fldPg.Code.Text = " PAGE" & LTrim(strCode)
fldPg.Update
End With
Loop
End With
End Sub





  #5  
Old May 15th, 2005, 09:24 AM
Shauna Kelly
external usenet poster
 
Posts: n/a
Default

Hi Max

I think Jezebel has answered the question.

It's really bugging me.

As a Sunday evening brain teaser, create a new document, type =rand(3,2),
press Enter, and step through the following code watching what happens to
the document:

Sub Test1()

Dim oRange As Word.Range
Set oRange = ActiveDocument.Range

With oRange.Find
.Text = "brown"
.Wrap = wdFindStop
Do While .Execute
oRange.Bold = True
Loop
End With

With ActiveDocument.Range.Find
.Text = "brown"
.Wrap = wdFindStop
Do While .Execute
ActiveDocument.Range.Bold = True
Loop
End With

End Sub


Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word


"Max Moor" wrote in message
6...
"Shauna Kelly" wrote in
:

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word



It absolutely does! I've been working with Access in VB for quite
a while. I'm new to doing it with word. It's like starting over.

Could you tell me about how the Find executes the second (or
later) loop? Once Find has been successful, oRg bounds the found field,
and no longer encompasses the whole of the document.

I would think the second execution of Find would be only on oRg's
new range (the field previously found). How can it find any later
fields in the document, beyond its new, redefined range? (I hope this
question makes sense. It's really bugging me.)

- Max


Sub AddPageNumberRefs()
Dim oRg As Range
Dim oIns As Range
Dim strCode As String
Dim fldPg As Field

Set oRg = ActiveDocument.Range
oRg.TextRetrievalMode.IncludeFieldCodes = True
With oRg.Find
.Forward = True
.Format = True
.Style = ActiveDocument.Styles("Cross-reference Char")
.Text = "^d REF"
Do While .Execute
strCode = oRg.Fields(1).Code.Text
Set oIns = oRg.Duplicate
With oIns
.Collapse wdCollapseEnd
.Text = ", pg "
.Collapse wdCollapseEnd
Set fldPg = ActiveDocument.Fields.Add(Range:=oIns,
Type:
=wdFieldEmpty)
fldPg.Code.Text = " PAGE" & LTrim(strCode)
fldPg.Update
End With
Loop
End With
End Sub







  #6  
Old May 15th, 2005, 09:30 AM
Max Moor
external usenet poster
 
Posts: n/a
Default

"Jezebel" wrote in
:

The search range is effectively a hidden property of the Find object.
The reference is created when the Find object is instantiated (ie,
either the Selection or the Range of which the object is a property),
and is not then changed. So while the range referenced by oRg is
redefined with each iteration, the base range through which Find is
progressing, is not.

The displayed text of a field is its Result. So you could use

oRg.Fields(1).Result


Seperately, as a matter of good programming practice, when you declare
object variables you should specify the library they belong to --

Dim oRg As Word.Range
Dim fldPg As Word.Field

It costs nothing to do this, and it will save you grief when you start
referencing other libraries (like Excel) in your projects. There are
many libraries that have Range and Field objects, and they are not
interchangeable.


HA! No Jezebel, you!

Thank you, so much. I hate hidden things, but what you say makes
perfect sense.

I appreciate your note about referencing libraries, also. As I
mentioned, I've done fairly extensive programming in VB for MS Access.
(My foray into Word is to help me ultimately re-format my Access App's
user manual, written in Word, into a set of HTML files from which I can
make a .chm help file.) Specifying whether something is an ADO or DAO
object is a similar "good practice" in Access-land. Ah, why can't
everything be simple and non-ambiguous? :-)

Again, thanks to you, Shauna, and Jay for the help. I still have
a long way to go to get my ultimate macro written, but you've all gotten
me a fair bit closer.

Thanks, Max
  #7  
Old May 15th, 2005, 10:52 AM
Jezebel
external usenet poster
 
Posts: n/a
Default

I played around with this earlier today (before reading your post) in
exactly this way, but using 'dog' instead of 'brown' --- what does this say
about our respective psyches?





"Shauna Kelly" wrote in message
...
Hi Max

I think Jezebel has answered the question.

It's really bugging me.

As a Sunday evening brain teaser, create a new document, type =rand(3,2),
press Enter, and step through the following code watching what happens to
the document:

Sub Test1()

Dim oRange As Word.Range
Set oRange = ActiveDocument.Range

With oRange.Find
.Text = "brown"
.Wrap = wdFindStop
Do While .Execute
oRange.Bold = True
Loop
End With

With ActiveDocument.Range.Find
.Text = "brown"
.Wrap = wdFindStop
Do While .Execute
ActiveDocument.Range.Bold = True
Loop
End With

End Sub


Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word


"Max Moor" wrote in message
6...
"Shauna Kelly" wrote in
:

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word



It absolutely does! I've been working with Access in VB for quite
a while. I'm new to doing it with word. It's like starting over.

Could you tell me about how the Find executes the second (or
later) loop? Once Find has been successful, oRg bounds the found field,
and no longer encompasses the whole of the document.

I would think the second execution of Find would be only on oRg's
new range (the field previously found). How can it find any later
fields in the document, beyond its new, redefined range? (I hope this
question makes sense. It's really bugging me.)

- Max


Sub AddPageNumberRefs()
Dim oRg As Range
Dim oIns As Range
Dim strCode As String
Dim fldPg As Field

Set oRg = ActiveDocument.Range
oRg.TextRetrievalMode.IncludeFieldCodes = True
With oRg.Find
.Forward = True
.Format = True
.Style = ActiveDocument.Styles("Cross-reference Char")
.Text = "^d REF"
Do While .Execute
strCode = oRg.Fields(1).Code.Text
Set oIns = oRg.Duplicate
With oIns
.Collapse wdCollapseEnd
.Text = ", pg "
.Collapse wdCollapseEnd
Set fldPg = ActiveDocument.Fields.Add(Range:=oIns,
Type:
=wdFieldEmpty)
fldPg.Code.Text = " PAGE" & LTrim(strCode)
fldPg.Update
End With
Loop
End With
End Sub








  #8  
Old May 15th, 2005, 09:50 PM
Max Moor
external usenet poster
 
Posts: n/a
Default

"Shauna Kelly" wrote in news:
:

http://www.shaunakelly.com/word


Hi Shauna,
I tried your brain teaser, but nothing happened. Let me make sure I
understand...
I created a new document, and on the first line, typed "=rand(3,2)"
Then I pressed return. I opened the VB window, and pasted the code, then
ran it. It never enters either of the loops. Should the 'rand' line have
generated something? Do I have something turned off that would let it?

- Max
  #9  
Old May 15th, 2005, 10:34 PM
Jezebel
external usenet poster
 
Posts: n/a
Default

You need autocorrect turned on.


"Max Moor" wrote in message
. 16...
"Shauna Kelly" wrote in news:
:

http://www.shaunakelly.com/word


Hi Shauna,
I tried your brain teaser, but nothing happened. Let me make sure I
understand...
I created a new document, and on the first line, typed "=rand(3,2)"
Then I pressed return. I opened the VB window, and pasted the code, then
ran it. It never enters either of the loops. Should the 'rand' line have
generated something? Do I have something turned off that would let it?

- Max



  #10  
Old May 16th, 2005, 01:01 AM
Max Moor
external usenet poster
 
Posts: n/a
Default

"Jezebel" wrote in
:

You need autocorrect turned on.


Got it. I see what's happening. The bolding part makes sense to me. rand
is a new one though.

- Max
 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Survey Results SAm Running & Setting Up Queries 10 May 17th, 2005 08:32 PM
Where do I find the upside down, leading question mark for Spanis. Donnie General Discussion 4 January 25th, 2005 10:10 PM
find and replace ladyinred General Discussion 14 August 12th, 2004 09:35 PM
decipher log of scanpst.exe km General Discussion 0 July 18th, 2004 09:00 AM
Question about an argument, in an OFFSET dynamic range formula Terry B. Worksheet Functions 6 December 10th, 2003 10:53 PM


All times are GMT +1. The time now is 01:17 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.