atw: Re: Char Char Styles in Word 2007 and Removing Char Char Styles in Word...

  • From: Janine Crutch <janine@xxxxxxxxxxxxxxx>
  • To: <austechwriter@xxxxxxxxxxxxx>
  • Date: Tue, 16 Aug 2011 22:39:50 +1000

Sub DeleteCharCharStylesKeepFormatting( )

  Dim sty As Style

  Dim i As Integer

  Dim doc As Document

  Dim sStyleName As String

  Dim sStyleReName As String

  Dim bCharCharFound As Boolean

  

  Set doc = ActiveDocument

  Do

  

      bCharCharFound = False

      For i = doc.Styles.Count To 1 Step -1

          Set sty = doc.Styles(i)

          sStyleName = sty.NameLocal

          If sStyleName Like "* Char*" Then

          bCharCharFound = True

          If sty.Type = wdStyleTypeCharacter Then

          Call StripStyleKeepFormatting(sty, doc)

          On Error Resume Next

          '#############################################

          ' COMMENT OUT THE NEXT LINE IN WORD 2000 OR 97

          sty.LinkStyle = wdStyleNormal

          sty.Delete

          Err.Clear

      Else

          sStyleReName = Replace(sStyleName, " Char", "")

          On Error Resume Next

          sty.NameLocal = sStyleReName

          If Err.Number = 5173 Then

             Call SwapStyles(sty, doc.Styles(sStyleReName), doc)

             sty.Delete

          Err.Clear

        Else

          On Error GoTo ERR_HANDLER

        End If

      End If

      Exit For

    End If

    Set sty = Nothing

   Next i

Loop While bCharCharFound = True

Exit Sub

ERR_HANDLER:

MsgBox "An Error has occurred" & vbCr & _

        Err.Number & Chr(58) & Chr(32) & Err.Description, _

        vbExclamation

End Sub

 

Function SwapStyles(ByRef styFind As Style, _

                    ByRef styReplace As Style, _

                    ByRef doc As Document)

With doc.Range.Find

     .ClearFormatting

     .Text = ""

     .Wrap = wdFindContinue

     .MatchCase = False

     .MatchWholeWord = False

     .MatchWildcards = False

     .MatchSoundsLike = False

     .MatchAllWordForms = False

     .Style = styFind

     .Replacement.ClearFormatting

     .Replacement.Style = styReplace

     .Replacement.Text = "^&"

     .Execute Replace:=wdReplaceAll

  End With

End Function

 

 

Function StripStyleKeepFormatting(ByRef sty As Style, _

                                  ByRef doc As Document)

Dim rngToSearch As Range

Dim rngResult As Range

Dim f As Font

 

Set rngToSearch = doc.Range

Set rngResult = rngToSearch.Duplicate

 

Do

    With rngResult.Find

    .ClearFormatting

    .Style = sty

    .Text = ""

    .Forward = True

    .Wrap = wdFindStop

    .Execute

   End With

 

   If Not rngResult.Find.Found Then Exit Do

  

   Set f = rngResult.Font.Duplicate

   With rngResult

        .Font.Reset

        .Font = f

        .MoveStart wdWord

        .End = rngToSearch.End

   End With

   Set f = Nothing

Loop Until Not rngResult.Find.Found

End Function

 

 

 

From: austechwriter-bounce@xxxxxxxxxxxxx
[mailto:austechwriter-bounce@xxxxxxxxxxxxx] On Behalf Of Janine Crutch
Sent: Tuesday, 16 August 2011 10:08 PM
To: austechwriter@xxxxxxxxxxxxx
Subject: atw: Re: Char Char Styles in Word 2007 and Removing Char Char
Styles in Word...

 

Hi Neil

 

You forgot Call SwapStyles macro.

 

I'm with Stefan - but since I can't  see them I don't see the problem now.

 

Janine.

 

From: austechwriter-bounce@xxxxxxxxxxxxx
[mailto:austechwriter-bounce@xxxxxxxxxxxxx] On Behalf Of Neil Maloney
Sent: Tuesday, 16 August 2011 9:33 PM
To: austechwriter@xxxxxxxxxxxxx
Subject: atw: Char Char Styles in Word 2007 and Removing Char Char Styles in
Word...

 

This post is for anyone who is interested in:

- whether Char Char styles are created in Word 2007 after turning off "keep
track of formatting" and "select formatting to show as styles", and turning
on "disable linked styles".

- using a macro to remove all Char Char styles from a document, any Word
version (go straight to the bottom of this post if that's your only
interest, both the macros there have been posted before).

This post is NOT about whether styles no longer break (or break much less
often) in 2007 or 2010, it's only about Char Char styles.

Starting off with whether it is possible to stop the creation of Char Char
styles in Word 2007 (and, I assume, 2010) ...

It's very difficult to find anything on the Internet that supports the view
that, with the requisite options turned off or on as stated, Char Char
styles do indeed continue to be created by Word 2007 but the user just can't
easily see them (i.e. they have just been more deeply hidden within Word).

