Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / March 2008

Tip: Looking for answers? Try searching our database.

How I can select all data between two headers ?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jim - 26 Feb 2008 14:02 GMT
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!
Stefan Blom - 27 Feb 2008 10:57 GMT
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.

Signature

Stefan Blom
Microsoft Word MVP

> Hi All,
>
[quoted text clipped - 6 lines]
>
> Thanks!
fumei - 27 Feb 2008 20:43 GMT
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.

>Hi All,
>
[quoted text clipped - 6 lines]
>
>Thanks!
Tony Strazzeri - 24 Mar 2008 03:05 GMT
> Hi All,
>
[quoted text clipped - 6 lines]
>
> 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

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.