Results 1 to 9 of 9
  1. #1
    3 Star Lounger
    Join Date
    May 2002
    Location
    Smithfield, Free State, South Africa
    Posts
    210
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Replace styles and then some (Word 2000/XP)

    1. How do I replace one style in a document with another style - eg Test with Test01 all at once without the screen flickering. The document has about 40 pages.
    2. Once this has been done, how do I get back to the position I was when I clicked on the button to activate the macro? (I selected all, otherwise the macro paused at the end of the document (if I was in the middle) and asked me if I wanted to continue searching the document.)
    3. Then, I need another macro (maybe the same as 1.) that will change it back from Test01 to Test.
    This is all for editing purposes - pre- and post-edit.

    Regards

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Replace styles and then some (Word 2000/XP)

    Create a procedure such as this:

    Sub ReplaceStyle(Style1 As Variant, Style2 As Variant)
    With ActiveDocument.Range.Find
    .ClearFormatting
    .Style = Style1
    .Replacement.ClearFormatting
    .Replacement.Style = Style2
    .Execute FindText:="", ReplaceWith:="", Format:=True, Replace:=wdReplaceAll
    End With
    End Sub

    Style1 and Style2 can be symbolic constants for built-in styles such as wdStyleNormal and wdStyleHeading1, or strings containing the name of a style, such as "Heading 1" or "Test01". Since this procedure does not select text, the screen should not flicker, and the insertion point will remain where it was.

    Call ReplaceStyle like this in a macro:

    ReplaceStyle "Test", "Test01"
    ...
    ReplaceStyle "Test01", "Test"

  3. #3
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Replace styles and then some (Word 2000/XP)

    Hi Lynden,

    To reduce screen flicker, include the following in Hans' macro, right before the With line:
    <pre>Application.ScreenUpdating = False</pre>

    You should set it back to true at the end of the macro, but that's not strictly necessary. You'll still see a fair amount of flicker on the Status Bar, which isn't affected by the ScreenUpdating property.
    And as a minor performance point, if you use vbNullString instead of "", the code will run faster.

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Replace styles and then some (Word 2000/XP)

    Setting Application.ScreenUpdating to False is often a good idea. In this particular case, running the macro I posted on a 40 page document with lots of occurrences of the style to be replaced didn't cause any screen flicker, and it ran in less than a second.

  5. #5
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Replace styles and then some (Word 2000/XP)

    Once I was demonstrating a new macro to a coworker, and hadn't yet included code to turn off the screen flicker. He said, "Oh, and the screen flashes a lot, so you can tell it's working. That's nice." <img src=/S/shrug.gif border=0 alt=shrug width=39 height=15>

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Replace styles and then some (Word 2000/XP)

    That's a good one! <img src=/S/laugh.gif border=0 alt=laugh width=15 height=15>

  7. #7
    3 Star Lounger
    Join Date
    May 2002
    Location
    Smithfield, Free State, South Africa
    Posts
    210
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Replace styles and then some (Word 2000/XP)

    Thanks to Hans, all others, and the humor as well!
    Regards
    Lynden

  8. #8
    Star Lounger
    Join Date
    Jun 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Replace styles and then some (Word 2000/XP)

    Thats not an uncommon attitude for a user to take. I've found that nothing annoys a user more than a button that doesn't appear to do anything.
    At least if they can see something happening they'll (hopefully) abstain from bashing the keyboard unnecessarily.

    When I run macros that 'move' my insertion point I first bookmark the current position, I use the
    same bookmark name in all macros that need this ability

    ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:="StartedHere"
    ...... here's the actual running code .....

    When the working code has completed its task I use goto to return to my original position
    Selection.GoTo What:=wdGoToBookmark, Name:="StartedHere"

  9. #9
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Replace styles and then some (Word 2000/XP)

    It's a fine line. On the one hand, users want things to run quickly, and turning off screen updating and using the Range object generally help with that, but on the other, they do appreciate visual feedback. If I know one of my macros will take more than 30 seconds or so, I usually pop up a message box telling the user that such-and-such may take several minutes, yada yada.

    Also a big fan of the status bar, a la:

    <pre>Dim para As Paragraph
    Dim k as Integer
    Dim iCount as Integer
    iCount = ActiveDocument.Paragraphs.Count
    k = 1
    For Each para In ActiveDocument.Paragraphs
    ' Do stuff here
    StatusBar = "Working on: " & k & " of " & iCount & " Paragraphs"
    k = k + 1
    Next para
    </pre>


    It's fairly low overhead, and is usually enough to keep itchy trigger fingers off the Task Manager.
    <img src=/S/2cents.gif border=0 alt=2cents width=15 height=15>

Posting Permissions

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