One thread on tech-archive.net that is of interest contains the following,
from Stefan Blom, Microsoft Word MVP, who states (without repeating the
entire thread):

"For what it's worth, here is how I discovered char styles in Word 2007: In
the Find and Replace dialog box, click More, and then click Format, Style.
Take a look at the style listings in the dialog box ..." and then, in
response to a suggestion to turn off keep track of formatting and turn on
disable linked styles  "... in Word 2007, I never apply a paragraph style to
a selection, and I have also selected 'Disable Linked Styles' in the Styles
pane, and still ... I obviously get the char styles anyway. So I'd say that
what is causing char styles in Word 2007 remains a mystery."

In response to Stefan's comment that the Char Char styles are found in the
Find and Replace dialog box as mentioned, DeanH comments: "Exactly, the only
place where I found them too" and provides a link to a macro to remove them
one at a time, which is reproduced further below (but with the addition of
an input box to define the Char style to be removed instead of typing it
into the macro code). I have also included a second macro which I consider
is better, as it gets rid of all Char Char styles in a document without
needing to define the styles.

Pamela Caswell comments, in the thread: "So [Microsoft] must think that the
best way to keep users from damaging documents that are perfectly OK (as far
as they are concerned) is to hide things from them. Sigh" ... and ... "the
MS programmers seem to think that we are imagining these problems and have
taken great pains to hide the broken styles from users."

I know that Christine, from a recent post, has gone into this more deeply
than I have and has a solution that appears to stop Char Char styles from
being created, i.e. because they can then no longer be seen in the Find and
Replace dialog after clicking More, and then clicking Format, Style.
Christine's solution is not available to us because Christine, as is her
right, prefers not to disclose it on this list (no problems with that, and
I'm the same way with keeping quiet about the techniques I use to recover
corrupted documents that "Repair and Open" does nothing to correct). After
extensive searching on the Web, if Christine's solution is out there, I have
missed it and so, especially because no-one else out there appears to have
discovered it, I say again, well done Christine.

Is there anyone on the list that has experiences to share about discovering
Char Char styles in Word 2007 or 2010 after having set the relevant options
as mentioned at the beginning of this post? Or just with any "war stories"
in general about Char Char styles, not particularly related to Word 2007 /
2010?

The techarchive-net thread is at:

http://www.tech-archive.net/Archive/Word/microsoft.public.word.tables/2010-0
2/msg00154.html

The macro for removing one Char Char style at a time is:


Sub Delete_Single_CharCharStyle()

On Error Resume Next

MyStyle = InputBox("What is the CharChar style?", "Style", "Body Text Char")

Dim styl As Word.Style, doc As Word.Document
Set doc = ActiveDocument
Set styl = doc.Styles.Add(Name:="Style1")
doc.Styles(MyStyle).LinkStyle = styl
styl.Delete

End Sub


The macro for removing all of the Char Char styles in a document in the one
hit is:


Sub Delete_All_CharCharStyles()
Dim sty As Style
Dim i As Integer
Dim doc As Document
Dim sStyleName As String
Dim sStyleReName As String
Dim bCharCharFound As Boolean

Application.DisplayAlerts = wdAlertsNone

Set doc = ActiveDocument
Do
    bCharCharFound = False
    For i = doc.Styles.Count To 1 Step -1
        Set sty = doc.Styles(i)
        sStyleName = sty.NameLocal
        If sStyleName Like "* Char*" Then
            bCharCharFound = True
            If sty.Type = wdStyleTypeCharacter Then
            On Error Resume Next
           '#############################################
           ' COMMENT OUT THE NEXT LINE IN WORD 2000 OR 97
           sty.LinkStyle = wdStyleNormal
           sty.Delete
           Err.Clear
        Else
           sStyleReName = Replace(sStyleName, " Char", "")
           On Error Resume Next
           sty.NameLocal = sStyleReName
           If Err.Number = 5173 Then
              Call SwapStyles(sty, doc.Styles(sStyleReName), doc)
              sty.Delete
              Err.Clear
           Else
             On Error GoTo ERR_HANDLER
           End If
         End If
         Exit For
      End If
      Set sty = Nothing
    Next i
  Loop While bCharCharFound = True
  Exit Sub
ERR_HANDLER:
   MsgBox "An Error has occurred" & vbCr & _
          Err.Number & Chr(58) & Chr(32) & Err.Description, _
          vbExclamation
  End Sub


Neil.

************************************************** To view the austechwriter
archives, go to www.freelists.org/archives/austechwriter To unsubscribe,
send a message to austechwriter-request@xxxxxxxxxxxxx with "unsubscribe" in
the Subject field (without quotes). To manage your subscription (e.g., set
and unset DIGEST and VACATION modes) go to
www.freelists.org/list/austechwriter To contact the list administrator, send
a message to austechwriter-admins@xxxxxxxxxxxxx
************************************************** 

Other related posts: