#1
|
|||
|
|||
applying heading style within paragraph using macro
Hi all,
I have a macro that moves a freestanding heading to an in-line heading. At the end of the macro, I would like to apply a heading style--for the sake of example, let's say Heading 4. Example of finished text would be: Some example heading. Paragraph text begins on same line... I would like the macro to apply the Heading 4 style to the text "Some example heading" (not including the period). However, when I record this action with the macro recorder and then later try to run the macro, it seems that Word is unable to apply a heading style to a selected portion of text within a line. Is there a way to force this in the VBA code to work around Word's limitation with the macro recorder? |
#2
|
|||
|
|||
Look into the Style Separator. For heading styles to have their special attributes, they must be used as paragraph styles. The Style Separator allows you to do this but still have them appear as if in a paragraph with text in another style.
Why use Microsoft Word’s built-in heading styles? by Shauna Kelly Last edited by Charles Kenyon; 09-18-2017 at 08:11 AM. Reason: update link |
#3
|
|||
|
|||
Hi Charles,
I'm sorry to say that the Style Separator link you provided gave me a Trojan virus. Also, this does not answer my question. I have been working with styles for years and am quite comfortable with them. I am also aware of the benefits of using Word's built-in styles (indeed, I am doing that in this very document, particularly to make pdf bookmarking easier). Having said all that, my question from the OP stands. When I use Word's macro recorder to select a string of text and apply a built-in heading style, the resulting macro does not apply the heading style. I'm wondering if we can force this with some manual coding in the macro, but I'm not sure how. Any assistance would be appreciated--preferably that does not give me a Trojan. |
#4
|
|||
|
|||
Neither site contains any virus. These are old web pages and have been used safely for many years. What led you to believe that either was infected?
Do you understand the concept of the Style Separator? If not, you need to learn it. The macro recorder is often unable to reproduce things that you do. If your heading styles are "linked," attempting to apply the style to selected text is going to apply a poor-relation to that style, a quasi-character style, to the text. What, exactly, step-by-step, is it you want your macro to do? Why? What do you hope to accomplish by doing this? |
#5
|
|||
|
|||
Quote:
Quote:
Quote:
I am pasting the current macro code below. Before running the macro, the user would select the freestanding heading (and any white space to the right of that heading). The macro places a period at the end of that text, brings the following paragraph up to join it, and, ideally would apply the Heading 3 style to the text before the period and the Body Text style to the remainder of the paragraph. I'm not sure if that is clear, but I can make a video if it is not. Code:
Sub ChangeAPA2toAPA3() ' ' ChangeAPA2toAPA3 Macro ' Changes from freestanding APA 2 level subhead to run-in APA level 3 subhead. Uses BodyText style to define the paragraph style. ' Selection.ClearFormatting Selection.Style = ActiveDocument.Styles("Body Text") Selection.Range.Case = wdLowerCase Selection.Range.Case = wdTitleSentence Selection.MoveRight Unit:=wdCharacter, Count:=1 Selection.TypeBackspace Selection.TypeText Text:=". " Selection.MoveLeft Unit:=wdCharacter, Count:=2 Selection.HomeKey Unit:=wdLine, Extend:=wdExtend Selection.Style = ActiveDocument.Styles("Heading 3") Selection.MoveRight Unit:=wdCharacter, Count:=1 Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend Selection.Font.Bold = wdToggle End Sub |
#6
|
|||
|
|||
I'm not sure you can do this with a recorded macro.
What your macro needs to be doing is inserting the style separator at the end of your heading text and then applying the heading style to the original. |
#7
|
||||
|
||||
Try something along the lines of:
Code:
Sub ChangeAPA2toAPA3() ' ' ChangeAPA2toAPA3 Macro ' Changes from freestanding APA 2 level subhead to run-in APA level 3 subhead. ' Uses BodyText style to define the trailing paragraph style. ' With Selection .Font.Reset With .Paragraphs.Last .Style = "Body Text" .Range.Case = wdLowerCase .Range.Case = wdTitleSentence End With With .Paragraphs.First .Style = "Heading 3" End With .Collapse wdCollapseStart .InsertStyleSeparator End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#8
|
|||
|
|||
Thank you for the macro Paul (and thank you Charles for clarifying the style separator).
This video shows the macro in action (I added a period with the TypeText function and added a missing End Width). I would like to make the period take the Body Text style (so that the period does not get generated into the Table of Contents), but I was not sure how to achieve that within the current code. Also it seems like the text following the heading is not taking the Body Text style in the video (although it worked for me when I tested it in another document, so I'm not sure what is preventing it from working here). Any ideas on that? Thank you again to you both--I'm always impressed by helpful you are! Last edited by cheech1981; 08-23-2017 at 08:30 AM. Reason: could not upload .mp4 file so I uploaded it to youtube and shared that way |
#9
|
||||
|
||||
The reason the 'Body Text' Style isn't being applied to the 2nd paragraph is that you didn't include it as part of your selection when running the macro.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
Tags |
heading styles, macro, recorder |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Applying heading style to multiple pages at once | jane.bugai | Word | 5 | 02-24-2017 04:05 PM |
Using heading style level in a paragraph | kam | Word | 7 | 03-24-2014 02:58 PM |
Macro to replace one specific heading style with another | ubns | Word VBA | 44 | 09-04-2012 08:17 PM |
Applying style to first part of paragraph | foxtrot | Word | 3 | 07-24-2012 07:30 AM |
Character style stripped while applying paragraph style | sams_gates | Word | 0 | 08-29-2009 02:03 AM |