Word VBA - changing the style of a paragraph mark

Andrew Fergus

MrExcel MVP
Joined
Sep 9, 2004
Messages
5,462
Office Version
  1. 365
  2. 2021
  3. 2016
Platform
  1. Windows
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
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
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
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top