Completely Copying Styles

D

Doug Tumeo

How can I use vba to create new style, using an existing style as a model -
but NOT using "based on". In other words, I want to copy EVERYTHING possible
about a style into a new style, with a new name.

Does anybody have any idea how to do this completely, thoroughly, and
accurately?

(This applies to paragraph styles as well as character styles.)

I would greatly appreciate any help you can provide.

Thanks! :)
 
G

Graham Mayor

It is far simpler to do it manually, but if you wish to do it from vba, you
would have to record each of the settings associated with the applied style
and apply them to a new style. To give you some idea of the settings,
recording the manual operation you get the list below.

ActiveDocument.Styles.Add name:="Style1", Type:=wdStyleTypeParagraph
ActiveDocument.Styles("Style1").AutomaticallyUpdate = False
With ActiveDocument.Styles("Style1").Font
.name = "Arial"
.Size = 11
.Bold = True
.Italic = False
.Underline = wdUnderlineNone
.UnderlineColor = wdColorAutomatic
.StrikeThrough = False
.DoubleStrikeThrough = False
.Outline = False
.Emboss = False
.Shadow = False
.Hidden = False
.SmallCaps = False
.AllCaps = False
.Color = wdColorAutomatic
.Engrave = False
.Superscript = False
.Subscript = False
.Spacing = -0.5
.Scaling = 100
.Kerning = 10
.Animation = wdAnimationNone
End With
With ActiveDocument.Styles("Style1").ParagraphFormat
.LeftIndent = CentimetersToPoints(0)
.RightIndent = CentimetersToPoints(0)
.SpaceBefore = 0
.SpaceBeforeAuto = False
.SpaceAfter = 0
.SpaceAfterAuto = False
.LineSpacingRule = wdLineSpaceSingle
.Alignment = wdAlignParagraphLeft
.WidowControl = True
.KeepWithNext = False
.KeepTogether = False
.PageBreakBefore = False
.NoLineNumber = False
.Hyphenation = True
.FirstLineIndent = CentimetersToPoints(0)
.OutlineLevel = wdOutlineLevelBodyText
.CharacterUnitLeftIndent = 0
.CharacterUnitRightIndent = 0
.CharacterUnitFirstLineIndent = 0
.LineUnitBefore = 0
.LineUnitAfter = 0
End With
ActiveDocument.Styles("Style1").NoSpaceBetweenParagraphsOfSameStyle = _
False
ActiveDocument.Styles("Style1").ParagraphFormat.TabStops.ClearAll
With ActiveDocument.Styles("Style1").ParagraphFormat
With .Shading
.Texture = wdTextureNone
.ForegroundPatternColor = wdColorAutomatic
.BackgroundPatternColor = wdColorAutomatic
End With
.Borders(wdBorderLeft).LineStyle = wdLineStyleNone
.Borders(wdBorderRight).LineStyle = wdLineStyleNone
.Borders(wdBorderTop).LineStyle = wdLineStyleNone
.Borders(wdBorderBottom).LineStyle = wdLineStyleNone
With .Borders
.DistanceFromTop = 1
.DistanceFromLeft = 4
.DistanceFromBottom = 1
.DistanceFromRight = 4
.Shadow = False
End With
End With
ActiveDocument.Styles("Style1").LanguageID = wdEnglishUK
ActiveDocument.Styles("Style1").NoProofing = False
ActiveDocument.Styles("Style1").Frame.Delete


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
S

Stefan Blom

Maybe I'm missing something, but couldn't you use something like this to
duplicate the font, paragraph, and language settings of a certain style?

Sub test()
Dim t As Style, s As Style

Set s = ActiveDocument.Styles("Source style")

Set t = ActiveDocument.Styles.Add("Target style")

With t
.BaseStyle = ""
.ParagraphFormat = s.ParagraphFormat
.Font = s.Font
.LanguageID = s.LanguageID
End With

End Sub

I'm assuming here that "Source style" is the name of an existing style and
"Target style" is the style that should be created. (If both styles already
exist, .Add is not needed and it will in fact generate an error message.)

--
Stefan Blom
Microsoft Word MVP


in message
 
G

Graham Mayor

That appears to work - I had assumed that it would need each individual
parameter setting from the source style, but in practice it doesn't.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
D

Doug Tumeo

I had begun down the path of copying absolutely everything, bit by bit, and
it was just getting to be too much. I had hoped it would be simple, and you
made it that. Thank you very much!

The version I am usind added a tad more, just to capture as much as I could
(see below). But your simplification is what made it possible. Thanks again!

Here's what I have now. It's not fully tested, but it seems do the job...

Dim srcStyle As Style, dstStyle As Style
On Error Resume Next
'By doing this first, it allows us to set style dependencies (BaseStyle)
without other complications...
For Each srcStyle In Document1.Styles
If srcStyle.InUse Then
Call Document2.Styles.Add(srcStyle.NameLocal & " (1)", srcStyle.Type)
End If
Next srcStyle

For Each srcStyle In Document1.Styles
If srcStyle.InUse Then
Set dstStyle = Document2.Styles(srcStyle.NameLocal & " (1)")
With dstStyle
.AutomaticallyUpdate = srcStyle.AutomaticallyUpdate
.BaseStyle = srcStyle.BaseStyle & " (1)"
.Font = srcStyle.Font
.LanguageID = srcStyle.LanguageID
.LanguageIDFarEast = srcStyle.LanguageIDFarEast
.LinkStyle = Document2.Styles(srcStyle.LinkStyle)
.ListTemplate = srcStyle.ListTemplate
.Locked = srcStyle.Locked
.NextParagraphStyle = srcStyle.NextParagraphStyle
.NoProofing = srcStyle.NoProofing
.NoSpaceBetweenParagraphsOfSameStyle =
srcStyle.NoSpaceBetweenParagraphsOfSameStyle
.ParagraphFormat = srcStyle.ParagraphFormat
.Shading = srcStyle.Shading
'I think these two are probably useless, but why not try...
.Frame = srcStyle.Frame
.Table = srcStyle.Table
End With
End If
Next srcStyle
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top