none
Import Word Styles with a macro. RRS feed

  • Question

  • Hi Friends,

    I have this word style template that I'm using again and again to format my documents. For now I'm using a macro to import all this into my document but it takes some reasonable time to import them all with the macro. This is the code I'm using right now:

        Application.OrganizerCopy Source:= _
            "C:\Word Templates\Global Template\Styles.dotx" _
            , Destination:=ActiveDocument.FullName, Name:="H1X", Object:=wdOrganizerObjectStyles

    This is just one line. In my original code i have 40-50 styles which i have to import so I've repeated this in my macro code. Problem is that macro opening and importing style one by one for all 50 styles which takes a long time to run. 

    Any help is much appreciated.

    Regards

    _________________

    Supun Samarakoon


    • Edited by Supunsam Monday, August 22, 2016 2:44 PM
    Monday, August 22, 2016 2:44 PM

Answers

  • A very simple way to do this is to add a paragraph in each of the Styles to be copied to your Styles 'template', then run the following macro (which you might add to your Normal template), from whichever document you want to update:

    Sub CopyStyles()
    Application.ScreenUpdating = True
    Dim DocSrc As Document, DocTgt As Document, Rng As Range
    Set DocTgt = ActiveDocument
    Set DocSrc = Documents.Open("C:\Users\" & Environ("Username") & "\Documents\StylesDoc.docx", _
      ReadOnly:=True, AddToRecentFiles:=False)
    Set Rng = DocTgt.Characters.Last
    With Rng
      .Collapse
      .FormattedText = DocSrc.Range.FormattedText
      .Delete
    End With
    DocSrc.Close False
    Set DocSrc = Nothing: Set DocTgt = Nothing
    Application.ScreenUpdating = True
    End Sub

    Simply copying content with the Styles will add them to the target document.

    Edit the 'DocSrc' definition as appropriate.


    Cheers
    Paul Edstein
    [MS MVP - Word]


    • Edited by macropodMVP Tuesday, August 23, 2016 3:14 AM Code revision
    • Marked as answer by Supunsam Sunday, August 28, 2016 8:01 PM
    Tuesday, August 23, 2016 3:13 AM

All replies

  • Hi Spunsam,

    As you are working in a macro. you can try to use Document.CopyStylesFromTemplate Method (Word).

    visit the link below to get more information regarding that method.

    Document.CopyStylesFromTemplate Method (Word)

    Hope that helps you to what you want to achieve.

    Regards

    Deepak


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, August 23, 2016 2:38 AM
    Moderator
  • A very simple way to do this is to add a paragraph in each of the Styles to be copied to your Styles 'template', then run the following macro (which you might add to your Normal template), from whichever document you want to update:

    Sub CopyStyles()
    Application.ScreenUpdating = True
    Dim DocSrc As Document, DocTgt As Document, Rng As Range
    Set DocTgt = ActiveDocument
    Set DocSrc = Documents.Open("C:\Users\" & Environ("Username") & "\Documents\StylesDoc.docx", _
      ReadOnly:=True, AddToRecentFiles:=False)
    Set Rng = DocTgt.Characters.Last
    With Rng
      .Collapse
      .FormattedText = DocSrc.Range.FormattedText
      .Delete
    End With
    DocSrc.Close False
    Set DocSrc = Nothing: Set DocTgt = Nothing
    Application.ScreenUpdating = True
    End Sub

    Simply copying content with the Styles will add them to the target document.

    Edit the 'DocSrc' definition as appropriate.


    Cheers
    Paul Edstein
    [MS MVP - Word]


    • Edited by macropodMVP Tuesday, August 23, 2016 3:14 AM Code revision
    • Marked as answer by Supunsam Sunday, August 28, 2016 8:01 PM
    Tuesday, August 23, 2016 3:13 AM
  • Hi paul,

    Thank you very much for your input. I don't know whats wrong on my end but this macro didn't worked for me.

    And can you kindly explain whats exactly executing with this macro. You've mentioned its copying contents from the template. Right?

    Regards,

    Supun Samarakoon

    Saturday, August 27, 2016 8:29 AM
  • Hi Deepak,

    Thank your for your contribution with this matter. I already tried out the method you've mentioned. Its very speed but it will copy all the styles including "Normal" style from the Template which is not necessary.

    Regards

    Supun Samarakoon

    Saturday, August 27, 2016 8:31 AM
  • Did you create a paragraph in the source document in each Style you want to copy to the target document? Did you edit the 'DocSrc' definition as advised?

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Saturday, August 27, 2016 12:07 PM
  • Hi Paul,

    Sorry for my foolishness. Mistake was at my end. Like you said. I missed to create style paragraphs. This works really really well. Takes only a second to run. Exactly what I was hoping for. Thank you very much my friend. Don't know how to thank you.

    Regards

    ______________________

    Supun Samarakoon

    Sunday, August 28, 2016 8:04 PM