Hello,
I have tried to use the following code to remove unused styles. Sadly, it has mashed my document base by deleting some styles that are in use. I cannot yet comprehend the pattern of which in-use styles the code deletes and which it does not.
Can anyone see any errors in the below code, or does anyone have any ideas for a better way to achieve this?
Many thanks.
Keith
PublicSharedSub F_S_DeleteUnusedStyles(ByVal V_V_Scalar_Document_Generic AsDocument)
ForEach L_V_Scalar_Style_GenericAsStyleIn V_V_Scalar_Document_Generic.Styles
Try
If Not L_V_Scalar_Style_Generic.BuiltInThen
With V_V_Scalar_Document_Generic.Content.Find
.ClearFormatting()
.Style = L_V_Scalar_Style_Generic.NameLocal
.Execute(FindText:=String.Empty, Format:=True)
IfNot .FoundThen
System.Diagnostics.Debug.WriteLine(L_V_Scalar_Style_Generic.NameLocal)
L_V_Scalar_Style_Generic.Delete()
EndIf
EndWith
EndIf
Catch L_V_Scalar_Exception_GenericAs System.Exception
'Swallow this error, since COM fails unpredictably, in a non-fatal way, sometimes.
System.Diagnostics.Debug.WriteLine("File '"& V_V_Scalar_Document_Generic.FullName &"'; Message: '"& L_V_Scalar_Exception_Generic.Message &"'; NameLocal = '"& L_V_Scalar_Style_Generic.NameLocal &"'.", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information, System.Windows.Forms.MessageBoxDefaultButton.Button1)
EndTry
Next
EndSub
R K Howard