Results 1 to 12 of 12
  1. #1
    New Lounger
    Join Date
    Oct 2003
    Location
    Bergen, New South Wales, Norway
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Styles anarchy - atyles accumulating in template (WordXP, Word2003)

    When copying text from one word doc to another, the styles from the first doc are copied along as well. After a while the number of styles in the target document's template are numbered in hundreds, rendering the styles usage almost useless.
    Which settings in Word can be adjusted to keem the styles from being copied?
    When I copy text into a doc, I want the text to have the same style as the text at the insertion point. I do not want any styles other than the ones in there already - if I want the styles, then I can copy them manually.

    tia
    Olav

  2. #2
    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: Styles anarchy - atyles accumulating in template (WordXP, Word2003)

    If you want pasted text to take on the style of the paragraph into which you are pasting, the easiest way is to use Paste Special as Unformatted text. You lose the style name/definition, but you also lose any direct formatting using this approach. If that's not acceptable, you could experiment with changing the underlying style of the source text before copying (e.g., change the paragraph format to Normal or Body Text or whatever matches your destination). This sometimes removes the direct formatting, sometimes doesn't; I haven't studied it closely enough to discover a pattern.

  3. #3
    2 Star Lounger
    Join Date
    Jul 2001
    Location
    Wisconsin, USA
    Posts
    193
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Styles anarchy - atyles accumulating in templa

    Hi Phil,

    The "S" is missing on first line of code for Sub if anyone is trying to copy it. Also, when I tried to run this, I kept getting an error pointing to ".Style = sty" under the Function portion. Should this be "style" rather than "sty"?

  4. #4
    2 Star Lounger
    Join Date
    Jul 2001
    Location
    Wisconsin, USA
    Posts
    193
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Styles - styles accumulating in template

    Phil,

    Not to be dense, but if I use a couple of the styles in my normal template but no others (all user-defined), it's supposed to delete the ones that aren't used or applied to anything, right?

  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: Styles - styles accumulating in template

    Hi Leanne:
    Correct. All paragraph styles are deleted if there isn't actually a paragraph in the document with that style. Also, I believe that the character styles are deleted also. I think I jumped to conclusions too quickly because they still show up under the Format/Styles/All Styles list. But if you look in the Organizer, they're gone.

    I also need to modify my first post. Olav's templates should NOT be accumulating styles. Only the document will accumulate styles, unless he checks the "Add to template" box under Format/Styles/Modify. Even then, the setting isn't sticky.

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

    Re: Styles anarchy - atyles accumulating in templa

    <P ID="edit" class=small>(Edited by Phil Rabichow on 22-Oct-03 13:12. to add the comments distinguishing documents & templates accumulating styles)</P>Guy Gallo posted a macro that will remove styles not actively in use in a document. So after you format the document to only include those styles to those which you want, you can remove the hundreds of styles that aren't used. However, I noticed that you said that the styles are accumulating in your templates. That shouldn't be the case. They will accumulate in your document only (assuming you are not pasting text consistently in your templates. You would have to deliberately add them to the template by checking Format/Styles/Modify/Add to template & that setting isn't sticky & isn't turned on by default when you paste text.<pre>Sub NukeStyleNotInDocument()
    ' by Guy Gallo
    ' Purpose: remove all user styles that are not ACTIVELY
    ' in use in the current document
    ' remove built-in styles that are not ACTIVELY in use
    ' from the style list

    'Adapted from a macro by Paul Cornell
    'http://office.microsoft.com/assistance/2002/articles/pwWordstyles.aspx


    Dim strTitle As String
    Dim astrstyles() As String
    Dim objstyle As Style
    Dim objDocument As Word.Document
    Dim intCount As Integer
    Dim sh ' Added 6/26

    ' Get the title of the active Word document.
    strTitle = _
    ActiveDocument.BuiltInDocumentProperties(wdPropert yTitle)

    ' Store, in memory, each style in the active document,
    ' including whether the style is in use.
    'Turn on Hidden
    sh = ActiveDocument.ActiveWindow.View.ShowHiddenText
    ActiveDocument.ActiveWindow.View.ShowHiddenText = True

    For Each objstyle In ActiveDocument.Styles

    ReDim Preserve astrstyles(intCount)
    'GG Mod
    If objstyle.BuiltIn = False Then
    If Not StyleInDoc(objstyle) Then
    Debug.Print "Removing " & objstyle
    objstyle.Delete
    Else
    Debug.Print "In use: " & objstyle
    astrstyles(intCount) = objstyle & " (style in Use)"
    intCount = intCount + 1
    End If

    Else
    If objstyle.InUse And Not StyleInDoc(objstyle) Then
    objstyle.Hidden = True
    Debug.Print "Hiding: " & objstyle
    Else
    Debug.Print "In Use: " & objstyle
    astrstyles(intCount) = objstyle & " (style in Use)"
    intCount = intCount + 1
    End If
    '**********
    End If
    Next objstyle

    'Set ShowHidden back
    ActiveDocument.ActiveWindow.View.ShowHiddenText = sh

    ' Create a new Word document to report the results.
    Set objDocument = Word.Documents.Add

    With objDocument.Range

    ' Report the results.
    .InsertAfter "styles in " & strTitle & ":"
    .InsertParagraphAfter
    .Collapse Direction:=wdCollapseEnd

    For intCount = 1 To UBound(astrstyles)

    .InsertAfter astrstyles(intCount)
    .InsertParagraphAfter
    .Collapse Direction:=wdCollapseEnd

    Next intCount

    End With

    MsgBox Prompt:="Done!"
    out:
    End Sub


    Function StyleInDoc(sty)
    'gg
    'Check if a style is in active use.

    Dim myRange As Range
    Set myRange = ActiveDocument.Content

    With myRange.Find
    .ClearFormatting
    .Style = sty
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False


    StyleInDoc = .Execute(FindText:="", Forward:=True, _
    Format:=True) = True
    .ClearFormatting
    End With

    End Function</pre>

    Hope this helps,

  7. #7
    2 Star Lounger
    Join Date
    Jul 2001
    Location
    Wisconsin, USA
    Posts
    193
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Styles - styles accumulating in template

    I keep getting a run time error 4608 - value out of range which in VBA points to the .style = sty line. Hmmm....

  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: Styles - styles accumulating in template

    <P ID="edit" class=small>(Edited by Phil Rabichow on 22-Oct-03 13:13. to add correction & last paragraph)</P>Hi Leanne:
    Thanks for pointing out the missing 'S'. <img src=/S/blush.gif border=0 alt=blush width=15 height=15> I tried the macro & it worked for me. The line
    .style = sty
    in the function seems to be correct. I am using Word 2000. I think it only deletes paragraph styles, not character styles. Correction: it deletes both.

    Later: I just ran it a few times on a couple files. I noticed that if you go to Format/Style/All styles, the deleted style still shows up, even though it doesn't exist in the Organizer. So I do think the style is deleted. If you try to delete the "non existent" style from the list, the delete button is grayed out. It does delete the style from the "in use" list.

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

    Re: Styles - styles accumulating in template

    I'm not sure. Maybe there are too many styles in the document. I tried them on some very long documents, which caused Word to stop responding. I'll have to leave it to one of the VBA experts to explain it.

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

    Re: Styles - styles accumulating in template

    FWIW: The macro works OK in my Word 2002 SP-2. I tried it on a 263 page document with several unused styles; the total number of styles was not very large, though.

  11. #11
    2 Star Lounger
    Join Date
    Jul 2001
    Location
    Wisconsin, USA
    Posts
    193
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Styles - styles accumulating in template

    I tried it in 2000 w/1 page document and 2 styles used (of the 8 I have created in there). I also tried a different doc. but it keeps giving me the error. Maybe for me, it just isn't meant to be. <img src=/S/shrug.gif border=0 alt=shrug width=39 height=15>

  12. #12
    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: Styles - styles accumulating in template

    What value are you passing to the Function as the sty parameter? If you put a Stop as the first statement in the function and inspect the variables in the View>Locals window, does it show as Nothing? If so, there seems to be a problem in the way the function is being called. If it shows as a variant, perhaps you could improve things by defining the variable type of sty:

    Function StyleInDoc(sty As Style)

    At least, so it would appear from this alternative working model:

    <pre>With ActiveDocument.Range.Find
    .ClearFormatting
    .Style = ActiveDocument.Styles("Body Text")
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    If .Execute Then
    MsgBox "Got it!"
    Else
    MsgBox "Ain't got it!"
    End If
    End With</pre>

    Hope this helps.

Posting Permissions

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