Results 1 to 7 of 7
  1. #1
    3 Star Lounger
    Join Date
    Apr 2003
    Location
    Calgary, Alberta
    Posts
    208
    Thanks
    0
    Thanked 0 Times in 0 Posts

    If Active Document Contains Style (Word 2002)

    I am new to development and have created a macro which first creates styles 1-9, then assigns them to a list template. Everything works fine to this point; however, in the current scenario, a user may include many different list templates. If the user uses more than one list template, I want them to be able to select a previous list template again... if they select the previous list template, the macro fails since the styles have already been created. I think I'm looking for an IF statement to begin looking for the styles THEN if not found, create... ELSEIF found THEN only apply list template. For the life of me I cannot figure out the required code to look for the style in the Active Document... can anyone help? Thanx in advance... trish <img src=/S/brickwall.gif border=0 alt=brickwall width=25 height=15>

  2. #2
    5 Star Lounger kmurdock's Avatar
    Join Date
    Feb 2003
    Location
    Pacific Grove, California, USA
    Posts
    716
    Thanks
    10
    Thanked 34 Times in 28 Posts

    Re: If Active Document Contains Style (Word 2002)

    I wish I knew a little more about the circumstances of this macro; I might have just a little more advice <img src=/S/2cents.gif border=0 alt=2cents width=15 height=15> <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

    However, on the face of it, it seems as if you need an error handler.

    This code checks for the style using Set (see below). Setting a value to an object that doesn't exist produces an error which you can capture and use to your benefit.

    <pre>Sub DoesNewStyleExist(fdummy As Boolean)
    ' Check for style named "StyX"
    ' If not there, goes to error handler

    Dim StyleExist As Object
    Dim S As Integer
    Dim Style2
    On Error GoTo StyleNotThere
    Set StyleExist = ActiveDocument.Styles("Sty1")
    If StyleExist.NameLocal = ("Sty1") Then
    End If
    Exit Sub

    StyleNotThere:

    ' Your code to create the styles and link to list template

    End Sub</pre>


    BTW (and forgive me if you already knew) "(fdummy As Boolean)" keeps a macro from appearing in Word's list of macros.

    Does this help?

    Kim

  3. #3
    3 Star Lounger
    Join Date
    Apr 2003
    Location
    Calgary, Alberta
    Posts
    208
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: If Active Document Contains Style (Word 2002)

    Hi Kim...

    Sorry it has taken me so long to get back to you (in the middle of an office move)... I tested your code this morning and it worked perfect... thanx again... <img src=/S/bravo.gif border=0 alt=bravo width=16 height=30>

  4. #4
    3 Star Lounger
    Join Date
    Apr 2003
    Location
    Calgary, Alberta
    Posts
    208
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: If Active Document Contains Style (Word 2002)

    Hi Kim... Sorry for bugging you about this, I posted this and have not received the answer that I want and, since you were so ingenious with this first puzzle, I thought you may be able to help with my next issue... If you are unable to help, I completely understand... thanx in advance for any ideas/suggestions...

    I have created a macro to create a list template with the help of many of you! I have run into one problem which I've tried to 'logically' figure out but with no success. Initially, the macro for each 'scheme' (list number template) which I create clears the list gallery, then begins looking for the first style in that particular list, if it exists then it simply reattaches the list template; however, if it does not exist it goes to the error handler area to begin creating all the styles then attaches the list template. The code is as follows:

    For Each listgal In ListGalleries
    For i = 1 To 7
    listgal.Reset (i)
    Next i
    Next listgal

    ' Check for style named "ArticleN2 L1"
    ' If not there, goes to error handler

    Dim StyleExist As Object
    Dim S As Integer
    Dim Style2
    On Error GoTo StyleNotThere

    Set StyleExist = ActiveDocument.Styles("ArticleN1 L1")
    If StyleExist.NameLocal = ("ArticleN1 L1") Then

    ' Check for List Template in document - if there, uses current,
    ' if not, creates

    Dim OLstTemp As ListTemplate, TemplateFound As Boolean

    For Each OLstTemp In ActiveDocument.ListTemplates
    If OLstTemp.Name = "ArticleN1" Then
    TemplateFound = True
    Exit For
    End If
    Next OLstTemp

    If TemplateFound Then
    Set OLstTemp = ActiveDocument.ListTemplates("ArticleN1")
    Else
    Set OLstTemp = ActiveDocument.ListTemplates.Add(OutlineNumbered:= True, _
    Name:="ArticleN1")
    End If

    The current problem which I'm trying to figure out is that all the styles are based on a style which I created called BodyText (not Word's built-in Body Text). This macro works fine for all new documents; however, if an older document is opened and the user wants the numbering applied it dies with error 5834 - not finding the based style BodyText. What I need is for it to look for the BodyText style first and if it does not exist then create, then look for the next style, e.g., ArticleNo1 L1 and continue the remainder of the macro. I tried to use another error handler after the first error handler... failed... tried to call another procedure upon error... failed... tried more if statements after error handler... failed??????????????

    I thought about changing the ArticleNo. L1 error handler to BodyText; however, a user may select more than one 'scheme' (list template) in their document. Logically, I'm fried like an egg... any ideas? Thanx for your help in advance...

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: If Active Document Contains Style (Word 2002)

    You could insert code after resetting the listgalleries:

    ' Check for style named "BodyText"
    ' If not there, create it

    Dim StyleBodyText As Style
    On Error Resume Next
    Set StyleBodyText = ActiveDocument.Styles("BodyText")
    If Err <> 0 Then
    Err.Clear
    Set StyleBodyText = ActiveDocument.Styles.Add("BodyText")
    With StyleBodyText
    ' Set style properties here, for example:
    .Font.Name = "Arial"
    End With
    End If

  6. #6
    3 Star Lounger
    Join Date
    Apr 2003
    Location
    Calgary, Alberta
    Posts
    208
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: If Active Document Contains Style (Word 2002)

    Have I told you lately that I LOVE YOU??? Big hugs and kisses... you should write a book! Thanx Hans... trish

  7. #7
    5 Star Lounger kmurdock's Avatar
    Join Date
    Feb 2003
    Location
    Pacific Grove, California, USA
    Posts
    716
    Thanks
    10
    Thanked 34 Times in 28 Posts

    Re: If Active Document Contains Style (Word 2002)

    (Edited by HansV to activate link to post - see <!help=19>Help 19<!/help>)

    Hi Trish, I'm going to post my response to the active thread (<post#=256454>post 256454</post#>). Hopefully, it will be helpful... Kim

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •