VBA Express Forum  




Go Back   VBA Express Forum > VBA Code & Other Help > Word Help
     Feedback     
Register FAQ Members Arcade Knowledge Base Training Articles Consulting

Reply
 
Thread Tools Display Modes
Old 09-28-2007, 07:05 AM   #1
markh1182

 
Joined: May 2007
Posts: 76
Kb Entries: 0
Articles: 0
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 tags courtesy of www.thecodenet.com

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

Local Time: 06:21 PM
Local Date: 05-23-2013
Location:

 
Reply With Quote Top
Old 09-28-2007, 08:29 AM   #2
TonyJollans
 
TonyJollans's Avatar

 
Joined: May 2004
Posts: 2,290
Kb Entries: 3
Articles: 0
If you know it always exists why do you need code to see if it exists?

VBA:
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
VBA tags courtesy of www.thecodenet.com


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

Local Time: 06:21 PM
Local Date: 05-23-2013
Location:

 
Reply With Quote Top
Old 09-28-2007, 09:04 AM   #3
markh1182

 
Joined: May 2007
Posts: 76
Kb Entries: 0
Articles: 0
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

Local Time: 06:21 PM
Local Date: 05-23-2013
Location:

 
Reply With Quote Top
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -7. The time now is 10:21 AM.


Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Copyright © 2004 - 2012 VBA Express