Reply
 
Thread Tools Display Modes
  #1   Report Post  
Max Moor
 
Posts: n/a
Default Can Range.Find jump its initial boundaries???

Hi All,
Well, my bravery level has gotten to the point that I'm coding. Of
course, I'm also debugging. I hope you can bear with me a bit...

The point of the function below is to cycle through every section in
my document. For the ones that start out with a Header 1 style someplace,
I do some further processing. (This skips stuff like the cover pages and
TOC.)

If the Heder 1 is found, I select everything up to the first Header 2
style, intending to copy it to a new file. Next, I search through for all
the Header 2 "sections," selecting them in turn, also to be copied to new
files.

For the most part, it does what I expect, until the end of the first
section with headers. This section has a Header 1 section, and two Header
2 sections. The code selects the Header 1 area correctly, then each of the
two Header 2 areas. Then, the .Execute for the Header 2 runs a third time!

I've checked, and before the Heading 2 find loop runs, its range is
positions 7656 to 21024 (encompassing the current section). On its third
iteration, rngH2.End = 21790. It appears that Find has extended its own
range. I was expecting that it would stay within the "hidden" initial find
range, but it's not seeming to. Can it do that??? Can I make it stop?

As an aside, is there a way I can put the cusor in the document, and
see the character position?

Thanks for the help, Max




Here is the code. It's not overly pretty just now, but...



Sub SplitDoc()

Dim sctCurrent As Word.Section
Dim rngSection As Word.Range
Dim rngH1 As Word.Range
Dim rngH2 As Word.Range
Dim rngSelect As Word.Range
Dim lngSelectEnd As Long
Dim lngEOS As Long



' Cycle through all document sections
For Each sctCurrent In ActiveDocument.Sections

lngEOS = sctCurrent.Range.End
Set rngH1 = sctCurrent.Range
Set rngH2 = sctCurrent.Range

' Find Heading 1 in current section
With rngH1.Find
.Format = True
.Style = ActiveDocument.Styles("Heading 1")
.Execute
End With

' If Heading 1 was found, process the section
If (rngH1.Find.Found) Then

' Select H1 region
lngSelectEnd = GetNextH2OrEOS(rngH1.End, lngEOS)
Set rngSelect = ActiveDocument.Range(Start:=rngH1.Start, End:
=lngSelectEnd)
rngSelect.Select

' Do some stuff with H1 selection

' While Heading 2's are found...
With rngH2.Find
.Format = True
.Style = ActiveDocument.Styles("Heading 2")
Do While .Execute

' Select H2 region
lngSelectEnd = GetNextH2OrEOS(rngH2.End, lngEOS)
Set rngSelect = ActiveDocument.Range(Start:
=rngH2.Start, End:=lngSelectEnd)
rngSelect.Select

' Do some stuff with H2 section

Loop ' While rngH2.Find.Execute
End With ' rngH2.Find
End If ' H1 found
Next sctCurrent ' For
End Sub



Function GetNextH2OrEOS(lngStart As Long, lngEOS As Long) As Long

Dim rngSearch As Word.Range


If (lngStart lngEOS) Then
MsgBox "Range out of bounds in 'GetNextH2OrEOS': S:= " & lngStart &
" E:= " & lngEOS

Else
' Set the search range
Set rngSearch = ActiveDocument.Range(Start:=lngStart, End:=lngEOS)

' Find Heading 2 in current section
With rngSearch.Find
.Format = True
.Style = ActiveDocument.Styles("Heading 2")
.Execute
End With

' If Heading 2 was found, return its start
If (rngSearch.Find.Found) Then
GetNextH2OrEOS = rngSearch.Start

' Else, return EOS
Else
GetNextH2OrEOS = lngEOS
End If
End If
End Function
  #2   Report Post  
Word Heretic
 
Posts: n/a
Default

G'day Max Moor ,

to put the cursor at a point in the document, simply MyRange.Select on
a Range object. You are already doing this - but nothing else.

I cant see where you 'shrink' mgh2, and also you might want .Wrap =
wdwrapnone



Steve Hudson - Word Heretic

