PDA

View Full Version : [SOLVED:] How can you add a Heading style to just the first paragraph of a document with VBA?



Helen269
07-11-2022, 07:33 AM
Here's one that should be quite useful to a lot of people.

How would you use VBA to apply a style to just the first paragraph? That is, make everything Heading 1 style, say, up to the first backwards P mark.

Thanks.

Paul_Hossler
07-11-2022, 11:46 AM
Option Explicit


Sub Macro1()
ActiveDocument.Paragraphs(1).Style = "Heading 1"
End Sub

Helen269
07-11-2022, 12:53 PM
That's it? So simple! :-)

I didn't realise paragraphs could be referred to by number. Thank you!

macropod
07-11-2022, 02:56 PM
Similarly:


Sub Demo()
ActiveDocument.Paragraphs.First.Style = wdStyleHeading1
End Sub