PDA

View Full Version : Solved: Extract style properties



MacroShadow
04-01-2012, 02:41 PM
Is it possible using VBA to extract all properties/definitions of a given style (i.e; Font, Font Size, Font Color, Margins etc.)

fumei
04-01-2012, 08:23 PM
Yes, by testing every single property.

MacroShadow
04-01-2012, 09:32 PM
Can you give a quick example?

Talis
04-01-2012, 09:52 PM
Try this very simple starter:
Select some text with the style and run

Sub GetStyleDescription()
Dim showMe As Variant
showMe = Selection.Style.Description
MsgBox showMe
End Sub

Look up "Description" in VBA Help for a fuller subroutine which shows descriptions of all styles.

MacroShadow
04-01-2012, 10:53 PM
Talis,

Thank you that helped me very much. Can the macro be adjusted so it shows each property in a separate line?

Talis
04-01-2012, 11:31 PM
Sub GetStyleDescription2()
Dim showMe As Variant
showMe = Selection.Style.Description
MsgBox Replace(showMe, ",", vbCr)
End Sub

fumei
04-01-2012, 11:45 PM
Well blow me down with a feather. I completely forgot about that.

MacroShadow
04-02-2012, 03:27 AM
Thank you.

Frosty
04-02-2012, 08:39 AM
Just a quick addendum... I believe that .description property is limited by the same issues as what shows in the dialog box... namely, if your style is based on another style, you may not see all of the properties you think you're getting.

Example: If heading 1 is fully justified, and heading 2 is based on heading 1... I don't think the .description of Heading 2 will show fully justified.

fumei
04-02-2012, 11:44 AM
Another issue with using run amok parent/child styles.

Paul_Hossler
04-02-2012, 02:17 PM
Is it possible using VBA to extract all properties/definitions of a given style (i.e; Font, Font Size, Font Color, Margins etc.)

What do you intend to do with the extracted information?

I've seen macros that will list all the styles' information in a new document

Paul

MacroShadow
04-02-2012, 05:05 PM
What do you intend to do with the extracted information?
l

First and foremost I'm just beginning to discover Word's styles, and so I'm snooping around to find out how to manipulate styles using VBA. Right now this is purely academic, although I can see a case in the future were it will help; say for example I have a need to create a new style identical to an existing one, that has to be sent to a client in a .bas file



I've seen macros that will list all the styles' information in a new document
l

Can you point me to them, it sounds interesting.

Thanks.

p.s.
Maybe I marked the thread as Solved too early.

macropod
04-02-2012, 05:35 PM
Why not just send the client a document with the Style in it, then use the Organizer to copy that Style to whatever the target file is? For a fully-fledged approach to this, see: http://www.msofficeforums.com/vba/10450-loop-through-listboxes-application-organizercopy-3.html#post28497

MacroShadow
04-02-2012, 05:49 PM
Why not just send the client a document with the Style in it, then use the Organizer to copy that Style to whatever the target file is?

Security regulations for one, user that doesn't know how to use styles for another.

macropod
04-02-2012, 05:56 PM
Security regulations for one, user that doesn't know how to use styles for another.
So you're saying security regulations will allow you to send a bas file containing macro code that does who know what, but not an empty document containing it? Bizarre. Aside from that, what's the point of sending code to create a Style if the user doesn't know how to use it?

MacroShadow
04-02-2012, 06:12 PM
This is a theoretical issue as I mentioned in an earlier post, if those examples don't make sense (which apparently they don't) let it be purely academic.

macropod
04-02-2012, 06:18 PM
Theoretically, then, it's a purely academic waste of time. What's the point of asking questions, having someone tell you how it might be done, then you coming back and saying it's no good when (a) you don't have a use for it; and (b) you haven't bothered saying what the real-world constraints are.

fumei
04-02-2012, 07:16 PM
so I'm snooping around to find out how to manipulate styles using VBA

I have been using Word, and styles, for decades and I have rarely if ever manipulated styles using VBA. If by manipulating you mean changing them. USING styles via VBA, sure. But not changing them, no.

And I am assuming that if you want to know properties, that you have some idea of changing those properties.

As for creating a new style equivalent to an existing style, this is very rarely done. Why? You simply use the existing style. Plus trying to recreate a new style by determining the properties of an existing one is rather pointless. You can either use the existing one, or if you really need to (and I can not think of a single reason for it), you can COPY the existing style.

It is great that you want to learn about styles - they are the main basic structure of Word.

I have to agree with macropod re: any .bas file. Sending a .bas file is VERY unusual. I doubt most people would even know what to do with one.

MacroShadow
04-02-2012, 10:33 PM
then you coming back and saying it's no goo
On the contrary, it is good, I've learned something new, how to find properties of styles, and therefore how to define the properties for a new style.


