Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Max Moor
 
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
 
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
MailMerge.OpenDataSource not able to access a dynamic range name ? Bruce Cooley Mailmerge 2 April 17th 05 10:28 AM
Adding Border to an object moves the caption!?? bandy2000 Microsoft Word Help 1 March 27th 05 04:21 PM
Conditional inclusion of WordArt? Bill Watkins Mailmerge 13 February 1st 05 03:51 PM
MS Word Picture Object tj Microsoft Word Help 0 January 26th 05 03:09 AM
Find and Replace anomaly BruceM Microsoft Word Help 7 January 18th 05 05:47 PM


All times are GMT +1. The time now is 06:26 AM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"