How I can select all data between two headers ?

J

Jim

Hi All,

Does anyone know is it possible to select all data between two
header ? I can find header # 1 by running followed code:

Selection.Find.ClearFormatting
Selection.GoTo what:=wdGoToHeading, which:=wdGoToAbsolute, Count:=1,
Name:=""

Thanks!
 
S

Stefan Blom

What are you trying to accomplish? Why do you need to select the text?

If you explained what you are trying to do, someone might be able to suggest
an alternative, and potentially better, approach.

--
Stefan Blom
Microsoft Word MVP


in message
news:11f4b2fc-d8ef-4de1-8233-d12fbc9e6e50@t66g2000hsf.googlegroups.com...
 
F

fumei via OfficeKB.com

I agree. Please describe precisely what you are trying to do. First of all,
"all data between two header " is not accurate, at least in respect to your
code.

Header is a very specific term in Word.

I believe you mean heading, not header.

Not only that, but wdGoToHeading will only go to a paragraph that has a
Heading style attached to it. If you are using a different style for your
headings, then it will not work.

In any case, yes, certainly, it can be done. You can select - and do you
REALLY mean Select???? - the text between, well, anything, including headings.
Here are some hints.

1. use Range, not Selection.
2. use a loop on the Execute of the Range.Find
3. use a Collapse on the Found of the range.Find
4. use two variables to set the values of the END of the Found, and the
START of the next
5. grab the range - between the headings - using those values. I.e. the END
of the Found heading, and the START of the next one.

Voila, the text between headings.
 
T

Tony Strazzeri

Hi All,

Does anyone know is it possible to select all data between two
header ? I can find header # 1 by running followed code:

Selection.Find.ClearFormatting
Selection.GoTo what:=wdGoToHeading, which:=wdGoToAbsolute, Count:=1,
Name:=""

Thanks!

Assuming you mean between paragaphs formatted with the same paragraph
style eg "Heading 1"
The code below will do it.


Note: I assume by header you mean some sort of heading in the text. I
further assume the heading is formatted using a paragraph style called
"Heading 1". In the word context header can also refer to the
paragraph style called "Header" but usually refers to the text that
appears at the top of each page as distinct from the the "Footer" also
a defalut word style but also meaning the bit that appears at the
bottom of every page, ad defined using "View Header/Footer".

I hope this is more helpfule to get you thinking.

Cheers!
TonyS.



Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Heading 1")
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute

'moves the selection off the found heading 1 style
'towards the end of the document
Selection.EndKey Unit:=wdLine
Selection.MoveRight Unit:=wdCharacter, Count:=1


' will extend the selection from here to wherever
' the next find takes us
Selection.Extend

'find the neaxt heading
Selection.Find.Execute


'move the end of the selection to before the start of this
selection/heading style
' Selection.HomeKey Unit:=wdLine, Extend:=True
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=True
Selection.HomeKey Unit:=wdLine, Extend:=True

Selection.ExtendMode = False

X = Selection.Text

'set up to before the previous find in case we want to recurse
this operation
Selection.Collapse wdCollapseEnd
 

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