Using VBA to Select and Store the Formatting Style of Text in Word

I am using some VBA in the Normal template to search through text and find stuff, then hyperlink it and format it. I'd like to allow the user on a Preferences userform to customize a font style and have that style remembered (either as some VBA variable values, or perhaps by defining a MS Word "Style" into the Normal template to go along for the ride, then applying that style to selected text (via either objHyperlink.Range.Font. ... or objHyperlink.Range.Style ..., depending on the method selected above).

What is the best/easiest way to store a style or font setup and apply it to selected text ranges?
Who is Participating?
Chris BottomleyConnect With a Mentor Software Quality Lead EngineerCommented:
A form to identify changes, not that I know of.  Call up the font dialog, not as far as I can see.  And for information teh following is an extract from a differentg Q relating to cloning a style which should give some idea of the permutations involved.

Sub AddStyle2(doc As Document, strStyleName As String, styleprefix As String)
Dim srcStyle As Style
Dim nuStyle As Style
Dim prop As Variant
    On Error Resume Next
    If Right(styleprefix, 1) <> "_" Then styleprefix = styleprefix & "_"
    Set srcStyle = ActiveDocument.Styles(strStyleName)
    ActiveDocument.Styles(styleprefix & strStyleName).Delete
    ActiveDocument.Styles.Add styleprefix & strStyleName, srcStyle.Type
    Set nuStyle = ActiveDocument.Styles(styleprefix & strStyleName)
    nuStyle.AutomaticallyUpdate = srcStyle.AutomaticallyUpdate
    nuStyle.NoSpaceBetweenParagraphsOfSameStyle = srcStyle.NoSpaceBetweenParagraphsOfSameStyle
    nuStyle.LanguageID = srcStyle.LanguageID
    nuStyle.NoProofing = False
    nuStyle.font.Name = srcStyle.font.Name
    nuStyle.font.Size = srcStyle.font.Size
    nuStyle.font.Bold = srcStyle.font.Bold
    nuStyle.font.Italic = srcStyle.font.Italic
    nuStyle.font.Underline = srcStyle.font.Underline
    nuStyle.font.UnderlineColor = srcStyle.font.UnderlineColor
    nuStyle.font.StrikeThrough = srcStyle.font.StrikeThrough
    nuStyle.font.DoubleStrikeThrough = srcStyle.font.DoubleStrikeThrough
    nuStyle.font.Outline = srcStyle.font.Outline
    nuStyle.font.Emboss = srcStyle.font.Emboss
    nuStyle.font.Shadow = srcStyle.font.Shadow
    nuStyle.font.Hidden = srcStyle.font.Hidden
    nuStyle.font.SmallCaps = srcStyle.font.SmallCaps
    nuStyle.font.AllCaps = srcStyle.font.AllCaps
    nuStyle.font.Color = srcStyle.font.Color
    nuStyle.font.Engrave = srcStyle.font.Engrave
    nuStyle.font.Superscript = srcStyle.font.Superscript
    nuStyle.font.Subscript = srcStyle.font.Subscript
    nuStyle.font.Scaling = srcStyle.font.Scaling
    nuStyle.font.Kerning = srcStyle.font.Kerning
    nuStyle.font.Animation = srcStyle.font.Animation
    nuStyle.ParagraphFormat.LeftIndent = srcStyle.ParagraphFormat.LeftIndent
    nuStyle.ParagraphFormat.RightIndent = srcStyle.ParagraphFormat.RightIndent
    nuStyle.ParagraphFormat.SpaceBefore = srcStyle.ParagraphFormat.SpaceBefore
    nuStyle.ParagraphFormat.SpaceBeforeAuto = srcStyle.ParagraphFormat.SpaceBeforeAuto
    nuStyle.ParagraphFormat.SpaceAfter = srcStyle.ParagraphFormat.SpaceAfter
    nuStyle.ParagraphFormat.SpaceAfterAuto = srcStyle.ParagraphFormat.SpaceAfterAuto
    nuStyle.ParagraphFormat.LineSpacingRule = srcStyle.ParagraphFormat.LineSpacingRule
    nuStyle.ParagraphFormat.Alignment = srcStyle.ParagraphFormat.Alignment
    nuStyle.ParagraphFormat.WidowControl = srcStyle.ParagraphFormat.WidowControl
    nuStyle.ParagraphFormat.KeepWithNext = srcStyle.ParagraphFormat.KeepWithNext
    nuStyle.ParagraphFormat.KeepTogether = srcStyle.ParagraphFormat.KeepTogether
    nuStyle.ParagraphFormat.PageBreakBefore = srcStyle.ParagraphFormat.PageBreakBefore
    nuStyle.ParagraphFormat.NoLineNumber = srcStyle.ParagraphFormat.NoLineNumber
    nuStyle.ParagraphFormat.Hyphenation = srcStyle.ParagraphFormat.Hyphenation
    nuStyle.ParagraphFormat.FirstLineIndent = srcStyle.ParagraphFormat.FirstLineIndent
    nuStyle.ParagraphFormat.OutlineLevel = srcStyle.ParagraphFormat.OutlineLevel
    nuStyle.ParagraphFormat.CharacterUnitLeftIndent = srcStyle.ParagraphFormat.CharacterUnitLeftIndent
    nuStyle.ParagraphFormat.CharacterUnitRightIndent = srcStyle.ParagraphFormat.CharacterUnitRightIndent
    nuStyle.ParagraphFormat.CharacterUnitFirstLineIndent = srcStyle.ParagraphFormat.CharacterUnitFirstLineIndent
    nuStyle.ParagraphFormat.LineUnitBefore = srcStyle.ParagraphFormat.LineUnitBefore
    nuStyle.ParagraphFormat.LineUnitAfter = srcStyle.ParagraphFormat.LineUnitAfter
    nuStyle.shading.Texture = srcStyle.shading.Texture
    nuStyle.shading.ForegroundPatternColor = srcStyle.shading.ForegroundPatternColor
    nuStyle.shading.BackgroundPatternColor = srcStyle.shading.BackgroundPatternColor

    nuStyle.Borders(wdBorderLeft).LineStyle = srcStyle.Borders(wdBorderLeft).LineStyle
    nuStyle.Borders(wdBorderRight).LineStyle = srcStyle.Borders(wdBorderRight).LineStyle
    nuStyle.Borders(wdBorderTop).LineStyle = srcStyle.Borders(wdBorderTop).LineStyle
    nuStyle.Borders(wdBorderBottom).LineStyle = srcStyle.Borders(wdBorderBottom).LineStyle
    nuStyle.Borders.DistanceFromTop = srcStyle.Borders.DistanceFromTop
    nuStyle.Borders.DistanceFromLeft = srcStyle.Borders.DistanceFromLeft
    nuStyle.Borders.DistanceFromBottom = srcStyle.Borders.DistanceFromBottom
    nuStyle.Borders.DistanceFromRight = srcStyle.Borders.DistanceFromRight
    nuStyle.Borders.Shadow = srcStyle.Borders.Shadow
    On Error GoTo 0

