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  

Can Range.Find jump its initial boundaries???



 
 
Thread Tools Display Modes
  #1  
Old May 17th, 2005, 07:22 PM
Max Moor
external usenet poster
 
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  
Old May 21st, 2005, 01:51 AM
Word Heretic
external usenet poster
 
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


 




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
Line Jump not working Dennis Visio 0 May 18th, 2005 01:40 PM
How do I export autocorrect files to a jump drive? Susie General Discussion 4 April 12th, 2005 11:53 PM
Initial Paragraph rickp3131 Page Layout 3 January 25th, 2005 02:32 PM
Jump from Blank screen to slide N during presentation Yohs Powerpoint 3 December 30th, 2004 07:23 PM
Identifying Upper case or identifying a single initial Peo Sjoblom Worksheet Functions 1 January 23rd, 2004 07:41 PM


All times are GMT +1. The time now is 09:28 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.