PDA

View Full Version : Solved: Create a style within a macro



island17
10-07-2004, 02:48 PM
Hello all

I would like to create a style for use in a word macro. I thought it would be more efficient to create a style, then to have duplicate lines of code, change the font size, changing to bold, ect. I tried to declare a variable as type style, but just can't get off the ground.

Any hints on how to do this will be appreciated.:help

Island17 (russ)

Jacob Hilderbrand
10-07-2004, 02:55 PM
Here is some code for making a style:


ActiveDocument.Styles.Add Name:="MyStyle", Type:=wdStyleTypeParagraph
ActiveDocument.Styles("MyStyle").AutomaticallyUpdate = False
With ActiveDocument.Styles("MyStyle").Font
.Name = "Times New Roman"
.Size = 12
.Bold = False
.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
.Scaling = 100
.Kerning = 0
.Animation = wdAnimationNone
End With
With ActiveDocument.Styles("MyStyle").ParagraphFormat
.LeftIndent = InchesToPoints(0)
.RightIndent = InchesToPoints(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 = InchesToPoints(0)
.OutlineLevel = wdOutlineLevelBodyText
.CharacterUnitLeftIndent = 0
.CharacterUnitRightIndent = 0
.CharacterUnitFirstLineIndent = 0
.LineUnitBefore = 0
.LineUnitAfter = 0
End With
ActiveDocument.Styles("MyStyle").NoSpaceBetweenParagraphsOfSameStyle = False
ActiveDocument.Styles("MyStyle").ParagraphFormat.TabStops.ClearAll
With ActiveDocument.Styles("MyStyle").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("MyStyle").LanguageID = wdEnglishUS
ActiveDocument.Styles("MyStyle").NoProofing = False
ActiveDocument.Styles("MyStyle").Frame.Delete
Selection.Style = ActiveDocument.Styles("MyStyle")

Bilby
10-07-2004, 04:06 PM
WARNING WARNING...
The macro recorder generated code is ok for simple styles ONLY.
If you decide to create a style that has autonumbering or bulleting and resides in a template used to create other documents then you MUST NOT use this method.
Word slots numbered/outlined into gallery positions apparently at random and can cause you incredible grief.

If you simply need to create straight forward styles in a document then your safe, otherwise download one of the papers on style numbering such as the Legal Users guide to Word before attempting numbered/outline styles.

Howard Kaikow
10-07-2004, 07:40 PM
WARNING WARNING...
The macro recorder generated code is ok for simple styles ONLY.
If you decide to create a style that has autonumbering or bulleting and resides in a template used to create other documents then you MUST NOT use this method.
Word slots numbered/outlined into gallery positions apparently at random and can cause you incredible grief.

If you simply need to create straight forward styles in a document then your safe, otherwise download one of the papers on style numbering such as the Legal Users guide to Word before attempting numbered/outline styles.You better believe that! I've got the scars to show for it.
More than once, I have developed macros to build a set of styles.
The output is very valuable, but the task is difficult and oh so tedious.

Be very careful in specifying base styles.

This is one of my least favorite tasks.

island17
10-08-2004, 11:55 AM
Thanks for the input. I started experimenting with the code provided, still need to work out some bugs. The styles I'm think of will be simple no numbering or list intended. Thanks for the warnings.
Don't mark this one solved yet, but I think I am close.

Thanks again to all.

island17
10-14-2004, 02:11 PM
Sorry it took so long to get back to you, Please mark this one solved.

And thanks again for your help

TonyJollans
10-15-2004, 01:16 AM
Russ,

I have marked this one as solved for you, but I think you can do it yourself now. If you click on the "Thread Tools" dropdown at the right hand side above the first post you should have an option to "Mark Solved" - select it and click on "Perform Action" and it's done.

Howard Kaikow
10-15-2004, 08:28 AM
Russ,

I have marked this one as solved for you, but I think you can do it yourself now. If you click on the "Thread Tools" dropdown at the right hand side above the first post you should have an option to "Mark Solved" - select it and click on "Perform Action" and it's done.

Does it matter whether the OP or a moderator marks a thread as solved, i.e., if a moderator marks the thread as solved, can the OP unmark the thread?

If not, except under unusual circumstances, only an OP should mark a thread as solved or ... .

Anne Troy
10-15-2004, 08:33 AM
If the OP indicates or states that their problem is resolved as a result of the communications here, it is marked Solved. If there's no indication, and the OP never returns, it becomes marked Expired. Island asked us to mark this solved.

Howard Kaikow
10-15-2004, 12:44 PM
If the OP indicates or states that their problem is resolved as a result of the communications here, it is marked Solved. If there's no indication, and the OP never returns, it becomes marked Expired. Island asked us to mark this solved.Let me clarify.

My point was, if the moderator marks a thread as solved, can the OP unmark the thread?

fumei
10-18-2004, 11:25 PM
Just to comment on the WARNING WARNING....

WARNING WARNING...do NOT use autonumbering, whether you can making a style by code or not. Do NOT use autonumbering....

Autonumbering is up there with Master/Sub documents. Nice idea...let me know when you finally work it out. I have tried using both since Word 2.0...sorry, they both stink. Explicit number is another thing entirely...