![]() |
|
#4
|
||||
|
||||
|
Quote:
Code:
Sub StyleCleaner()
Application.ScreenUpdating = False
Dim wdDocA As Document, wdDocB As Document, Stl As Style, StrStl As String, bHid As Boolean
bHid = ActiveWindow.View.ShowHiddenText
ActiveWindow.View.ShowHiddenText = True
Set wdDocA = ActiveDocument: StrStl = "|"
With wdDocA
Set wdDocB = Documents.Open(FileName:=.AttachedTemplate.FullName, AddToRecentFiles:=False, Visible:=False)
With wdDocB
For Each Stl In .Styles
If Stl.BuiltIn = False Then StrStl = StrStl & Stl.NameLocal & "|"
Next
.Close False
End With
For Each Stl In .Styles
With Stl
If .BuiltIn = False Then
If .InUse = False Then
If InStr(StrStl, "|" & .NameLocal & "|") = 0 Then
.Delete
Else
Call DelUnusedStyle(wdDocA, .NameLocal)
End If
End If
End If
End With
Next
End With
Set wdDocA = Nothing: Set wdDocB = Nothing
ActiveWindow.View.ShowHiddenText = bHid
Application.ScreenUpdating = True
MsgBox "All Done.", vbOKOnly
End Sub
Sub DelUnusedStyle(Doc As Document, StlNm As String)
Dim Rng As Range, Sctn As Section, HdFt As HeaderFooter, Shp As Shape, bDel As Boolean
bDel = True
With Doc
'Loop through all story ranges
For Each Rng In .StoryRanges
bDel = Not bFnd(Rng, StlNm)
If bDel = False Then Exit For
Next
'Loop through all headers & footers
For Each Sctn In .Sections
If bDel = False Then Exit For
For Each HdFt In Sctn.Headers
If bDel = False Then Exit For
With HdFt
If .Exists = True Then
If (Sctn.Index = 1) Or (.LinkToPrevious = False) Then
bDel = Not bFnd(HdFt.Range, StlNm)
If bDel = False Then Exit For
For Each Shp In .Shapes
If bDel = False Then Exit For
If Not Shp.TextFrame Is Nothing Then bDel = Not bFnd(Shp.TextFrame.TextRange, StlNm)
Next
End If
End If
End With
Next
For Each HdFt In Sctn.Footers
If bDel = False Then Exit For
With HdFt
If .Exists = True Then
If (Sctn.Index = 1) Or (.LinkToPrevious = False) Then
bDel = Not bFnd(HdFt.Range, StlNm)
If bDel = False Then Exit For
For Each Shp In .Shapes
If bDel = False Then Exit For
If Not Shp.TextFrame Is Nothing Then bDel = Not bFnd(Shp.TextFrame.TextRange, StlNm)
Next
End If
End If
End With
Next
Next
If bDel = True Then .Styles(StlNm).Delete
End With
End Sub
Function bFnd(Rng As Range, StlNm As String) As Boolean
With Rng.Find
.ClearFormatting
.Forward = True
.Format = True
.Style = StlNm
.Wrap = wdFindContinue
.Execute
bFnd = .Found
End With
End Function
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| typing "scr" and problem with "extending selection" | danielmatt | Word | 16 | 07-21-2019 12:31 PM |
| extract email from google search "help" please " i can pay no problem after test" | mohamedabdu | Excel Programming | 1 | 08-22-2018 07:43 PM |
| remove repeated words with " macro " or " wild cards " in texts with parentheses and commas | jocke321 | Word VBA | 2 | 12-10-2014 11:27 AM |
Problem with "Next page" section break automatically turning into "Odd or Even Page"
|
c_gallagher25 | Word | 1 | 02-12-2014 04:27 PM |
How to choose a "List" for certain "Heading" from "Modify" tool?
|
Jamal NUMAN | Word | 2 | 07-03-2011 03:11 AM |