If by manipulating you mean changing them. USING styles via VBA, sure. But not changing them, no.
I mean setting them.

fumei
04-03-2012, 01:22 AM
Setting them? What does that mean?

Again, it is quite rare to create styles from scratch using VBA. I never do. I seriously doubt I could write code faster than simply making a new style. And the chance of making an error in writing the code would be MUCH higher than creating a style the normal way.

So if by "setting them", you mean using VBA to create styles...sure go ahead as a learning experience if you want. But it is not an efficient use of VBA.

Frosty
04-03-2012, 12:06 PM
There are a couple of scenarios where you might use VBA to "create" a style which doesn't exist in a document. One example:
Running a footer macro, which determines the footer info, where to put it... and then apply a custom "MyFooter" style to that footer info.

If the document you're running this footer macro on doesn't have the MyFooter style, you would need to make sure the document has that style before you attempt to apply the style.

Whether you hard-code all the values of that style within VBA (and thus, truly create the style by using something like ActiveDocument.Styles.Add) or allow a more flexible approach by copying the style from a different template (by using something like Application.OrganizerCopy) is a question of approach.

I think it is good practice to separate data from functionality... so I would always recommend against hard-coding style properties inside VBA. Although the practice of getting a style into a document which doesn't have it is certainly something you would code for.

fumei
04-03-2012, 01:27 PM
of approach.

I think it is good practice to separate data from functionality... so I would always recommend against hard-coding style properties inside VBA.

I would strongly agree with that.

Paul_Hossler
04-09-2012, 05:07 AM
MacroShadow -- until I find the macro, you can go to Print, and in 'Print what' select 'Styles'. You get a new document that looks like this

Paul

macropod
04-09-2012, 06:46 AM
An extremely simple method of copying new Styles from a source document to a set of target documents is to create a series of paragraphs using those Styles in the source document (if they're paragraph Styles, the paragraphs don't need any text), add the following macro to the source document, open the source document and all the target documents, then run the macro.
Sub AddStyles()
Application.ScreenUpdating = False
Dim Rng As Range, i As Long
If Windows.Count = 1 Then Exit Sub
ThisDocument.Range.Copy
For i = 1 To Windows.Count
Windows(i).Activate
Set Rng = ActiveDocument.Characters.Last
With Rng
.InsertAfter vbCr
.Collapse wdCollapseEnd
.Paste
.Delete
End With
Next
Set Rng = Nothing
Application.ScreenUpdating = True
End Sub
Keeps the code and data completely separate.

fumei
04-09-2012, 11:51 AM
Nice one Paul.

MacroShadow
04-10-2012, 11:08 AM
Thank you both! This has been quite some learning experience.

PReinie
12-21-2012, 03:25 PM
I too have a need to extract all attributes/properties of a style, back to and including the main based-on style(s).

I've found that when sending another person a document, when they open it the style I've created it doesn't look the same due to Word's cascading styles (as fumei said "Another issue with using run amok parent/child styles." where the other user has modified a style my style was based on.

As near as I can tell, the only way to make your style look proper for any other user is to define all properties of "my" style.

For instance, even if you were simply using the default Body Text style (which as near as I can tell is the default Normal style with 6pt after the paragraph) and the user you send your document to has (and this DOES happen) changed some property of their Normal style (which user's can't delete, but can rename and change properties thereof) then what you sent them won't look "correct".

This isn't achademic or for security reasons, isn't this what happens?

My idea is to define all properties of a style and give it a hopefully unique name then I would feel safe sending a document to a user (or using Organizer to copy styles to another document).

If the target user wants to change the style for a different font or whatever, that's up to them.

The VBA for Word problem I'm having is finding all the properties of a style independent of the version of Word. In other words, I'd like the VBA code to work for a style defined in any version of Word that employs styles without having to know each property's name. (I'm assuming Word 2010 might have more properties than Word 2003 which I'm currently using, and who knows what will happen in the future?)

The only other way I've found to do this takes a fair amount of time is Allen Wyatt's word.tips .net/T000095_Duplicating_Styles_without_Dependency .html
(Sorry about the not-quite-a-link - I don't have enough posts yet to put links in.)

The psuedo code: For each "active" property in a given style copy the property to a new style (name provided by code or entry box).

By "active" I mean all properties in effect including the based on styles' properties if not overridden by a higher-level style (the highest level property being active).

So although this thread is marked Solved, I do not really see a solution. Should I open a new thread? Is there a better way to solve this?

macropod
12-21-2012, 03:42 PM
Check that your document doesn't have the 'automatically update document styles' option set.

Other than that, yes, you should start a new thread as your query concerns a different issue from that which this thread discusses.