keep lines together

R

Rick

I'm writing a module in my macro that needs to keep all lines of a
paragraph together. This module needs to execute after
Selection.TypeText types out the paragraph. My understanding is that
the way you keep lines together is by using Selection.KeepTogether
after the paragraph is selected. However, the number of lines in the
paragraph may vary depending on the context, so I'm not sure how to
select my entire paragraph after Selection.TypeText has finished
typing it out. Thanks for any help.
 
J

Jay Freedman

Rick said:
I'm writing a module in my macro that needs to keep all lines of a
paragraph together. This module needs to execute after
Selection.TypeText types out the paragraph. My understanding is that
the way you keep lines together is by using Selection.KeepTogether
after the paragraph is selected. However, the number of lines in the
paragraph may vary depending on the context, so I'm not sure how to
select my entire paragraph after Selection.TypeText has finished
typing it out. Thanks for any help.

Your understanding is not quite right. There is no such property as
Selection.KeepTogether, and it doesn't apply to any specific number of
lines.

KeepTogether is a property of an entire paragraph (or of a paragraph style).
If less than an entire paragraph is selected, setting

Selection.ParagraphFormat.KeepTogether = True

will still apply the property to the entire paragraph that contains the
Selection. If (all or part of) more than one paragraph is selected, the
property applies to all of the affected paragraphs.

Further, instead of Selection.TypeText you would be better off assigning the
new string to Selection.Text,

Selection.Text = "This is my inserted text."

which will leave the entire inserted text selected, and then you can apply
whatever (paragraph or character) formatting you need to the whole thing at
once. Also, it would be a good idea to apply styles rather than direct
formatting to the text, so it could be easily modified later.

Even better, learn to use Range objects so you don't have to move the
Selection around. Your code will run faster, with fewer side effects and
without scrolling the window.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
R

Rick

Thanks much but I'm still a bit unclear. The macro just needs to type
out a paragraph of text at the insertion point -- usually around
300-400 characters that ends up being a paragraph of about four or
five lines -- and then ensure that paragraph stays together on the
page. What is my best route to doing this?
 
J

Jay Freedman

As long as the added material is all in one paragraph, and the Selection is
anywhere in that paragraph after the TypeText is completed, you just need to
put Selection.ParagraphFormat.KeepTogether = True as the next statement. You
don't have to extend the selection back to the beginning of the inserted
text.
 

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