How to create style to automatically suppress CR after a Heading?

M

MarkLTaper

I am trying to create a heading style so that following text begins on the
same line, but I am baffled. I see no way to suppress the carriage return.
Jay Freedman's response to my previous question showed how to manually
format to create the proper effect on a case by case basis not how to set up
a style.
I use this format frequently so I would like to have it automatic, if
possible.

Thanks MLT

I want:

Heading 4: Following text

not

Heading 4:
Following text
 
J

Jay Freedman

OK, I see what you're trying to do. It will be possible with a macro
-- more likely, a series of macros -- that do all the formatting. You
can assign each macro to a toolbar button, a shortcut key, or both.

An example macro is shown below. This is specific for the situation
you showed; if you want other heading styles, or other assumptions
about the starting selection, you'd need other macros for those.

This example assumes that you've already typed the text of both the
heading and at least part of the following text, all in the regular
body style in a single paragraph, and that you've selected the part
you want to make into a Heading 4. It doesn't work right if you select
an entire paragraph.

Sub ApplyRunin4()
' the selected text will take Heading 4 style
' with a hidden paragraph mark at the end

If Selection.Type <> wdSelectionNormal Then
MsgBox "Please select the heading text first."
Exit Sub
End If

If Selection.Start <> Selection.Paragraphs(1).Range.Start Then
MsgBox "Please select text at the beginning of the paragraph."
Exit Sub
End If

With Selection
.InsertParagraphAfter
.Collapse wdCollapseStart
.Style = ActiveDocument.Styles("Heading 4")
.Paragraphs(1).Range.Select
.Collapse wdCollapseEnd
.MoveStart wdCharacter, -1
.Font.Hidden = True
End With
End Sub

For help with installing the macro and assigning it to a button and/or
shortcut, see these articles:

http://www.gmayor.com/installing_macro.htm
http://www.word.mvps.org/FAQs/Customization/AsgnCmdOrMacroToToolbar.htm
http://www.word.mvps.org/FAQs/Customization/AsgnCmdOrMacroToHotkey.htm

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

MarkLTaper

Jay, This is great. I can see that macros open up a vast flexibility. I
will work on implimenting this macro. However, I am thinking that for my
purposes, a macro that searchs for every level 4 heading goes to its end and
hides the CR would be most effective. Are their subtle enough search
functions in the macro language to make this approach work? If you think so
then I will try to work this out.

Thanks again
 
J

Jay Freedman

Hi Mark,

Ask and ye shall receive....

Sub RuninAllHeading4()
Dim SearchRg As Range
Dim HideRg As Range
Set SearchRg = ActiveDocument.Range
With SearchRg.Find
.ClearFormatting
.Text = ""
.Style = wdStyleHeading4
.Format = True
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = False
Do While .Execute
Set HideRg = SearchRg.Duplicate
With HideRg
.Collapse wdCollapseEnd
.MoveStartWhile cset:=Chr(13), Count:=wdBackward
.Font.Hidden = True
End With

With SearchRg
.Collapse wdCollapseEnd
.MoveEnd unit:=wdCharacter, Count:=1
.Select
If (.Text <> " ") And (.Text <> Chr$(9)) Then
.InsertBefore " "
End If
.Collapse wdCollapseEnd
End With
Loop
End With
End Sub

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

MarkLTaper

Well I didn't want to ask because that would have been greedy. This is
perfect, and I doubt I could have constructed it myself.

Thanks MarkLTaper
 
J

Jay Freedman

You're welcome.

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

Dan Hart

Is a macro still the only option for creating a heading with no CR/LF? Any
updates on this answer?
 
D

DeanH

There is no way of creating a Heading with out a paragraph mark because these
are by definition a paragraph style BUT you can by using the Style Separator
to combine the Heading and the following text. And only the "Heading" text
will show in the Table of Contents if that is something that you require.

Alt+Ctrl+Return or InsertStyleSeparator icon in the Commands listing.

Hope this helps
DeanH
 

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