Delete unused styles

H

Henslow

I had a macro that in Word 2002, would delete any styles in a document that
were not in use (shown below). Now when I try to run it in 2003, I get an
error message that "myStyle" (the variable) is not found. Does anyone know
the fix for this problem?

Thanks,

-dz


Sub DeleteUnusedStyles()
Dim myStyle As Style

Dim Msg, Style, Title, Help, Ctxt, Response, MyString
Msg = "Before running this procedure, it is wise to save your file. Hit OK
to continue macro or Cancel to save file." ' Define message.
Style = vbOKCancel + vbExclamation '+ vbDefaultButton2 ' Define buttons.
Title = "Save your file." ' Define title.
Response = MsgBox(Msg, Style, Title, Help, Ctxt)
If Response = vbCancel Then ' User chose Yes.
GoTo bye
'MyString = "Yes" ' Perform some action.
Else ' User chose No.
MyString = "OK" ' Perform some action.
End If

For Each myStyle In ActiveDocument.Styles
If myStyle.InUse Then
' For sleep = 1 To 405000000
' Next sleep
Select Case myStyle
Case ActiveDocument.Styles(wdStyleDefaultParagraphFont)
Case ActiveDocument.Styles(wdStyleNormal)
Case ActiveDocument.Styles(wdStyleHeading1) To _
ActiveDocument.Styles(wdStyleHeading9)
Case Else
Selection.Collapse (wdCollapseStart)
With Selection.Find
.ClearFormatting
.Style = myStyle
.Forward = True
.Wrap = wdFindContinue
.Execute FindText:="", Format:=True
If .Found = False Then
' StatusBar = myStyle.NameLocal
' MsgBox "Delete", vbOKOnly, myStyle.NameLocal
myStyle.Delete
' Else
' MsgBox "Selected?", vbOKOnly, myStyle.NameLocal
' End
End If
End With
End Select
End If
Next myStyle
bye:

Selection.Find.ClearFormatting

End Sub
 
A

Astrid

Hi dz,

I just tried to run your macro.I didn't receive an error.
If you step line by line through the code (F8 in the Visual Basic Editor) on
which line do you get the error?

Astrid
 
H

Henslow

Thanks for the reply.

Actually, I just ran it again, and it normally works. Now I see that in this
particular document I have, it is getting hung up on the very item that is
making this file a problem. There's a proliferation of "char" or "char char"
styles in this document (thousands), and the VBA code does not like the
presence of those. So the macro bombs out on mystyle.delete with the variable
set to "char char".

I have done some research on this quirk in Word, and have found that there
is some documentation/discussion comments that acknowledge this thorny
technical issue. I may have to dig farther to see if some of the suggested
solutions work.
 

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