I found the following Macro to clean up a workbook of 60k+ styles: https://stackoverflow.com/a/8933399
Every time I run this macro Excel crashes with the error "The object invoked has disconnected from its clients" on the line For Each styleObj In wb.Styles
. This causes styleObj to be Nothing (i.e. null) and crashes the script.
What am I doing wrong? the wb.Styles.Count
lines work perfectly, however everything below that breaks. I am using Excel 2010.
Even simple code like this fails in the same way at the beginning of the for loop:
Sub KillCustomStyles()
Dim mpStyle As Style
For Each mpStyle In ActiveWorkbook.Styles
If Not mpStyle.BuiltIn Then
mpStyle.Delete
End If
Next mpStyle
End Sub