Get all the styles

P

Patrick386

Hello!

How come that when I loop on a document's styles, I do not find all the styles as they are displayed in the Styles and Fomratting pan?
for instance if the user applyes the style "Normal" and then changes the alignment or the font color, the pan displays:
"Normal"
"Normal + Center"

but when I make loop on all the styles existing in the document, I only see "Normal"?
For i=1 to ThisDocument.Styles.Count
msgBox ThisDocument.Styles(i).NameLocal
Next i

How can get all of them?

Thanks in advance for any suggestion!
 
J

Jay Freedman

"Normal + Center" is _not_ a style, in the sense that it isn't a
member of the Styles collection. It's the result -- in the user
interface only -- of having the "Keep track of formatting" option set.
That option causes Word to display each application of direct
formatting as a separate choice in the panel so you can use the
"select all instances" button. VBA doesn't know anything about it.
 
P

Patrick386

Ok, but if Word can display it in the Styles & Formating pan, I should be
able to do it as well..
I understand that it's not a style. but then, is there a collection of the
format tracking? because it's automatically displayed, it must be stored
somewhere..

Thanks again
 
J

Jay Freedman

It may be "stored somewhere" but the "somewhere" is not accessible to VBA;
or it may be generated on the fly in the user interface. The sad fact is
that there are a great many things that Word obviously "knows" but doesn't
make available to VBA. This is one of them.
 
P

Patrick386

I'm still not sure that you cannot have the list of the format changes. it's
not because I can't get it, that it doesn't exist.. it would be the 1st time
that I would see that
Do you have an example of a function that you have in Word and not in Vba?
 
K

Klaus Linke

You could get the strings that are displayed from the Styles control:

Dim myC As CommandBarComboBox
Dim i As Long
Set myC = CommandBars("Formatting").Controls("&Style:")
With myC
For i = 1 To .ListCount
Debug.Print myC.List(i)
Next i
End With

(not sure if "&Style:" is the correct caption... You can use the index of the styles control on the toolbar instead.)

It's still a good distance from getting those strings to translating them to something you can use in your code to actually, say, search for that combination of style and formatting.
Plus, it'll only work in one specific language version (... a drawback for me, since I work with both the Engish and German version on different machines).

Like you, I'd regard the ability to get directly at these formatting pseudo-styles as a major improvement, and have asked for it a few times.

As far as I understand it, they are stored pretty much the same way as styles are stored.
On the other hand, Word itself has a terrible time selecting "all instances" of some formatting.

On a large document, Word typically spends minutes analyzing the doc, whereas when I search for that style and formatting directly, it'll find it much, much faster.
So I think there's some pretty basic problem either with my understanding of how Word works, or with the implementation.
If formatting is really stored much the same way as a style, Word should be able to locate all instances where that formatting was used very quickly.
On the other hand, the styles and formatting pane often lists styles and manual formatting that only existed in the documents hours ago.
So it seems as if Word may not in fact have any way to look the list of "pseudo-styles" up quickly, or at least not reliably.

Regards,
Klaus
 
K

Klaus Linke

Set myC = CommandBars("Formatting").Controls("&Style:")

or using the control's Id:
Set myC = CommandBars.FindControl(ID:=1732)

Klaus
 
P

Patrick386

Hi Klaus!

Great thanks for your help and the time you've spent on the question!!
That's true about the time that Word takes to list the fomatting list.. I
was wondering if may be, it doesn't scann the whole document and look for
'modified' styles (formats) and keep them in memory and then keep traking of
it if the user makes any changes.

but the liste is displayed so fast once the document open. it is displayed
in the pan, in the Find & Replace: when you choose a style, it shows
imediatly of what the format or style is composed of..

anyway thanks and good luck with the styles
Auf Wiedersehen


Klaus Linke said:
Set myC = CommandBars("Formatting").Controls("&Style:")

or using the control's Id:
Set myC = CommandBars.FindControl(ID:=1732)

Klaus
 
K

Klaus Linke

Yes, something like that probably, tracking back through all the changes that were made to consolidate the list.
But my feeling is that someone just implemented it in a hurry, and that it shouldn't need to take nearly as long.

Which is a shame, since if the pane often lists formatting that does not really exist, it diminishes the value of the pane.
And if you have to wait a few minutes for Word to analyze the document and select all instances of some formatting, people will avoid this in principle great feature.

I've never actually tried to exploit the list of (style+manual) formatting that can be acquired with the macro I posted. Now that you've reminded me of the possibility, maybe I'll give it a run.

So... Danke schön :)
and good luck to you, too
Klaus
 

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