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 / June 2005

Tip: Looking for answers? Try searching our database.

Create style if not already present

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
MMH - 23 Jun 2005 01:53 GMT
Hello all

I am trying to work out the VBA code to see if a style (FaxHeader) exists in
a particular document, and if it does not, create the style.

I can work out the code for creating a new style, it is just the code to
check if it already exists that is giving me problems.  Any help would be
much appreciated.

Thanks in advance,
MMH.
Jay Freedman - 23 Jun 2005 03:54 GMT
>Hello all
>
[quoted text clipped - 7 lines]
>Thanks in advance,
>MMH.

Here are two ways to go about it:

(1) Iterate through the entire Styles collection, checking to see
whether any of them have the name you want to use...

   Dim oStyle As Style, NewStyle As Style
   Dim bFound As Boolean
   Dim strNewStyle As String
   
   strNewStyle = "FaxHeader"
   
   For Each oStyle In ActiveDocument.Styles
       If LCase(oStyle.NameLocal) = LCase(strNewStyle) Then
           bFound = True
           Exit For
       End If
   Next oStyle
   
   If Not bFound Then
       Set NewStyle = ActiveDocument.Styles.Add( _
                       Name:=strNewStyle, _
                       Type:=wdStyleTypeParagraph)
       NewStyle.BaseStyle = ActiveDocument.Styles("Normal")
   End If

(2) Use error-trapping. Try to use the style; if it doesn't exist, the
Err.Number property is assigned a nonzero value...

       On Error Resume Next
       If LCase(strNewStyle) <> "normal" Then
           rgAT.Style = ActiveDocument.Styles(strNewStyle)
           If Err.Number <> 0 Then
               Err.Clear
               ' define & apply style
               Set NewStyle = ActiveDocument.Styles.Add( _
                       Name:=strNewStyle, _
                       Type:=wdStyleTypeParagraph)
               NewStyle.BaseStyle = ActiveDocument.Styles("Normal")
               On Error Resume Next
               rgAT.Style = ActiveDocument.Styles(strNewStyle)
           End If
       End If

--
Regards,
Jay Freedman
Microsoft Word MVP         FAQ: http://word.mvps.org
MMH - 23 Jun 2005 05:06 GMT
Thank you - I used option 1 and it worked brilliantly.

MMH

> Here are two ways to go about it:
>
[quoted text clipped - 8 lines]
> Jay Freedman
> Microsoft Word MVP         FAQ: http://word.mvps.org
 
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.