Results 1 to 10 of 10
  1. #1
    New Lounger
    Join Date
    Jan 2001
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Copying styles from a template into current doc

    I'm trying to do something which I believe is relatively simple, without success. I want to copy all the styles from a particular template (on the network) to whatever document the user is currently in. Here's what I've got:

    Sub UpdateStyles()
    With activedocument
    Application.OrganizerCopy Source:= _
    "N:USERSGuy WigginsWordReal EstateNORMAL.DOT" _
    , Destination:="activedocument", Name:="CenterBoldCap", Object:= _
    wdOrganizerObjectStyles
    Application.OrganizerCopy Source:= _
    "N:USERSGuy WigginsWordReal EstateNORMAL.DOT" _
    , Destination:="activedocument", Name:="DraftLine", Object:= _
    wdOrganizerObjectStyles
    End With
    End Sub

    Actually, this is just two styles; I want to copy all the styles. How do I get this to work? Any help would definitely be appreciated.

  2. #2
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Copying styles from a template into current doc

    Hi David,

    There are at least three ways to do this:

    Temporarily attach the new template and automatically update styles
    Copy the styles from the Organizer - you can use a For Each loop for a more efficient way than the sample you've posted.
    Temporarily paste a block of text, containing all the styles, into the document and then delete it.

    I've got some code at work for copying all the styles via Organizer, and can post the relevant part later today, if that helps.

    Gary

  3. #3
    New Lounger
    Join Date
    Jan 2001
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Copying styles from a template into current doc

    Gary,

    I'd love to see the code for the For Each loop macro. I knew this required some kind of looping procedure, but my macro skills unfortunately are not good enough to create this from scratch.

    Thanks.

  4. #4
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Copying styles from a template into current doc

    Hi David,

    Here's the whole chunk. I'll set it out with <pre> tags below, as well as attach a text file (if I can figure out how!). Post back if you need any tips on adapting this to your situation.

    It's also an appropriate point to mention that you should try to get your customized styles out of Normal.dot; it's best to create templates to control things like styles - this is particularly important if you are sharing your templates and styles with other users.

    <pre>Option Explicit
    Public objCurrentDoc As Document

    Sub SelectiveCopyStyles()
    'Gary Frieder October 2000 Word 97
    'Purpose: For existing documents that do not contain
    ' the full set of house styles
    'Method: Selectively copies in only those styles
    ' that are missing.
    ' This method avoids the problems inherent in
    ' using the "Automatically update doc styles" method.
    'Assumes: Existence of a "StylesSource" template. This could be
    ' a ".dat" file so invisible to users in File/New
    Dim objTempDoc As Document
    Dim sCurrentDocPath As String
    Dim objStyle As Style
    Dim sStyleName As String

    Set objCurrentDoc = ActiveDocument
    sCurrentDocPath = objCurrentDoc.FullName

    'Add a temp doc based a StylesSource template
    Set objTempDoc = Documents.Add("h:Office97TemplatesMeStylesSource_H S1.dot")

    'Loop through all styles in the temp doc and test if they exist
    'in the original doc. If not, copy them in via Organizer
    For Each objStyle In objTempDoc.Styles
    If objStyle.BuiltIn = False Then 'user-defined; test for inclusion
    sStyleName = objStyle.NameLocal
    If IsStyleInDoc(sStyleName) Then 'already in doc; skip
    Else: 'add via Organizer
    Application.OrganizerCopy Source:=objTempDoc.FullName, Destination:= _
    sCurrentDocPath, Name:=sStyleName, Object:=wdOrganizerObjectStyles
    End If
    End If
    Next objStyle
    objCurrentDoc.Save
    objTempDoc.Close (wdDoNotSaveChanges)
    End Sub
    '--------------------------------------------------------------
    Private Function IsStyleInDoc(sStyName As String) As Boolean
    'Purpose: Determine whether a style in StyleSource
    ' already exists in original document.
    Dim aStyle As Style
    For Each aStyle In objCurrentDoc.Styles
    If aStyle = sStyName Then
    IsStyleInDoc = True
    Exit For
    End If
    Next aStyle
    End Function
    </pre>

    Gary
    Attached Files Attached Files

  5. #5
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Copying styles from a template into current doc

    Gary, this is from my Stylescrossreferencer utility, I wrote the function when I discovered that MS's concept of "in the document" differed from mine. I haven't comparison shopped this function vs. your IsStyleInDoc.

    My understanding (help!) is that MS will say that a style is "in" a document if that style has ever been applied to a document. If the applied-to paragraph is then removed, then there is no tangible use of the style in the document, but the style is still "in use".

    Now you know why my ears pricked up about four days ago when the procedures for detecting FONTS were raised ....



    <pre>Public Function boolStyleInDocument(strStylename As String, strDoc As String) As Boolean
    ' Procedure : boolStyleInDocument
    ' Description: Returns TRUE if the style is in the named document.
    ' By: Chris Greaves Inc.
    ' Inputs: A style name.
    ' A document name
    ' Returns: Boolean
    ' Assumes: Nothing
    ' Side Effects: None.
    ' Tested: by the calls shown below.
    boolStyleInDocument = False
    Selection.Find.ClearFormatting
    On Error GoTo Failed
    Selection.Find.Style = ActiveDocument.Styles(strStylename) ' 2000/10/25 fails 5941
    With Selection.Find
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    boolStyleInDocument = Selection.Find.Execute
    Selection.Find.ClearFormatting
    Failed:
    End Function
    </pre>


  6. #6
    New Lounger
    Join Date
    Jan 2001
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Copying styles from a template into current doc

    Thanks Gary this worked like a charm. Actually, the first document I tried it on caused Word to crash repeatedly. (Not sure why.) However, I tried it on several other documents and it worked fine. I'm going to study your code and learn from it.

    By the way, we are using templates to store our styles. The reason we needed this macro was to copy the new set of styles into old documents that were created before these styles were devised. This is the Normal.dot template for the real estate practice area. Each practice area will have its own Normal.dot template with a different set of custom styles.

    Thanks again.

  7. #7
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Copying styles from a template into current doc

    Hi David,

    Glad this is working for you.[img]/w3timages/icons/cool.gif[/img]

    I do have to mention again that you are using Normal.dot for a purpose it shouldn't be used for. If each practice area needs a specific set of styles etc., these should be provided for by workgroup templates specific to each group. Using Normal.dot instead is going to cause you grief down the road.

    For some good information on setting up and deploying templates, track down some of Charles Kenyon's posts (particularly on the Word forum) - he has links back to his own site as well as the MS MVP site - there's good info there about best methods for this.

    Gary

  8. #8
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Copying styles from a template into current doc

    Chris,
    You're right to have gotten a sense of deja vu - you posted this code or code like it around last October on the old Lounge - that posting caused a bevy of lightbulbs to go off in my head as it solved a problem I'd been sitting on for months: how to determine whether a style exists in a document (I recall Andrew Lockton and I had had a torturous lengthy thread about this some months before that, with no solution) - I needed something like this for my Copy/Update styles macro.

    Somewhere along the line I settled on using the compare stylename method rather than the Find/Found - I don't know whether I got this from your posting, or someone else's, or Help file or what.

    And you are right about the problem of a style leaving a 'residue' in the document. In the case of the documents at our firm, this was not the problem so it wasn't an issue.

    What we needed the styles updater for: as our templates evolved, we kept adding new styles. All older documents, including our precedents, lacked these newer styles. A quick method was needed for updating the styles, but not by using "Update Styles".

    More recently I'm looking at an even simpler method, stolen (appropriated?) from an idea by Pam Hanks on the Word forum, to insert an autotext containing all the styles and then delete it immediately - this puts the 'residue' effect you note, to good use. And it should be quicker than looping through all the styles as in this code. I'll probably also have it insert some kind of flag, such as a bookmark or DocVariable, that will let us know the styles update status (T/F) as well as styles update version for any document, so the update styles subroutine can be run on open, only when needed.

    Gary

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

    Re: Copying styles from a template into current doc

    Gary

    I ended up with a macro that searched the document for a style in use (as .InUse didn't behave the way you would expect) and deleted any User defined styles that were not found. I still wonder what Microsoft meant by naming the .InUse property when that is not what it does with Styles.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  10. #10
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Copying styles from a template into current doc

    > I don't know whether I got this from your posting, or someone else's, or Help file or what.

    [blushing] Oh! You got it from *MY* posting! [/blushing]

    >A quick method was needed for updating the styles,

    You needed my Styles Cross Referencer, then. Did you ever use it? It builds a list of all styles actually in use in a set of documents in a filestore and produces a table with the name of the style, a description/definition of the style, and the name of at least one document where that style can be found actually in use.

    Lemme see, armed with this you'ld have (had) a catalogue of all styles active on that day. Armed with THAT table, you could map rogue styles to kosher styles and then rip through all documents (using my Files processor, of course), replacing all occurences of a rogue style with its kosher partner.

    Shouldn't have taken you this long if you started last October (grin)


    BTW, for those of you using lollipop code, I first playfully enclosed the words "blush" and "/blush" in square brackets (I'm too old for yet another window on my screen) and was surprised to find a rather rude version of Eileen appear in place of the [img]/w3timages/icons/blush.gif[/img], so being an extremely intelligent, though modest, sort of guy, I switched to [img]/w3timages/icons/shy.gif[/img] and [/shy].

    Finally I thought that if this darned lounge is going to analyse my particples, I'd give up [trying] allthogether, but it didn't so I haven't.

Posting Permissions

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