Macros Testing Styles and Creating Styles....

N

NYSA-HD

I want to read through my document and test to see if a style exists. If the
style exists I want to make sure it is applied to all text in the document
that are centered, bold, and italics. If it does not exist in the document I
want it to be added as a new style and applied to all text that is centered
bold italic. I have the following code I have pieced together but it isn't
working. I think some syntax is off and I may be missing something. I would
appreciate some assistance.

Thanks in advance.
-----------------------------
Public Sub CheckStyle()
Dim currentParagraph As Paragraph

' Call CreateStyle, passing the name and attributes.
CreateStyle "MyDateHeader", "Times New Roman", 11, True, False, 0.25

' Apply Style to each paragraph that is center, bold & italic
For Each currentParagraph In ActiveDocument.Paragraphs
If currentParagraph.Alignment = wdAlignParagraphCenter Then
If currentParagraph.Font.Bold = True Then
If currentParagraph.Font.Italic = True Then
currentParagraph.Style = "MyDateHeader"
Else
End If
Else
End If
Else
End If
Next currentParagraph
End Sub
Public Function StyleExists(stylename As String) As Boolean
Dim currentStyle As Style
Dim stylePresent As Boolean
stylePresent = False

' Check for existence of style in active document.
For Each currentStyle In ActiveDocument.Styles
If currentStyle.MyDateHeader = stylename Then
stylePresent = True
Exit For
End If
Next currentStyle

' Return.
StyleExists = stylePresent
End Function

Public Sub CreateStyle(stylename As String, styleFontName As String, _
styleFontSize As Single, styleBold As Boolean, _
styleItalic As Boolean, Optional styleFirstLineIndent As Single, _
Optional styleSpaceBefore As Single)

' Check if the style already exists.
If Not StyleExists(stylename) Then

' Create the style with attributes passed.
ActiveDocument.Styles.Add stylename
With ActiveDocument.Styles(stylename)
.Font.Name = styleFontName
.Font.Size = styleFontSize
.Font.Bold = styleBold
.Font.Italic = styleItalic
.ParagraphFormat.FirstLineIndent = _
InchesToPoints(styleFirstLineIndent)
.ParagraphFormat.SpaceBefore = _
InchesToPoints(styleSpaceBefore)
.ParagraphFormat.Alignment = wdAlignParagraphCenter
End With
End If
End Sub
 
G

Greg Maxey

You were close and I like your idea.

Option Explicit
Sub Test()
Dim oPar As Paragraph
If Not StyleExists("MyDateHeader") Then
CreateStyle "MyDateHeader", "Times New Roman", 11, True, False, 0.25
End If
For Each oPar In ActiveDocument.Range.Paragraphs
With oPar.Range
If .ParagraphFormat.Alignment = wdAlignParagraphCenter And
..Font.Bold = True And .Font.Italic = True Then
.ParagraphFormat.Reset
.Font.Reset
.Style = ActiveDocument.Styles("MyDateHeader")
End If
End With
Next
End Sub
Public Sub CreateStyle(styName As String, styFont As String, _
pSize As Single, bBold As Boolean, bItalic As Boolean, _
Optional pIndent As Single, Optional pSpaceBefore As Single)
'Create the style with attributes passed.
ActiveDocument.Styles.Add styName
With ActiveDocument.Styles(styName)
With .Font
.Name = styFont
.Size = pSize
.Bold = bBold
.Italic = bItalic
End With
With .ParagraphFormat
.FirstLineIndent = InchesToPoints(pIndent)
.SpaceBefore = InchesToPoints(pSpaceBefore)
.Alignment = wdAlignParagraphCenter
End With
End With
End Sub
Public Function StyleExists(styName As String) As Boolean
Dim oStyle As Style
StyleExists = False
'Check for existence of style in active document.
For Each oStyle In ActiveDocument.Styles
If oStyle.NameLocal = styName Then
StyleExists = True
Exit For
End If
Next oStyle
End Function
 
N

NYSA-HD

Thanks. I really like your response. It works great...except I found one
other issue. In some documents the text that is Times New Roman, 11, Bold,
Italic is being autoformatted as a style other than Normal. I notice the
macro below doesn't change these instances, but I want it to...any ideas?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top