steve from wordheretic.com (Email replies require payment)
Without prejudice


Max Moor reckoned:

Hi All,
Well, my bravery level has gotten to the point that I'm coding. Of
course, I'm also debugging. I hope you can bear with me a bit...

The point of the function below is to cycle through every section in
my document. For the ones that start out with a Header 1 style someplace,
I do some further processing. (This skips stuff like the cover pages and
TOC.)

If the Heder 1 is found, I select everything up to the first Header 2
style, intending to copy it to a new file. Next, I search through for all
the Header 2 "sections," selecting them in turn, also to be copied to new
files.

For the most part, it does what I expect, until the end of the first
section with headers. This section has a Header 1 section, and two Header
2 sections. The code selects the Header 1 area correctly, then each of the
two Header 2 areas. Then, the .Execute for the Header 2 runs a third time!

I've checked, and before the Heading 2 find loop runs, its range is
positions 7656 to 21024 (encompassing the current section). On its third
iteration, rngH2.End = 21790. It appears that Find has extended its own
range. I was expecting that it would stay within the "hidden" initial find
range, but it's not seeming to. Can it do that??? Can I make it stop?

As an aside, is there a way I can put the cusor in the document, and
see the character position?

Thanks for the help, Max




Here is the code. It's not overly pretty just now, but...



Sub SplitDoc()

Dim sctCurrent As Word.Section
Dim rngSection As Word.Range
Dim rngH1 As Word.Range
Dim rngH2 As Word.Range
Dim rngSelect As Word.Range
Dim lngSelectEnd As Long
Dim lngEOS As Long



' Cycle through all document sections
For Each sctCurrent In ActiveDocument.Sections

lngEOS = sctCurrent.Range.End
Set rngH1 = sctCurrent.Range
Set rngH2 = sctCurrent.Range

' Find Heading 1 in current section
With rngH1.Find
.Format = True
.Style = ActiveDocument.Styles("Heading 1")
.Execute
End With

' If Heading 1 was found, process the section
If (rngH1.Find.Found) Then

' Select H1 region
lngSelectEnd = GetNextH2OrEOS(rngH1.End, lngEOS)
Set rngSelect = ActiveDocument.Range(Start:=rngH1.Start, End:
=lngSelectEnd)
rngSelect.Select

' Do some stuff with H1 selection

' While Heading 2's are found...
With rngH2.Find
.Format = True
.Style = ActiveDocument.Styles("Heading 2")
Do While .Execute

' Select H2 region
lngSelectEnd = GetNextH2OrEOS(rngH2.End, lngEOS)
Set rngSelect = ActiveDocument.Range(Start:
=rngH2.Start, End:=lngSelectEnd)
rngSelect.Select

' Do some stuff with H2 section

Loop ' While rngH2.Find.Execute
End With ' rngH2.Find
End If ' H1 found
Next sctCurrent ' For
End Sub



Function GetNextH2OrEOS(lngStart As Long, lngEOS As Long) As Long

Dim rngSearch As Word.Range


If (lngStart lngEOS) Then
MsgBox "Range out of bounds in 'GetNextH2OrEOS': S:= " & lngStart &
" E:= " & lngEOS

Else
' Set the search range
Set rngSearch = ActiveDocument.Range(Start:=lngStart, End:=lngEOS)

' Find Heading 2 in current section
With rngSearch.Find
.Format = True
.Style = ActiveDocument.Styles("Heading 2")
.Execute
End With

' If Heading 2 was found, return its start
If (rngSearch.Find.Found) Then
GetNextH2OrEOS = rngSearch.Start

' Else, return EOS
Else
GetNextH2OrEOS = lngEOS
End If
End If
End Function


Reply
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
How do I export autocorrect files to a jump drive? Susie Microsoft Word Help 4 April 12th 05 11:53 PM
Remembering initial caps set in a document markymids New Users 9 March 20th 05 11:49 PM
Text Boundaries in Print Layout Brad New Users 4 February 12th 05 09:27 PM
Initial Paragraph rickp3131 Page Layout 3 January 25th 05 02:32 PM


All times are GMT +1. The time now is 08:07 PM.

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"