• If Active Document Contains Style (Word 2002)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » If Active Document Contains Style (Word 2002)

    • This topic has 6 replies, 3 voices, and was last updated 22 years ago.
    Author
    Topic
    #386685

    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 brickwall

    Viewing 0 reply threads
    Author
    Replies
    • #672103

      I wish I knew a little more about the circumstances of this macro; I might have just a little more advice 2cents smile

      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.

      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

      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

      • #672708

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

      • #678909

        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…

        • #678928

          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

          • #678942

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

        • #679011

          (Edited by HansV to activate link to post – see Help 19)

          Hi Trish, I’m going to post my response to the active thread (post 256454). Hopefully, it will be helpful… Kim

    Viewing 0 reply threads
    Reply To: If Active Document Contains Style (Word 2002)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: