• Char styles not in the document Styles collection for 2007

    Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » Char styles not in the document Styles collection for 2007

    Author
    Topic
    #484541

    Our office uses Office 2007, but there are some legacy docs created in Word 2003. Many have accumulated those evil Char styles. I’d like to create a macro to find them, break the link to their associated style, and delete them.

    But it seems that they do not show up when I iterate through the document.Styles collection. Is there any way to search or iterate and get to them?

    For what it’s worth, they don’t show up on any styles list in the Word 2007 UI, except for doing a Find with style specified.

    Any ideas or suggestions?

    Thanks,

    Jessica

    Viewing 12 reply threads
    Author
    Replies
    • #1342656

      Try:

      Code:
      Sub CleanStyles()
      Dim oSty As Style
      With ActiveDocument
        On Error Resume Next
        For Each oSty In .Styles
          If oSty.Linked = True Then
            .Styles.Add Name:="TmpSty"
            oSty.LinkStyle = "TmpSty"
            .Styles("TmpSty").Delete
          End If
        Next
      End With
      End Sub

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

      • #1342668

        Thanks. I tried something equivalent to your code and none of those linked char styles ever turned up as I iterated through activeDocument.Styles…or at least when I displayed each style.namelocal in the loop the Char styles didn’t appear, and the Char styles were not deleted.

        I’ll try precisely this code when I am back at the office on Friday. I’m hoping that my code actually was’t equivalent and this will work.

        – Jessica

    • #1342717

      Good morning. I tried your code, and none of the Char styles were deleted. I tried removing the On Error line, and of course it then failed on the many styles where the oSty.Linked property was not available.

      So I changed it to

      If oSty.Type = wdStyleTypeLinked

      which at least doesn’t cause an error and seems to be equivalent to your code.

      Same result: char styles are still there. Iterating through activedocument.Styles doesn’t seem to pick them up.

      Any other ideas?

      Thanks,

      Jessica

    • #1342837

      Hi Jessica,

      Sorry, it seems I omitted the line that actually deletes the linked Style:

      Code:
      Sub CleanStyles()
      Dim oSty As Style
      With ActiveDocument
        On Error Resume Next
        For Each oSty In .Styles
          If oSty.Linked = True Then
            .Styles.Add Name:="TmpSty"
            oSty.LinkStyle = "TmpSty"
            .Styles("TmpSty").Delete
            oSty.Delete
          End If
        Next
      End With
      End Sub

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

    • #1343131

      Hi again. I copied your code exactly. It still did not work; the char styles are still shown when I try to search with format matching a style, though they don’t show anywhere else in the UI, including the Organizer in the Templates and Add-ins dialog box.

      Even after a Save As, the char styles still show up when I do a search including style in the formatting.

      It’s slightly odd that the Styles.Add method uses the Name parameter where the parameter is now called NameLocal otherwise. And it is even odder that the Word 2007 help for the styles.add method onlytalks about HeadingStyle and not regular styles. I assume the latter is an error.

      Thanks for your help.

      – Jessica

      Perhaps that listing is just some kind of phantom.

    • #1343136

      Ok, this is getting wierder. I created a new untouched Normal.dotx. It has no char styles in it. I ran your macro, copied and pasted.

      It created, and I am NOT making this up, a style called Body Text Char.

      I deleted that style using the Immediate window. I tried to search formatting with that style specified, and the style was not there.

      I ran your macro again. Body Text Char was created again.

      This is spooky.

    • #1343170

      I have used several macros since 2oo4, or so, to delete char styles, but none of them work today. The only thing that does work is using thiscommand typed into the immediate window:

      Active.Document.Styles(“Put style name here”).delete

      In W2003 the style name was case sensitive, but that’s not true in W2001&10. It only works one style at a time.

      Pam

    • #1343248

      The only Style the macro creates is one named “TmpSty”, which it also deletes. There is a built-in Character Style named ‘Body Text’. I do not get a Style named ‘Body Text Char’ when I run the macro.

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

    • #1343287

      Hi, Paul. I understand that the macro only creates TmpSty on purpose. However, before I ran this particular test of the macro I had a Body Text style but no Body Text Char style. After I ran the macro I had both Body Text and Body Text Char. Honest. It may have something to do with Body Text being the alphabetically-first linked style in the doc. Or it may not.

      I’m going to consider this a Microsoft Word Version Change Mystery ™ and use Pam’s method.

    • #1343487

      In 2004, Gary Frieder created the following code to remove “CharChar” styles. And as you’ll see, Gary mentioned code from Andrew Lockton, too. There’s a comment in the code regarding built-in styles. Does this help at all?

      Best, Kim

      Code:
      Public Sub RemoveCharCharStyles(ByVal Control As IRibbonControl)
      ‘Gary Frieder 2004
      Dim objDocStyles As Styles
      Dim objTempStyle As Style
      Dim StylesCt As Long
      Dim strStyleName As String
      Dim c As Long
      Dim p As Long
      Set objDocStyles = ActiveDocument.Styles
      StylesCt = objDocStyles.Count
      
      ‘First pass – get rid of linked character styles:
      For c = StylesCt To 1 Step -1
      If objDocStyles(c).Type = wdStyleTypeCharacter Then
        strStyleName = objDocStyles(c).NameLocal
        If InStr(1, strStyleName, ” Char”)  0 Then
          Set objTempStyle = objDocStyles.Add(Name:=”zTempStyle”)
          objDocStyles(strStyleName).LinkStyle = objTempStyle
          objTempStyle.Delete
          Set objTempStyle = Nothing
        End If
      End If
      Next
      
      ‘Second pass – delete remnant linked paragraph styles with “Char” in name –
      ‘However, built-in styles can’t be deleted, these must be renamed:
      ‘Number of styles in document has changed:
      
      StylesCt = objDocStyles.Count
      For p = StylesCt To 1 Step -1
        If objDocStyles(p).Type = wdStyleTypeParagraph Then
          strStyleName = objDocStyles(p).NameLocal
          If InStr(1, strStyleName, ” Char”)  0 Then
            If objDocStyles(p).BuiltIn = False Then
              objDocStyles(p).Delete
            Else ‘it’s a built-in style, need to rename it instead:
                  ‘how to rename it? – use code to get rid of aliases:
              objDocStyles(p).NameLocal = strGetStyleNameMinusAliases(strStyleName)
            End If
          End If
        End If
      Next ‘n
      Set objDocStyles = Nothing
      End Sub
      
      Public Function strGetStyleNameMinusAliases(ByVal StyleName As String) As String
      ‘Adapted from code by Andrew Lockton:
      Dim iPos As Long
      iPos = InStr(1, StyleName, “,”)
      If iPos  0 Then
        StyleName = Left$(StyleName, iPos – 1)
      End If
      strGetStyleNameMinusAliases = StyleName
      End Function
      
      
    • #1343600

      Not sure what I am supposed to pass to the first procedure – do I need to add a control or something?

      In any case, I copied the code as is, ran it, and no char styles were deleted.

      Has anybody run this code in Word 2007 and had it work?

      – Jessica

      • #1343639

        Sorry, I had this set up to run from the Ribbon. Remove everything in the parens so it reads

        Code:
        Public Sub RemoveCharCharStyles()

        Then run it like a regular macro.

        However, I’ve never had the issue of creating Char styles in Word 2007. I’m currently using Word 2010 and there are a couple of places to turn off format-to-style. Once I turned those off, the +format styles disappeared. You can do the same thing in 2007.

        Turn off Word Options | Advanced, make sure “Keep track of formatting” is unchecked.

        With the Styles Pane visible, at the bottom click Options… and in the dialog that appears, uncheck anything that says “Select formatting as styles”

        That caused the + format nonsense to disappear without running any code. But, I never saw the “Char” in the first place, so I may not be addressing your issue.

        Kim

    • #1344346

      Thanks. I don’t use format-to-style, and I have set the option to display only real styles in the styles pane.

      What I am dealing with is char styles embedded in documents that originated in 2003 but are being edited in 2007.

      The char styles are visible only when I do a search that includes a style specification.

      The macro does not work to remove these styles. I can remove them only via the immediate pane, and even then there are some strange results. For example, if I remove a char style for certain built-in styles, I get a spurious error message but the style is removed anyway.

      At this point I’m going to stop worrying about it, since even the most recalcitrant authors here have been forcibly moved away from using 2003.

      Thanks again.

      – Jessica

    • #1346256

      Interesting and useful. But doesn’t solve my particular problem which has to do with char styles that show up ONLY in the Search box and are not related to the linked character styles discussed in much of the doc.

      The description of 2007-specific char styles is the clearest I’ve seen.

      Thanks!

    • #1346331

      Maybe the macro at the link below will clean out those troublesome Char Styles – seems to work in my testing:
      http://windowsdevcenter.com/pub/a/windows/excerpt/wdhks_1/index.html?page=2

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

    Viewing 12 reply threads
    Reply To: Char styles not in the document Styles collection for 2007

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: