xOverTime

Results 1 to 5 of 5

Thread: Solved: How to check if a Word Style exists

  1. #1

    Solved: How to check if a Word Style exists

    Hi, I am trying to find if a style exists within the template in VBA so if it did exist I could call another sub.

    I can do it this way:
    Selection.Find.Style = ActiveDocument.Styles("_AG Green Highlight") 
    Selection.Find.Execute 
    If Selection.Find.Found = True Then 
        If ActiveDocument.Bookmarks.Exists("PerfectPitch_Merged") = False Then 
            Call PerfectPitchLoad 
        End If 
    End If 
    
    
    Formatting tags added by mark007
    However, I know the style always exists in the template of the document, but isn't necessarily in use, therefore I need code to check if it exists, rather than if it is used.

    Hope this makes sense and someone can help.

    Mark

  2. #2
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,290
    Location
    If you know it always exists why do you need code to see if it exists?

     
    Sub Test() 
        MsgBox StyleExists("_AG Green Highlight") 
    End Sub 
     
    Function StyleExists(StyleName As String) As Boolean 
        Dim MyStyle As Word.Style 
        On Error Resume Next 
         ' maybe this ...
        Set MyStyle = ActiveDocument.Styles(StyleName) 
         ' or maybe this ...
         ' Set MyStyle = ActiveDocument.AttachedTemplate.Styles(StyleName)
        StyleExists = Not MyStyle Is Nothing 
    End Function 
    
    
    Formatting tags added by mark007
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  3. #3
    thanks for your help I will give them a go.

    I need the code because word is opened from an external website in this instance and therefore if it exists I want to run some code. However, when a user runs word normally, I don't always want to run the code if that makes sense.

    Thanks, Mark

  4. #4

    How to check if something exists: really good start

    Many thanks, Tony. When I share templates, sometimes I reference objects that don;t exist (yet) on their systems. This is a great base for checking the existence of a lot of vba objects, e.g., ListCaptions, then if the do not exist, adding them: ' So this line calls the function that sets things straight If CaptionLabelExists("AppTable") = False Then CaptionLabels.Add ("AppTable") End If Function CaptionLabelExists(CaptionLabelName As String) As Boolean ' This function checks to see if a referenced CaptionLabel exists ' and if it does not exist, it creates it Dim MyCL As CaptionLabel On Error Resume Next Set MyCL = CaptionLabels(StyleName) CaptionLabelExists = Not MyCL Is Nothing End Function -Bill
    Quote Originally Posted by markh1182 View Post
    thanks for your help I will give them a go. I need the code because word is opened from an external website in this instance and therefore if it exists I want to run some code. However, when a user runs word normally, I don't always want to run the code if that makes sense. Thanks, Mark

  5. #5
    My code for this is:

     
    Sub test_style_exists() 
         
        Dim test_style As String 
         
        test_style = "Quota" 
        MsgBox "Style " & test_style & " exists in " & ActiveDocument.Name & "?" & vbCr & _ 
        style_exists(ActiveDocument, test_style) 
         
    End Sub 
     
    Function style_exists(test_document As Word.Document, style_name As String) As Boolean 
         
        style_exists = False 
        On Error Resume Next 
        style_exists = test_document.Styles(style_name).NameLocal = style_name 
         
    End Function 
    
    
    Formatting tags added by mark007
    Any comments on using set MyStyle versus testing NameLocal?

    Richard.

Posting Permissions

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