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
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