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:
[VBA] 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[/VBA]
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
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
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