macros for styles

M

MALA

I am trying to create a macro where it checks all the
templates styles with extra styles and displays which are
not matching with the template styles.

eg. I have template where there are 5 styles and people
are suppose to use those only. If they copy paste they are
bringing styles from outside. So, when they click the
buttone it should say these styles are not matching.

Any help!!!

thanks
MALA
 
K

Klaus Linke

bringing styles from outside. So, when they click the
buttone it should say these styles are not matching.


Hi Mala,

The macro below will check for styles that aren't defined in the attached
template.
One thing that may not be obvious is that you need to actually open the
template to get at the styles in the template.

The macro works by raising an error if a style from the doc isn't defined
in the template... this is faster than looping all styles in the template,
looking for the one defined in the document.

The macro could be improved in several ways (don't show the opened template
on screen; add error handlers if no template is attached, or if the
attached template can't be opened; create a list instead of showing
multiple message boxes ...)

Another thing you may want to change: The macro tests all styles in the
document.
Maybe you don't want to check built-in styles, or styles that aren't used?

Regards,
Klaus


Sub TestIfStylesInDot()

On Error GoTo ErrHandler
Dim styleLoop As style
Dim docOpen As Document
Set docOpen = ActiveDocument
Dim docTemplateAttached As Document
Set docTemplateAttached = _
docOpen.AttachedTemplate.OpenAsDocument

' just used to raise an error if style isn't in template:
Dim boolDummy As Boolean

' check all styles in the document
For Each styleLoop In docOpen.Styles
StatusBar = "Checking " & styleLoop.NameLocal
' If styleLoop isn't in the template, the ErrHandler will kick in:
boolDummy = docTemplateAttached.Styles(styleLoop.NameLocal).InUse
Next styleLoop
docTemplateAttached.Close SaveChanges:=False
docOpen.Activate
Exit Sub

ErrHandler:

If MsgBox(styleLoop.NameLocal, vbOKCancel, _
"Style not in Template:") = vbCancel Then
docTemplateAttached.Close SaveChanges:=False
docTemplateAttached.Close SaveChanges:=False
docOpen.Activate
Exit Sub
End If
Err.Clear
Resume Next

End Sub
 

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