PDA

View Full Version : Solved: How to check if a Word Style exists



markh1182
09-28-2007, 07:05 AM
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

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

TonyJollans
09-28-2007, 08:29 AM
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

markh1182
09-28-2007, 09:04 AM
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

William J. W
01-16-2014, 02:53 PM
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
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

RAK_da_Pira
12-04-2014, 11:02 AM
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



Any comments on using set MyStyle versus testing NameLocal?

Richard.