Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

copying styles from one doc to another

Options
  • 14-04-2005 11:17am
    #1
    Moderators, Arts Moderators Posts: 35,471 Mod ✭✭✭✭


    I have two versions of a file, in two languages, but the font styles are different in both. I want to copy all the styles from one to the other.

    So far I have:
    Dim SourcePath As String
        Dim SourceFile As String
        Dim DestFile As String
                SourcePath = InputBox("Enter Path to Source File(s)")
        SourceFile = SourceFile
        DestFile = ActiveDocument.FullName
                    
        Application.OrganizerCopy Source:=SourceFile, _
            Destination:=DestFile, Name:="Normal", Object:=wdOrganizerObjectStyles
    
        Application.OrganizerCopy Source:=SourceFile, _
            Destination:=DestFile, Name:="Header 1", Object:=wdOrganizerObjectStyles
    ' repeat for all styles
    
    End Sub
    

    This works as it stands, but rather than repeat the code segment explicitly for each possible font style, I want to loop through the existing styles in the source document and copy them to the destination doc.

    I think I need to use Document.Styles.Count and/or Document.Styles.Item, but neither seem to return a usable style name, just the number of styles available and each style's index. Also, Sourcefile is currently a String, so I probably need to cast that to a File/Document (?) object somehow.

    [edit]
    I think this might work for copying the styles, but as SourceFile is not a real Document object I can't check.
    Dim sStyle As Style
    ....
    For Each sStyle In SourceFile.Styles
    '    ActiveDocument.Styles.Add (sStyle)
    Next sStyle
    

    P.S. would it be possible to enable the [vbcode] tags in vBulletin3 ?


Comments

  • Moderators, Arts Moderators Posts: 35,471 Mod ✭✭✭✭pickarooney


    Okay, I've rethunk this down to a much slimmer Subroutine, and I guess it should work, but I'm having trouble with a couple of smallish issues.
    Sub CopyStyles()
    
        Dim SourcePath As String
        Dim SourceFile As String
        Dim DestFile As String
        Dim sStyle As Style
        Dim tStyle As Style
            
        SourcePath = InputBox("Enter Path to Source File(s)")
        SourceFile = SourcePath & "\" & ActiveDocument.Name
        DestFile = ActiveDocument.FullName
                    
        Open SourceFile For Input As 1
        
        For Each sStyle In ActiveDocument.Styles
            'copy style to variable and close/open destination file
            Set tStyle = sStyle
            'Open DestFile For Output As 2  (should be switch focus/activate)
            ActiveDocument.Styles.Add (tStyle)
        Next sStyle
    End Sub
    

    So I'm reading in the path to the source file, appending the name of the currently opened file (the destination file to which the styles are to be copied)
    to make SourceFile, a String object. I then open it for Input. I think it works up to this point. AFAIK, the focus should switch to the newly opened document at this point, but I'd like to force it just to be sure.
    I then loop through the available styles and... here's where it goes tits-up.
    I want to store each Style in turn in a temporary variable of type Style. If I do this without Set it crashes. I can't seem to copy the value of the Style directly into a variable, only a reference to it. This is probably very fundamental in VB, apologies for the n00bness. At this stage I don't know if copying a pointer to it is enough, or indeed if I need this temp variable, or if I can still copy from SourceFile's Styles when it's no longer the active Document.

    Which is where I meet my second problem.

    Once the Style is copied, referenced or otherwise stored in memory, I want to reactivate the destination file, so it becomes ActiveDocument, then use ActiveDocument.Styles.Add (the style I've just copied from SourceFile). I can't find the syntax for the Document.Activate function anywhere. I also don't know if I can just use the String containing the name of the Document or if I need to make a Document object out of that String first.

    Hope this is clear enough. In reality, the problem is no longer with the Styles per se but with activating and switching between Documents and with storing a property of a Document.
    Also I'm not sure of the significance of the "As 1" part of the Open Statement. I think the '1' is just a window number for the new document, but don't know if it matters what number/name I use.


  • Moderators, Arts Moderators Posts: 35,471 Mod ✭✭✭✭pickarooney


    FWIW, I eventually got it working and with a fairly short bit of code at that;
    Sub CopyStyles()
    
        Dim SourcePath As String
        Dim SourceFile As String
        Dim DestFile As String
        Dim DestPath As String
        Dim BaseName As String
        Dim sStyle As Style
        Dim tName As String
                    
        ActiveDocument.ActiveWindow.Caption = "Destination"
        
        BaseName = ActiveDocument.Name
        DestFile = ActiveDocument.FullName
        DestPath = ActiveDocument.Path
        SourcePath = InputBox("Enter Path to Source File(s)")
        SourceFile = SourcePath & "\" & BaseName
                                                             
        'open the source document
        ChangeFileOpenDirectory SourcePath
        Documents.Open FileName:=BaseName, ConfirmConversions:=False, _
            ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:="", _
            PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
            WritePasswordTemplate:="", Format:=wdOpenFormatAuto
            
        ActiveDocument.ActiveWindow.Caption = "Source"
        
        'Copy only used styles to the Organizer
        For Each sStyle In ActiveDocument.Styles
            If sStyle.InUse = True Then
                'copy style to variable and thence to destination file
                tName = sStyle.NameLocal
                Application.OrganizerCopy Source:=SourceFile, _
                Destination:=DestFile, Name:=tName, Object:=wdOrganizerObjectStyles
            End If
        Next sStyle
        
        'Close Source File and rename Destination to original caption
        Windows("Source").Activate
        ActiveDocument.Close
        Windows("Destination").Activate
        ActiveDocument.ActiveWindow.Caption = BaseName
                      
    End Sub
    


Advertisement