Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-31-2017, 06:28 AM
cheech1981 cheech1981 is offline applying heading style within paragraph using macro Windows 7 64bit applying heading style within paragraph using macro Office 2010 64bit
Advanced Beginner
applying heading style within paragraph using macro
 
Join Date: Nov 2011
Location: New Jersey, USA
Posts: 77
cheech1981 is on a distinguished road
Default 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?
Reply With Quote
  #2  
Old 05-31-2017, 11:17 AM
Charles Kenyon Charles Kenyon is offline applying heading style within paragraph using macro Windows 10 applying heading style within paragraph using macro Office 2013
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,083
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

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
Reply With Quote
  #3  
Old 05-31-2017, 12:15 PM
cheech1981 cheech1981 is offline applying heading style within paragraph using macro Windows 7 64bit applying heading style within paragraph using macro Office 2010 64bit
Advanced Beginner
applying heading style within paragraph using macro
 
Join Date: Nov 2011
Location: New Jersey, USA
Posts: 77
cheech1981 is on a distinguished road
Default

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.
Reply With Quote
  #4  
Old 05-31-2017, 01:21 PM
Charles Kenyon Charles Kenyon is offline applying heading style within paragraph using macro Windows 10 applying heading style within paragraph using macro Office 2013
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,083
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

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?
Reply With Quote
  #5  
Old 08-21-2017, 07:41 AM
cheech1981 cheech1981 is offline applying heading style within paragraph using macro Windows 7 64bit applying heading style within paragraph using macro Office 2010 64bit
Advanced Beginner
applying heading style within paragraph using macro
 
Join Date: Nov 2011
Location: New Jersey, USA
Posts: 77
cheech1981 is on a distinguished road
Default

Quote:
Originally Posted by Charles Kenyon View Post
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?
Sorry Charles--it was very odd. I clicked on the link my antispyware program gave me a warning message. Then, when I tried to replicate the issue, I had no issue. Not sure what the problem was, but maybe I already had something. I just ran a clean just to be safe but, like you said, I'm sure it wasn't your site, and I apologize for the misunderstanding.

Quote:
Originally Posted by Charles Kenyon View Post
Do you understand the concept of the Style Separator? If not, you need to learn it.
I can successfully create a heading style at the beginning of the paragraph (e.g., Heading 3) while maintaining another style for the rest of the paragraph (e.g., Body Text). I would like to do this through a macro though, rather than doing it manually.

Quote:
Originally Posted by Charles Kenyon View Post
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?
I format documents that have a freestanding, flush-left heading style, and, sometimes, those headings must be changed to a run-in heading at the beginning of the paragraph below (think changing an APA 2 subheading to an APA 3 subheading). I can just do this manually if it is a few of them, but often I have to do it 50 times or so in a document. So I have set up a macro that does this, but the recorder does not seem to be recording the action of applying the Heading 3 style to the first bit of text.

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
Reply With Quote
  #6  
Old 08-21-2017, 10:44 AM
Charles Kenyon Charles Kenyon is offline applying heading style within paragraph using macro Windows 10 applying heading style within paragraph using macro Office 2013
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,083
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

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.
Reply With Quote
  #7  
Old 08-21-2017, 03:08 PM
macropod's Avatar
macropod macropod is offline applying heading style within paragraph using macro Windows 7 64bit applying heading style within paragraph using macro Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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
Note: The above code assumes you've already split the heading & run-in text into two paragraphs. If you're trying to apply bolding to the "Heading 3" content, that should be done via a change in the Style definition; if you're doing it to the run-in text, you should apply the "Strong" Style to that.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #8  
Old 08-23-2017, 08:28 AM
cheech1981 cheech1981 is offline applying heading style within paragraph using macro Windows 7 64bit applying heading style within paragraph using macro Office 2010 64bit
Advanced Beginner
applying heading style within paragraph using macro
 
Join Date: Nov 2011
Location: New Jersey, USA
Posts: 77
cheech1981 is on a distinguished road
Default

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
Reply With Quote
  #9  
Old 08-23-2017, 03:07 PM
macropod's Avatar
macropod macropod is offline applying heading style within paragraph using macro Windows 7 64bit applying heading style within paragraph using macro Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
Reply

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
applying heading style within paragraph using macro Using heading style level in a paragraph kam Word 7 03-24-2014 02:58 PM
applying heading style within paragraph using macro Macro to replace one specific heading style with another ubns Word VBA 44 09-04-2012 08:17 PM
applying heading style within paragraph using macro 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

Other Forums: Access Forums

All times are GMT -7. The time now is 10:51 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft