Results 1 to 8 of 8
  1. #1
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Freeze from replacing style in table (Word 2002 SP-2)

    I recently posted about the issue that, when you change a paragraph's style, Word deletes any direct character formatting that involves more than half the paragraph. In an earlier thread that I found, one of the Lounge wise men had noted that this doesn't happen if you change the paragraph's style using Find/Replace, so I've been playing around with an "ApplyTheStyle" macro that uses that technique.

    I've been having no problems for the most part, except within tables. To illustrate my (replicable, I expect) problem, here's some sample code that loops through a document's paragraphs and changes each paragraph's style to Normal using Find/Replace:

    <pre> Dim oPar As Word.Paragraph
    Dim oRng As Word.Range

    For Each oPar In ActiveDocument.Paragraphs
    Set oRng = oPar.Range
    oRng.Collapse wdCollapseStart
    If oRng.Style <> "Table Grid" Then
    With oRng.Find
    .Format = True
    .Style = oPar.Style
    .Replacement.Style = "Normal"
    .Execute Replace:=wdReplaceOne
    End With
    End If
    Next oPar

    Set oRng = Nothing</pre>

    If I create a new document, add a few Body Text paragraphs, then create a 4-column, several-row table using the toolbar button while the cursor's in a Body Text paragraph, all the table's cells are styled Body Text.

    Now if I run the above code, it works until it reaches the 2nd row of the table, at which point Word freezes and needs to be shut down with Task Manager.

    If I comment out the If oRng.Style <> "Table Grid" line (and its corresponding End If), the freeze happens a little sooner (in the 2nd cell of the first row).

    Besides being puzzled by the freezes, I'm also puzzled about why, when I change the style of the first cell in a row using Find/Replace, it changes the style of every cell in the row. This seems to be related to the freeze that happens in the 2nd cell of the first row if I comment out the "Table Grid" check. In that case, after the Find/Replace has changed the first cell's style to "Normal" (and hence indirectly changed the whole row's style to Normal), the freeze happens in the 2nd cell under circumstances where oRng.Style is "Table Grid" but oPar.Style is "Normal" (I learned this by adding Debug.Print lines in the middle of the loop) -- even though oRng has just been set to oPar.Range (so I would think oPar.Style and oRng.Style would be the same).

    1. Can anyone shed light on why Word is freezing?

    2. Can anyone explain why the Find/Replace of the style in the first cell changes the style for the whole row?

  2. #2
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Freeze from replacing style in table (Word 2002 SP-2)

    As a follow-up note: If I run the code one loop at a time and stop it (and Reset) after it does the replacement in the first cell of the first row (which, as previously noted, restyles the entire row), and then I use Word's Find/Replace dialog and set the Find box to "Body Text" style and the Replace box to "Normal" style, (1) hitting the Find Next button results in the first cell of the 2nd row being selected (just as you'd expect), and (2) then hitting the Replace button results in the same freeze I've previously described.

    So the freeze doesn't just happen in the VBA context (although I suspect it may happen because the VBA replacement in the first cell of the first row somehow screws up the table).

  3. #3
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Freeze from replacing style in table (Word 200

    This is a bit of a hornets nest you have stumbled onto and I have no real inputs to add but will provide some observations that might help you solve it.

    One problem I expected immediately is the oddity with Table Styles you mentioned - you can't search for them in a document, and Table Grid is a table style not a paragraph style. This means that the oPar.Style is not necessarily the same as the oRange.Style in the table cells - ie when the paragraph style in a table is Normal then the oRange.Style will return the Table Style name not the paragraph style name. It turns out that this is not the problem though.

    I noticed that replacing the wdReplaceOne with wdReplaceAll avoids the crash but also ignores the range completely and does everything of the same style in the document. This makes it look like the oRng.Find is not just looking in the oRng range but doing the entire document. Thus the Replace One is changing the entire row because that is the limit of the first found object.

    I would resolve this problem from another angle assuming that your ultimate aim is to write a macro to remove all local formatting but retain character styles. I would do this by:
    1. Loop through the defined character styles and put real tags around them ala Wordperfect style [zzBold]bold text[/zzBold]
    2. Remove all formatting as per Ctrl-Q, Ctrl-Spacebar
    3. Search and replace with Wildcards for the character tags to apply the character styles again
    4. Remove all the tags
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  4. #4
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Freeze from replacing style in table (Word 2002 SP-2)

    Once upon a time in a universe far away...

    I wrote a macro to reset manual formatting, but leave paragraph and character formatting alone. This was before word had Table styles, so I have never tested it with those. If you want to try and see if it does what you want then head over to <post#=126295>post 126295</post#>.

    StuartR

  5. #5
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Freeze from replacing style in table (Word 2002 SP-2)

    Thanks, Stuart, I've filed that one away for future study and probable future use. (One of your earlier posts noted that it took you "hours." It would take me days.) However, unless I've misunderstood your procedure, it does somewhat the opposite of what I'm trying to accomplish. I'm trying to preserve (rather than reset) manual character formatting that Word would otherwise reset as a side effect of a style change (because the manual character formatting involves more than half the paragraph).

  6. #6
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Freeze from replacing style in table (Word 200

    <hr> This is a bit of a hornets nest you have stumbled onto<hr>
    Do you mean this issue specifically or my move from WordPerfect to Word generally? <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

    Thanks for your input. I'm still messing around, but the following code seems to be working for me at the moment:
    <pre> Dim oPar As Word.Paragraph
    Dim oRng As Word.Range
    Dim rngTest As Word.Range
    Dim strStyle As String

    strStyle = "Normal"

    For Each oPar In ActiveDocument.Paragraphs
    Set oRng = oPar.Range
    With oRng
    If .Information(wdWithInTable) = False Then
    .Collapse wdCollapseStart
    With .Find
    .Format = True
    .Style = oRng.Style
    .Replacement.Style = strStyle
    .Execute Replace:=wdReplaceOne
    End With
    Else
    Set rngTest = .Duplicate
    rngTest.Collapse wdCollapseStart
    If rngTest.Information(wdAtEndOfRowMarker) = False Then
    If .Characters.Count > 1 Then
    .MoveEnd Count:=-1
    .Cut
    .Style = .Document.Styles(strStyle)
    .Paste
    Else
    .Style = .Document.Styles(strStyle)
    End If
    End If
    End If
    End With
    Next oPar

    Set oRng = Nothing
    Set rngTest = Nothing</pre>


  7. #7
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Freeze from replacing style in table (Word 2002 SP-2)

    > unless I've misunderstood your procedure, it does somewhat the opposite of what I'm trying to accomplish

    Woops. Must read posts more carefully.

    StuartR

  8. #8
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Freeze from replacing style in table (Word 200

    You and me both Stuart.

    I'll have to read more carefully too.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

Posting Permissions

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