Re: One more question
- From: JAnderson <JAnderson@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 18 Dec 2006 08:46:00 -0800
That is an excellent tutorial on styles; thank you! For my personal use, I
think I'll start using these.
I tried it and it worked perfectly. Part of my challenge, however, is
creating a file for other users. These users are able to run macros, but
applying styles might be a bit much.
Is there some way I can use the macro Greg gave me to insert a paragraph
break instead of a page break? This is what he gave me:
Sub Scratchmacro()
Dim oRng As Word.Range
Dim oRngDup As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "Smith"
While .Execute
Set oRngDup = oRng.Duplicate
oRngDup.Collapse wdCollapseStart
oRngDup.InsertBreak Type:=wdPageBreak
oRng.Collapse wdCollapseEnd
Wend
End With
End Sub
Isn't there a property that would allow me to insert paragraph breaks
instead of page breaks?
Thanks!
"Shauna Kelly" wrote:
Hi JAnderson.
Today is the day you learn about styles!
Make a backup of your document before you do all this, in case of accidents.
Create a paragraph style and name it Smith (or something related to the
magic word whose paragraph you always want to be at the top of a page).
Modify this new style's paragraph formatting so it has page break before
and, just for the moment, modify its font to give it a funny colour (bright
green, pink, whatever). The funny colour will help you see what's going on.
(See How to modify styles in Microsoft Word
http://www.ShaunaKelly.com/word/styles/ModifyAStyle.html)
Use Find and replace as follows:
- Find What = your magic word
- Replace with = your magic word
- Click More
- Click in the Replace with box, then click Format > Style and click on your
style name.
- Click Replace all.
Bingo. All paragraphs containing your magic word are in your funny colour
and are at the top of a page.
Now, delete all the manual page breaks. To do that, use Find and Replace
again:
- Click in Find What. Click Special and choose Manual Page Break.
- Leave Replace With empty.
- Click Replace all.
All OK? Then modify your style again to put the colour back to Automatic.
Hope this helps.
Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
"JAnderson" <JAnderson@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:BE78DA73-8A63-4469-8007-AE6F5AEE6687@xxxxxxxxxxxxxxxx
So this does exactly what I asked, but sometimes my text in the original
document appears in the middle of the page, with several spaces before it.
I
want to retain this position, and the only way I can do that is if I
insert a
page break in the line BEFORE my text.
I can't insert the blank text into the macro itself because the number of
spaces is variable. Basically, I want to know if it's possible to have
the
macro find my text, then insert a break at the beginning of the line or on
the previous line (which is always blank in my document).
Thanks!!
"JAnderson" wrote:
Um, wow- that was perfect. My document has about 300 instances of this
particular word, so I like this macro. Could a style do the same thing
with
such a large document?
I'm a bit of a novice with VB, but I have to admit I have NO idea why/how
this works, but it's perfect.
Thank you!
"Greg Maxey" wrote:
A more conventional approach might be to define a style with page break
before and apply that style to the text where appropriate.
However, to answer your question this might work:
Sub Scratchmacro()
Dim oRng As Word.Range
Dim oRngDup As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "Smith"
While .Execute
Set oRngDup = oRng.Duplicate
oRngDup.Collapse wdCollapseStart
oRngDup.InsertBreak Type:=wdPageBreak
oRng.Collapse wdCollapseEnd
Wend
End With
End Sub
--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
JAnderson wrote:
Greetings,
I'm trying to insert multiple page breaks in a very large document.
Essentially, every time a certain word appears (ex: "Smith"), I want
Word to insert a page break.
This should be easy because my document has a lot of areas which
should be at the top of a page (my company's name), and every single
time that name appears, I want a page break in front of it.
Is there a way to write a macro to insert a page break before a
particular word or phrase?
- Follow-Ups:
- Re: One more question
- From: Graham Mayor
- Re: One more question
- References:
- Re: Simple macro question - multiple page breaks
- From: Greg Maxey
- One more question
- From: JAnderson
- Re: One more question
- From: Shauna Kelly
- Re: Simple macro question - multiple page breaks
- Prev by Date: Re: Help with Insertdatabase command
- Next by Date: Re: Help with Insertdatabase command
- Previous by thread: Re: One more question
- Next by thread: Re: One more question
- Index(es):
Relevant Pages
|