Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / January 2005

Tip: Looking for answers? Try searching our database.

Copy all styles from a template using VBA

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Staroslav - 26 Jan 2005 08:59 GMT
I am trying to write a macro that copies all existing styles from a given
template to an active document. Can't get it to work because Word requires
that I give a name for each style I want to copy. I know it's doable, just am
having problems figuring out the code for it.
Thanks ahead of time for your help!

Below is what I have, but it doesn't work:

Sub CopyAllStyles()
'
Dim SDoc As Document, strDDoc, strSDoc, nStile As String, Stil As Style, S
As Long
strDDoc = ActiveDocument.FullName
strSDoc = "C:\Documents and Settings\Myself\Application
Data\Microsoft\Templates\MyTemplate.dot"

On Error Resume Next
For S = 1 To SDoc.Styles.Count
   nStil = SDoc.Styles(S).NameLocal
   Application.OrganizerCopy Source:=strSDoc, Destination:=strDDoc, _
   Name:=nStil, Object:=wdOrganizerObjectStyles
Next S

End Sub
Jezebel - 26 Jan 2005 09:46 GMT
Why not just use the organizer and do them all at once?

> I am trying to write a macro that copies all existing styles from a given
> template to an active document. Can't get it to work because Word requires
[quoted text clipped - 20 lines]
>
> End Sub
Staroslav - 26 Jan 2005 11:59 GMT
Well, I guess it is because I am too lazy to do it for every single document
I have to work with (there are lots of them). I didn't get into the reasons
for why, because it would take a good deal of explanation. I hope it will be
sufficient to say that this procedure is part of a larger document
preparation macro that goes through a number of steps, and copying styles is
just one of them. Do you know how to???

> Why not just use the organizer and do them all at once?
>
[quoted text clipped - 23 lines]
> >
> > End Sub
Tom Winter - 26 Jan 2005 14:14 GMT
I haven't done exactly what you are doing, but I though this code my
help....(OpenDocument() and CloseDocument() are just my error handling
wrappers around Documents.Open and Document.Close. There's nothing special
about them.)

Public Sub CopyAllAutoText(oWord As Word.Application, sSrcFileSpec As
String, sDstFileSpec As String)

   On Error GoTo ErrorHandler

       Dim oSrcDocument As Word.Document

       Dim oSrcTemplate As Word.Template

       Dim oAutoTextEntry As Word.AutoTextEntry

       Set oSrcDocument = OpenDocument(oWord, sSrcFileSpec)

       Set oSrcTemplate = oSrcDocument.AttachedTemplate

       For Each oAutoTextEntry In oSrcTemplate.AutoTextEntries

           CopyAutoText oWord, sSrcFileSpec, sDstFileSpec,
oAutoTextEntry.Name

       Next

       CloseDocument oSrcDocument

   Exit Sub

ErrorHandler:

   CloseDocument oSrcDocument

   ' Do something with the error

End Sub

Public Sub CopyAutoText(oWord As Word.Application, sSrcFileSpec As String,
sDstFileSpec, sName As String)

   On Error GoTo ErrorHandler

       oWord.OrganizerCopy sSrcFileSpec, sDstFileSpec, sName,
wdOrganizerObjectAutoText

   Exit Sub

ErrorHandler:

   ' Do something with the error

End Sub

> I am trying to write a macro that copies all existing styles from a given
> template to an active document. Can't get it to work because Word requires
[quoted text clipped - 20 lines]
>
> End Sub
Staroslav - 27 Jan 2005 10:19 GMT
Thank you Tom and Charles for the hints. I spent some time trying to get
Tom's code to work, but so far, no success. My VBA knowledge is rather basic,
I am afraid. Still looking for a solution.

> I haven't done exactly what you are doing, but I though this code my
> help....(OpenDocument() and CloseDocument() are just my error handling
[quoted text clipped - 76 lines]
> >
> > End Sub
Charles Kenyon - 26 Jan 2005 14:25 GMT
Note that you will want to run the copy cycle three times to preserve links
between styles.
Signature


Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide

See also the MVP FAQ: http://www.mvps.org/word which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.

>I am trying to write a macro that copies all existing styles from a given
> template to an active document. Can't get it to work because Word requires
[quoted text clipped - 21 lines]
>
> End Sub

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.