End Sub

Open in new window

Chris BottomleySoftware Quality Lead EngineerCommented:
Saving a font as data is a significant undertaking whereas saving and modifying a style is relatively easy.  I would suggest therefore you have a default style saved in which is your reference style ... to simplify reset.  Now create a user style from the reference and allow the user to select specific parameters which your code changes in the 'copied' style.

If you want 'everything' variable that can of course be done but the interface will be much easier on you if the choices are limited to significant aspects.

Let me know what you want to do and then we can see if we can flesh something out.

travisjbennettAuthor Commented:
Chris --
That's what I suspected. I really like your idea of a "default" or "default template" style that gets copied to a custom style.
However, I'm looking for the easiest way (e.g. hoping for an easy way) to customize the style. Is there a way to call the Font dialog and get what a user puts into it, or maybe a pre-made VBA Form from which I could retrieve properties?
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Chris BottomleySoftware Quality Lead EngineerCommented:
WHat is it you are asking at this point ...They have a font which they modify - why do you want to know what they changed?

travisjbennettAuthor Commented:
Ok, let me rephrase this...
I have a vba macro program that searches through text and finds stuff to hyperlink. I want to give the users a form from which they can customize how the links appear (text color, background color, underlining, boldness, italics, and even font/size options. Everything as seen in the font window in MS Word.
To allow the user to set these options, I need to either
(1) call up a window that comes with word (Font) and save their selections, or
(2) make a window myself with the same properties and save those selections.
From those saved selections I will define a style saved to apply to hyperlinks as created by the vba macro.
My question is: Is there an easy way to accomplish either (1) or (2) above? I.E. has someone already recreated the font form, or is there a way to call it up and get the selections the user makes?
travisjbennettAuthor Commented:
A bit off... but nevertheless helpful.
Chris BottomleySoftware Quality Lead EngineerCommented:
>>> A bit off... but nevertheless helpful.

I'm glad it helped a little, the idea of course was to provide a though provoker to establish how you wanted to proceed, not be an end of it as such so I was awaiting your answer to see what to do next but if you've made it work for you then all the better in terms of personal satisfaction for you.

Don't be afraid to ask for more help on such matters (though, we might say nah!), but there's no harm in asking.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.