formatting a selection of text within a document

T

Tony Logan

I'm trying to write a macro that selects the text between two headings, so that I can do some formatting to the text that appears between those headings, but nowhere else in the document

Here's an example

Paragraph
Another paragraph
Heading 1
A paragraph
Another paragraph
Heading 2
Yet another paragraph
Final paragraph

I want to be able to select everything between Heading 1 and Heading 2 and make some changes to what's there, but I don't want to make the changes anywhere else in the document

Thanks.
 
A

Anne Troy

So, Tony... would Heading1 and Heading2 be instances of level 1 headings? Or
are they actual text that we can define? Try recording a macro like this:

Ctrl+F (find either a heading 1 style or the text you're looking for), hit
Find next.
Cancel.
Hit the right arrow key to take focus off it.
Double-click the EXT button in the status bar at the bottom of the screen.
Ctrl+F (find either a heading 1 or 2 style or the text your'e looking for--I
can't make out what you're doing), hit Find next.
Cancel.
Shift+Home
Do your formatting.
Stop recording.
<-*-><-*-><-*-><-*-><-*-><-*-><-*-><-*->
Hope this helps!
Anne Troy (better known as Dreamboat)
Author: Dreamboat on Word
Email: Dreamboat*at*Piersontech.com
Web: www.TheOfficeExperts.com
<-*-><-*-><-*-><-*-><-*-><-*-><-*-><-*->
Tony Logan said:
I'm trying to write a macro that selects the text between two headings, so
that I can do some formatting to the text that appears between those
headings, but nowhere else in the document.
Here's an example:

Paragraph.
Another paragraph.
Heading 1.
A paragraph.
Another paragraph.
Heading 2.
Yet another paragraph.
Final paragraph.

I want to be able to select everything between Heading 1 and Heading 2 and
make some changes to what's there, but I don't want to make the changes
anywhere else in the document.
 
K

Klaus Linke

Hi Tony,

"Heading 1" and "Heading 2" are the same level (say, "Heading 1" style)?
Then use "Edit > GoTo" and type in "\HeadingLevel".
If you record that, you get the code for selecting the current
(sub-)chapter.
It's enough that the cursor (selection) is currently somewhere in the
(sub-)chapter, and the built-in bookmark = the (sub-)chapter will be
selected. If you want to make sure to get a whole chapter and not only a
subchapter, you'd better be in a "Heading 1" paragraph first.

Greetings,
Klaus


Tony Logan said:
I'm trying to write a macro that selects the text between two headings,
so that I can do some formatting to the text that appears between those
headings, but nowhere else in the document.

Here's an example:

Paragraph.
Another paragraph.
Heading 1.
A paragraph.
Another paragraph.
Heading 2.
Yet another paragraph.
Final paragraph.

I want to be able to select everything between Heading 1 and Heading 2 and
make some changes to what's there, but I don't want to make the changes
anywhere else in the document.
 
B

Bruce Brown

Tony,

I'm taking you at your word that it's text between Heading 1 and
Heading 2 that you want to format, regardless of whether or not
chapters or subchapters are involved, as Klaus is hypothecating.

Dim R As Range, X As Range
On Error GoTo OuttaHere
Set R = Selection.Range
With R.Find
.Style = "Heading 1"
.Execute
If .Found Then
R.Collapse wdCollapseEnd
Set X = R.Duplicate
End If
End With
With R.Find
.Style = "Heading 2"
.Execute
If .Found Then
R.Collapse
X.End = R.End
End If
End With
X.Select
OuttaHere:

- Bruce
 
K

Klaus Linke

Hi Bruce,

Thanks! I misread the question...

All text *should* best belong to a proper subchapter.
Else -- especially if you have numbering, or styleref fields in the headers --, documents quickly become nightmares.

A nice layout gets difficult to achieve, too, if you don't follow a strict hierarchy.
Take a book where the author puts the text after "Heading 2" styles in some cases, and after "Heading 3/4/5" styles in other cases,
and you'll end up with a messy look, a messy TOC, and lots of manual formatting.

Regards,
Klaus
 

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