Results 1 to 4 of 4

Word VBA - changing the style of a paragraph mark

This is a discussion on Word VBA - changing the style of a paragraph mark within the General Excel Discussion & Other Questions forums, part of the Question Forums category; Hello I'm a little bit out of my depth here and I'm looking for some help with Word VBA . ...

  1. #1
    MrExcel MVP Andrew Fergus's Avatar
    Join Date
    Sep 2004
    Location
    New Zealand
    Posts
    5,240

    Default Word VBA - changing the style of a paragraph mark

    Hello

    I'm a little bit out of my depth here and I'm looking for some help with Word VBA. I have a paragraph mark for which I need to change the style from "Heading 3" to "Normal".

    The text flows as follows:

    Normal text in normal style (CR)
    (PM)
    New Heading in Heading 3 style (CR)

    where:
    (CR) = carriage return or some such thing (i.e. the little arrow that drops down and left; it can be input using Alt 011)
    (PM) = paragraph mark where the style is "Heading 3" (it looks like a backwards 'P' when you unhide the hidden characters)

    Through VBA I can find the paragraph mark that has the style "Heading 3" but I need to change this to a "Normal" style, without changing the style of the line below.

    To date all of my coding attempts have failed, these include selecting just the (PM) line and trying to set the style using a command like this:

    Selection.Style = ActiveDocument.Styles("Normal")

    I have also tried selecting the entire (PM) line as well as the entire line above and setting the style using the same command, to no avail. I have also tried setting the style to an intermediate style like "Heading 2" and then re-setting to "Normal" - again the style change doesn't work. I have also tried to brute force it with a command like this:

    Selection.Style = "Normal"
    again, it doesn't work.

    Whilst I can re-set the style manually in the Word document, I can't get a macro to do this - given the document has hundreds of pages and I don't know how many stray paragraph marks there are, I think a macro is my only option - unless someone has another method? Any help with setting the style of the paragraph mark to "Normal" using VBA would be appreciated. I have spent way too long on this and it feels like it should be very easy!

    Thanks in advance
    Andrew
    ~ >*()))><(


    I'd rather have a full bottle in front of me than a full frontal lobotomy.

  2. #2
    MrExcel MVP Andrew Fergus's Avatar
    Join Date
    Sep 2004
    Location
    New Zealand
    Posts
    5,240

    Default Re: Word VBA - changing the style of a paragraph mark

    Ok, I have (kind of) solved this so will post a solution for anyone who may stumble upon this thread.....it's a bit of a work-around that addresses the symptom not the cause....

    Try what I may I couldn't set the style, but given the text always followed the format of:

    some text in normal style, carriage return, paragraph mark, and some more text in heading 3 style

    I solved this by adding another CR and PM before the first CR, then deleting the CR and PM that had the incorrect style.
    The new CR and PM automatically pick up the correct style from the first block of text. Solved!

    Andrew
    ~ >*()))><(


    I'd rather have a full bottle in front of me than a full frontal lobotomy.

  3. #3
    MrExcel MVP Cindy Ellis's Avatar
    Join Date
    Jun 2006
    Location
    California
    Posts
    1,707

    Default Re: Word VBA - changing the style of a paragraph mark

    Hi Andrew,
    If all Heading 3's needed to be changed to normal, you could possibly have used Find&Replace (without VBA), replacing only the format. If it was just some of the heading 3 paragraphs, though, you pattern matching was probably your best option.
    Cindy
    Cindy

    Excel 2010 on Windows 7 at work, 2003 on Vista at home.
    If you need to post part of your worksheet, try one of these: Excel jeanie, MrExcel HTML Maker or
    Borders-Copy-Paste

  4. #4
    Board Regular Marbles's Avatar
    Join Date
    Feb 2005
    Posts
    560

    Default Re: Word VBA - changing the style of a paragraph mark

    Andrew

    A carriage return in Word is a new paragraph. What you are using as the carriage return, is a new line in the same paragraph. It might make a difference when formatting paragraphs.

    Alan

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


DMCA.com