PDA

View Full Version : Inline (run-in) heading formatting using a style separator



awan2471
11-01-2014, 06:52 AM
Hi all,

Many thanks in advance on any tips or assistance on how to get started! A VBA newbie here!

I've been struggling with this one for a while, but haven't been able to fine any sample code to get started. Most sources recommend a manual insert of the style separator via Ctrl+Alt+Enter which is extremely tedious for large documents.

Here's the project: I'd like to generate a TOC from inline/run-in headings, but don't want to use linked styles or TC fields. Thus, I'd like to have properly marked paragraphs corresponding to the correct heading style (which are linked up to respective multi-level list levels) with a style separator between the heading text and the paragraph text. The big issue with doing this manually is that insertion of the style separator will mess up the visible numbering of the current list unless you have a second paragraph that is already in list paragraph form. The default for doing this process manually would be to place a new paragraph mark after the first sentence, restyle the second paragraph to list paragraph form since it currently picks up the format from the previous style, then replace cursor on the first paragraph and finally insert the style separator via keyboard shortcut.

The macro would do this:
Remove all current style separators
For each paragraph of header level 1-n
create a second paragraph of list paragraph style starting from the period of the first sentence of each header

insert a style separator at end of first paragraph which would automatically bring the second paragraph up to the first
next

e.g.
1. Heading 1 text[style separator inserted]. This is the list paragraph text that will no longer be picked up by TOC.
2. Heading text only[style separator inserted].
2.1 Sub-heading text[style separator inserted]. More text here.

gmayor
11-01-2014, 08:26 AM
What you propose is possible, and the work has been done for you in my Table of Contents add-in (http://www.gmayor.com/table_of_contents_addin.html).

The code to insert the separator is simple enough, but you will need to create the style for the Sub-heading text here called by oStyle.


With Selection
.InsertBefore vbCr
.InsertStyleSeparator
.Paragraphs(1).Range.Font.Reset
.Paragraphs(1).Style = oStyle
End With

RoyLasris
08-18-2016, 02:46 AM
Once I have applied a second style to a paragraph, I sometimes need to examine a paragraph to determine the name of the second style in a paragraph created with style separators.

Stylename = Selection.Paragraphs(1).Style returns the initial style name associated with the paragraph, but I need the second. Is there a way to 'loop' through the various styles in a paragraph.

gmayor
08-18-2016, 03:51 AM
If you are referring to the TOC tool I mentioned in my last message then the following will give you the styles at the start and end of the split paragraph


Dim orng As Range
Dim strStyle As String
Set orng = Selection.Paragraphs(1).Range
orng.start = orng.Paragraphs(1).Range.start
orng.Collapse 1
strStyle = orng.Style
orng.End = orng.Paragraphs(1).Range.End - 1
orng.Collapse 0
MsgBox strStyle & vbCr & orng.Style

RoyLasris
08-19-2016, 04:06 AM
Thanks. That does it!

Roy