Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Star Lounger
    Join Date
    Feb 2011
    Location
    Washington, DC
    Posts
    98
    Thanks
    8
    Thanked 6 Times in 6 Posts

    Char styles not in the document Styles collection for 2007

    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

  2. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    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
    [MS MVP - Word]

  3. #3
    Star Lounger
    Join Date
    Feb 2011
    Location
    Washington, DC
    Posts
    98
    Thanks
    8
    Thanked 6 Times in 6 Posts
    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

  4. #4
    Star Lounger
    Join Date
    Feb 2011
    Location
    Washington, DC
    Posts
    98
    Thanks
    8
    Thanked 6 Times in 6 Posts
    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

  5. #5
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    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
    [MS MVP - Word]

  6. #6
    Star Lounger
    Join Date
    Feb 2011
    Location
    Washington, DC
    Posts
    98
    Thanks
    8
    Thanked 6 Times in 6 Posts
    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.

  7. #7
    Star Lounger
    Join Date
    Feb 2011
    Location
    Washington, DC
    Posts
    98
    Thanks
    8
    Thanked 6 Times in 6 Posts
    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.

  8. #8
    WS Lounge VIP
    Join Date
    Mar 2006
    Location
    Maryland, USA
    Posts
    690
    Thanks
    17
    Thanked 66 Times in 56 Posts
    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

  9. #9
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    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
    [MS MVP - Word]

  10. #10
    Star Lounger
    Join Date
    Feb 2011
    Location
    Washington, DC
    Posts
    98
    Thanks
    8
    Thanked 6 Times in 6 Posts
    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 (tm) and use Pam's method.

  11. #11
    5 Star Lounger kmurdock's Avatar
    Join Date
    Feb 2003
    Location
    Pacific Grove, California, USA
    Posts
    716
    Thanks
    10
    Thanked 34 Times in 28 Posts
    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
    Last edited by kmurdock; 2012-08-01 at 19:01. Reason: forgot to add the function

  12. #12
    Star Lounger
    Join Date
    Feb 2011
    Location
    Washington, DC
    Posts
    98
    Thanks
    8
    Thanked 6 Times in 6 Posts
    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
    Last edited by jweissmn1; 2012-08-02 at 10:50. Reason: Was able to report results from running the code

  13. #13
    5 Star Lounger kmurdock's Avatar
    Join Date
    Feb 2003
    Location
    Pacific Grove, California, USA
    Posts
    716
    Thanks
    10
    Thanked 34 Times in 28 Posts
    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
    Last edited by kmurdock; 2012-08-03 at 12:11. Reason: Verified Styles Pane process in 2007.

  14. #14
    Star Lounger
    Join Date
    Feb 2011
    Location
    Washington, DC
    Posts
    98
    Thanks
    8
    Thanked 6 Times in 6 Posts
    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

  15. #15
    New Lounger
    Join Date
    Aug 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi Jessica,

    Anything in this article helpful?

    Finding and Fixing "Char" Styles
    Applies to: Word 2007, Word 2003
    http://www.microsystems.com/resource...wordtip014.php

    -Marc

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •