Style InUse

J

James

I want to check whether a style is currently applied to text in a document. I
know the InUse property can't do this. You can use the Task Pane to "Select
All nn instances", but is there a way to access this through VBA?

Thanks
 
G

Greg

James,

Maybe something like:
Sub CheckForStyle()

Dim oPara As Paragraph
Dim oStyName As String
Set oPara = ActiveDocument.Paragraphs(1)
oStyName = InputBox("What style name are you looking for?", "Style
Name")
Do
If oPara.Style = oStyName Then
MsgBox (oStyName & " is used in this document")
Exit Sub
End If
Set oPara = oPara.Next
Loop Until oPara Is Nothing
MsgBox (oStyName & " is not used in this document")
End Sub
 
D

Dave Lett

Hi James,

You can use the following to get a list of all the styles that you have
actually used in a document:

Dim oSty As Style
Dim oStylesInUse As String
Selection.HomeKey Unit:=wdStory
For Each oSty In ActiveDocument.Styles
If oSty.InUse And oSty.NameLocal <> "Default Paragraph Font" Then
With Selection.Find
.ClearFormatting
.Text = ""
.Style = oSty.NameLocal
If .Execute Then
oStylesInUse = oStylesInUse & oSty.NameLocal & vbCrLf
Selection.HomeKey Unit:=wdStory
End If
End With
End If
Next oSty
MsgBox oStylesInUse

You can invoke the dialog box and have the document "highlight" the found
items, as in using the Task Pane to "Select All nn instances", but then you
need to figure out a way to make the routine stop on that Style. That is,
this doesn't really work the way you want for ALL styles. You'd have to know
a single style you were looking for, as in the following:

Selection.HomeKey Unit:=wdStory
Selection.Find.Style = "Heading 1"

With Dialogs(wdDialogEditFind)
.Format = True
SendKeys "%t%f{ESC}"
.Show
End With

HTH,
Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top