PDA

View Full Version : How to get all the applied Paragraph Styles of a document?



gandhikumar
02-27-2012, 01:52 AM
Hi all,
I want to know all the paragraph styles applied in a particular MS Word 2007 document. I tried "<StyleObject>.InUse", but it doesn't meet my requirement. I exactly want the styles that are viewable in the Style Area Pane when in Draft View.

Frosty
02-27-2012, 02:19 PM
This is trickier than it appears. Where would you want the output? Ultimately, to really be accurate, you will need to have a macro which:
1. Goes through each paragraph in the document, checks the applied style
2. Adds that style to a list, if the name doesn't already appear in the list.
3. Outputs that list... somewhere.

As a demo, here is code which simply puts the list into a message box... This is a "quick and dirty" function, and may or may not be what you ultimately need... since it doesn't deal with sorting the results or anything else. And since it separates the returns with a vbCr and returns in a messagebox, if you have a lot of styles, this will not be a great return.

So-- just a proof of concept for you. Hope this helps.

Sub TheRealAppliedStyles()
Dim colStyleNames As Collection
Dim oPara As Paragraph
Dim oStyle As Style
Dim sRet As String
Dim i As Integer

'create a new instance of the collection
Set colStyleNames = New Collection

'cycle through all the paragrphs in the main body of the document
'NOTE: this won't include headers/footers/footnotes/end notes/text boxes
For Each oPara In ActiveDocument.Paragraphs
'set the style -- don't really need this, but easier to have a variable for troubleshooting
Set oStyle = oPara.Style

'since the "key field" of a collection has to be unique, this will error
'if you are trying to add a style name which already exists
On Error Resume Next
colStyleNames.Add oStyle.NameLocal, oStyle.NameLocal
On Error GoTo 0
Next

'now build the return string
For i = 1 To colStyleNames.Count
sRet = sRet & colStyleNames(i) & vbCr
Next

MsgBox sRet
End Sub

gandhikumar
02-29-2012, 01:00 AM
Hello Frosty,
Thanks for your reply. Is there any way obtaining those in Style Area of Draft View.

Frosty
02-29-2012, 06:15 AM
What version of word are you using? Is it word 2007? I don't think it is possible if you are using word 2003 to view the word 2007 document

gandhikumar
02-29-2012, 07:48 PM
It's MS Word 2007.

Frosty
03-01-2012, 11:53 AM
I guess I'm not 100% sure what you want. When you're in Draft View, there is a setting to display (on the left), whatever paragraph style is applied. That is the setting under Word Options > Advanced > Display > Style area pane width in Draft and Outline views. Set that to 1" and you will see the styles applied to the paragraphs.

If you want to show the "real" InUse styles (i.e., not Word's concept of "InUse" which is really an "EverUsed" functionality) in the actual Styles Pane (which appears, by default, on the right side of the document when you click the Launcher of the Styles group), then you would need to have code which
1. Analyzes each paragraph in your document, building the list of currently applied styles (the code I previously wrote)
2. Then goes through every style available in the document, and changes the .Visibility property and .UnhideWhenUsed properties as appropriate.

I've left options in the code, because the actual settings of the values is somewhat counter-intuitive to the property names. Uncomment what you want to use... I've left it as "hide always".

If you want to use "hide until used" settings, then you will be left with the same problem-- that the "In Use" concept is fundamentally different than what we think it should be.

Sub TheRealAppliedStyles()
Dim colStyleNames As Collection
Dim oPara As Paragraph
Dim oStyle As Style
Dim i As Integer

'create a new instance of the collection
Set colStyleNames = New Collection

'cycle through all the paragrphs in the main body of the document
'NOTE: this won't include headers/footers/footnotes/end notes/text boxes
For Each oPara In ActiveDocument.Paragraphs
'set the style -- don't really need this, but easier to have a variable for troubleshooting
Set oStyle = oPara.Style

'since the "key field" of a collection has to be unique, this will error
'if you are trying to add a style name which already exists
On Error Resume Next
colStyleNames.Add oStyle.NameLocal, oStyle.NameLocal
On Error GoTo 0
Next

'hide all styles in the document
For Each oStyle In ActiveDocument.Styles
'this is a "hide until used" setting
' oStyle.Visibility = True
' oStyle.UnhideWhenUsed = True

'this is a "hide always" setting
oStyle.Visibility = True
oStyle.UnhideWhenUsed = False

'this is a "show always" setting
' oStyle.Visibility = False
' oStyle.UnhideWhenUsed = False

Next
'now cycle through the collection, and change view these only
For i = 1 To colStyleNames.Count
Set oStyle = ActiveDocument.Styles(colStyleNames(i))
oStyle.Visibility = False
oStyle.UnhideWhenUsed = False
Next

End Sub

gmaxey
03-01-2012, 03:42 PM
Hi all,
I want to know all the paragraph styles applied in a particular MS Word 2007 document. I tried "<StyleObject>.InUse", but it doesn't meet my requirement. I exactly want the styles that are viewable in the Style Area Pane when in Draft View.

This might help: http://gregmaxey.mvps.org/word_tip_pages/style_report_addin.html

Frosty
03-01-2012, 03:50 PM
Hey Greg!

That's a neat looking addin (read through the page quickly). I haven't looked actually looked at it yet, but what does it do in the following scenario:

1. Customized template, with custom settings for Body Text style (a built-in MS word style).
2. No paragraphs with Body Text applied.
3. Run the style report, and user chooses to "delete" Body Text.

If someone else comes along later and applies Body Text within that document, do they retain the (original) custom formatting, or is Body Text applied with the MS default formatting for Body Text?

gmaxey
03-01-2012, 03:59 PM
Jason,

The latter. If the unused body text style is deleted and then applied later via styles and formattign the default formatting is applied.

I have a feeling if it can be made better I am going to soon know about it ;-)

