Results 1 to 6 of 6
  1. #1
    Star Lounger
    Join Date
    Nov 2003
    Posts
    61
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Deleting/Preserving Styles in Word 2003/VBA (VBA/Word 2003)

    I have the same list of styles in every file I use. There are about 15 of these. However, the original files come from different authors/users and contain the styles they use as well. I need to blow those styles away while keeping my 15 styles. There is no consistency in what styles are used by the authors so I can't really approach it from that route. I'm sure there is a way to automate that. I also need to account for Word's built in styles, which are ok to keep, but may cause issues in a macro.

    Any ideas/help is certainly appreciated! [img]/forums/images/smilies/smile.gif[/img]

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

    Re: Deleting/Preserving Styles in Word 2003/VBA (VBA/Word 2003)

    If you simply delete a style that is in use from a document, all text using that style will revert to the default Normal style. Is that OK, or should such text use one of your styles?

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

    Re: Deleting/Preserving Styles in Word 2003/VBA (VBA/Word 2003)

    The following macro will delete all styles except those specified in a list at the beginning of the macro, and those built-in styles that cannot be deleted.
    <code>
    Sub DeleteStyles()
    ' List of styles to keep, delimited by pipe characters |
    Const strStyleList = "|MyStyle|MyBullet|MyNumber|"
    Dim i As Integer
    ' Suppress error when trying to delete built-in style
    On Error Resume Next
    ' Loop backwards through styles
    For i = ActiveDocument.Styles.Count To 1 Step -1
    ' Check if in list
    If InStr(1, strStyleList, "|" & ActiveDocument.Styles(i) & "|", _
    vbTextCompare) = 0 Then
    ' If not, delete
    ActiveDocument.Styles(i).Delete
    End If
    Next i
    End Sub
    </code>
    Edit the constant strStyleList as needed, but keep | (Shift+) as delimiter, and make sure that the string begins and ends with a | character.

  4. #4
    Star Lounger
    Join Date
    Nov 2003
    Posts
    61
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Re: Deleting/Preserving Styles in Word 2003/VBA (VBA/Word 2003)

    It should be one of my styles which has already been applied. I have some subheads and block quotes that have to be captured, but I have the code for that taken care of.

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

    Re: Deleting/Preserving Styles in Word 2003/VBA (VBA/Word 2003)

    OK, if your style has already been applied, you should be able to use the macro I posted in the meantime to delete all unwanted styles from the document.

  6. #6
    Star Lounger
    Join Date
    Nov 2003
    Posts
    61
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Re: Deleting/Preserving Styles in Word 2003/VBA (VBA/Word 2003)

    Thanks Hans! I think thats exactly what I was needing.

Posting Permissions

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