altert if style is used?

S

Sydney

Hi

I am trying create a macro that prompt a message box only if a particular
style has been used in a document.

Below is what I have tried, however, the message box appears regardless if
the style is in use or not:

Dim objStyle As Word.Style

With ActiveDocument
If objStyle.InUse And objStyle.NameLocal = "MyStyle" Then
MsgBox "Incorrect Style in use"
End If
End With

With thanks
 
J

Jezebel

First, the code is no good. You haven't set objStyle to refer to anything.
Should be

If ActiveDocument.Styles("MyStyle").InUse then ...

However, this doesn't test whether the style has been applied to anything.
Read Help on the InUse property: "True if the specified style is a built-in
style that has been modified or applied in the document or a new style that
has been created in the document. ... This property doesn't necessarily
indicate whether the style is currently applied to any text in the document.
For instance, if text that's been formatted with a style is deleted, the
InUse property of the style remains True. For built-in styles that have
never been used in the document, this property returns False."

You'll need to use Find, to search for any text with the particular style
applied to it.
 

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