Results 1 to 9 of 9
  1. #1
    Lounger
    Join Date
    Jan 2002
    Location
    Israel
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    VBA to exchange Styles (W2K SR1)

    My problem is that Word VBA (as opposed to Word itself) does not seem to distinguish between style "list" and "List" in its replace (or delete, see below) command. That is precisely my problem as I Have both styles in a lot of documents and they cause a mess. The following recorded code just seems to delete "list" and if "list" does not exist, it deletes "List" A test for existence of "list" will solve the latter problem, but without the replacement, the macro is worthless. I am working with W2k

    Selection.Find.ClearFormatting
    Selection.Find.Style = ActiveDocument.Styles("list")
    Selection.Find.ParagraphFormat.Borders.Shadow = False
    Selection.Find.Replacement.ClearFormatting
    Selection.Find.Replacement.Style = ActiveDocument.Styles("List")
    Selection.Find.Replacement.ParagraphFormat.Borders .Shadow = False
    With Selection.Find
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    ActiveDocument.Styles("list").Delete

  2. #2
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA to exchange Styles (W2K SR1)

    The code in Recorded macros is usually incomplete and may even be incorrect.
    You need to manually correct the macro.

    See my rants about macro recording at www.standards.com.

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: VBA to exchange Styles (W2K SR1)

    The whole behavior of style name functions being case sensitive when there are two names that are identical but for case, and being case insensitive otherwise, is a coding disaster waiting to happen. Or that has already happened to you.

    Unfortunately, you might have to iterate over the styles collection using a case-sensitive comparison (e.g., the = operator) to ensure that you know whether the lower case list really exists or not. How about this?

    <pre>Dim sty As Style
    For Each sty In ActiveDocument.Styles
    If sty.NameLocal = "list" Then
    With Selection.Find
    .ClearFormatting
    .Style = ActiveDocument.Styles("list")
    .Replacement.ClearFormatting
    .Replacement.Style = ActiveDocument.Styles("List")
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .Execute Replace:=wdReplaceAll
    End With
    sty.Delete
    Exit For
    End If
    Next</pre>

    Does it work?

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

    Re: VBA to exchange Styles (W2K SR1)

    Another way to handle this problem when it occurs is to use the a style alias in the following way.

    The template contains one style named "List,list"
    When you have a file you wish to clean up, create a new file based on the template and paste the contents of the other file into this file.
    The two styles will now be merged into one.

    You can then remove the style alias if you wish but it doesn't do any harm (just looks untidy) there so you could leave it too.

    You could automate this activity with code to simplify it operationally.

    I would try Jeffersons code first though as it looks friendlier.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  5. #5
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles Area, California, USA
    Posts
    7,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA to exchange Styles (W2K SR1)

    I admit I haven't tested this, but if you create your "cleanup template", then attaching it to the problem documents & ticking Tools/Templates & add-ins.../Automatically update document styles should do the same thing. I'm not sure which is easier, copy & paste or attach a different template & tick the option. I suppose those two steps could be accomplished by code also (but beyond my abilities). <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

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

    Re: VBA to exchange Styles (W2K SR1)

    Phil

    Normally I would agree with you but I actually tested that way first and was dismayed to find that it didn't work. The copy/paste way did however so I thought it was still worthy of a mention. The page setup-header/footer problems might be a big limiter in doing it this way though.

    Jefferson's method still shows more promise but the way I described could be made to work (somewhat successfully).
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  7. #7
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA to exchange Styles (W2K SR1)

    I'd suggest using StrComp in the If statement with a binary compare.

  8. #8
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles Area, California, USA
    Posts
    7,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA to exchange Styles (W2K SR1)

    Thanks for pointing this out, Andrew. I just checked in Word 97 & it behaves the same. It's only merged if you paste text and if you use the alias. I'm not sure why Word treats "List" & "list" as "List,list" (or "list,List") for purposes of pasting, but not for updating document styles. Two different teams as Microsoft that weren't on speaking terms? <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

  9. #9
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: VBA to exchange Styles (W2K SR1)

    Are there situations in which the = operator is not case sensitive? If so, we should all migrate toward that method.

Posting Permissions

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