PDA

View Full Version : [SOLVED:] Word Macro apply bold to some text



Jammy
04-13-2016, 03:30 AM
This macro adapted from one found on MSDN exports a list of Word styles to a new document.

I would like to format "NameLocal" as bold (or Heading 1) and leave the "Description" text unformatted.

I'd greatly appreciate if anyone can help with the syntax.


Sub exportStyles()
Dim docActive As Document
Dim docNew As Document
Dim styleLoop As Style

Set docActive = ActiveDocument
Set docNew = Documents.Add

For Each styleLoop In docActive.Styles
With docNew.Range
Selection.Font.Bold = True
.InsertAfter Text:=styleLoop.NameLocal & Chr(9)
.InsertParagraphAfter
.InsertAfter Text:=styleLoop.Description
.InsertParagraphAfter
End With
Next styleLoop
End Sub

gmaxey
04-13-2016, 03:41 AM
Sub exportStyles()
Dim docActive As Document
Dim docNew As Document
Dim styleLoop As Style

Set docActive = ActiveDocument
Set docNew = Documents.Add

For Each styleLoop In docActive.Styles
With docNew.Range
.InsertAfter Text:=styleLoop.NameLocal & Chr(9)
.Paragraphs.Last.Range.Font.Bold = True
.Paragraphs.Last.Range.Font.Underline = wdUnderlineSingle
.InsertParagraphAfter
.Paragraphs.Last.Range.Font.Bold = False
.Paragraphs.Last.Range.Font.Underline = wdUnderlineNone
.InsertAfter Text:=styleLoop.Description
.InsertParagraphAfter
End With
Next styleLoop
End Sub

Jammy
04-13-2016, 03:52 AM
Greg, Thanks! Exactly what I was looking for. Much appreciated.

gmaxey
04-13-2016, 04:08 AM
You're welcome. I added the underline because without spacing (which could have been added too), the name still seemed lost in the weeds.