Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2006

Tip: Looking for answers? Try searching our database.

Macros Testing Styles and Creating Styles....

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
NYSA-HD - 20 Oct 2006 20:01 GMT
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
Greg Maxey - 20 Oct 2006 20:43 GMT
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

> 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
[quoted text clipped - 66 lines]
>     End If
> End Sub
NYSA-HD - 25 Oct 2006 17:07 GMT
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?

> You were close and I like your idea.
>
[quoted text clipped - 116 lines]
> >     End If
> > End Sub
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.