Frosty
03-01-2012, 06:13 PM
Nope. Nothing "better" to do. I was just curious what functionality you'd built in.

Although, now that I think about it... *grin*... I suppose, if you wanted, you could, before deleting a built-in style, verify whether the user simply wants to hide that built-in style or actually reset it back to defaults (as well as hide it). Since you can never actually delete a built-in style... (in that, if you are showing "All Styles", it will show up whether you've performed .Delete or not)

I think it's just important that people know there is a difference between deleting an unused custom style from a document, and deleting an unused built-in style in a document.

But, I do have a couple of fish to fry at the moment... (and some already in the fire), so perhaps my brainstorm is completely off-base.

- Jason

gandhikumar
03-02-2012, 02:17 AM
Hi Frosty,
Again many thanks for your reply. I actually want to know the code for the displayed styles that you specified in your reply as below:

"When you're in Draft View, there is a setting to display (on the left), whatever paragraph style is applied."

If there is no shortcut then I will use your last said macro.

Talis
03-04-2012, 10:12 AM
"When you're in Draft View, there is a setting to display (on the left), whatever paragraph style is applied."

If there is no shortcut then I will use your last said macro.

So do you simply require a subroutine which brings up Draft View and shows the lefthand-side Style pane? If so:

Sub StylesInUse()
ActiveWindow.View.Type = wdNormalView 'May need to be wdDraftView in Word 2007
ActiveWindow.StyleAreaWidth = CentimetersToPoints(2.5)
End Sub
and assign a shortcut key to run the macro.

Frosty
03-04-2012, 10:33 AM
And, in general, when there are simply settings you are looking for-- just record a macro and change the setting. In most cases, you will be able to find the code you were looking for.

macropod
03-04-2012, 08:59 PM
The simplest way to find which styles are in use is to set up a loop that goes through all Styles, uses Find to look for them, and record which ones are found:
Sub GetActiveStyles()
Application.ScreenUpdating = False
Dim RngStory As Range, oSty As Style, StrType As String, StrStyles As String
With ActiveDocument
For Each oSty In .Styles
For Each RngStory In .StoryRanges
With RngStory.Find
.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Style = oSty.NameLocal
.Forward = True
.Wrap = wdFindStop
.Format = True
.Execute
If .Found Then
Select Case oSty.Type
Case wdStyleTypeCharacter: StrType = "Character"
Case wdStyleTypeList: StrType = "list"
Case wdStyleTypeParagraph: StrType = "Paragraph"
Case wdStyleTypeTable: StrType = "Table"
End Select
StrStyles = StrStyles & oSty.NameLocal & " (" & StrType & ")" & vbCr
Exit For
End If
End With
Next RngStory
Next oSty
End With
MsgBox StrStyles
Application.ScreenUpdating = True
End Sub