PDA

View Full Version : List out Styles



Rakesh
03-12-2011, 12:07 AM
Hi Guys,

I had a Word Document which contains multiple Styles.

How to list out the styles used in the document using VB script.

If anyone knows please help me.

I have attached a sample file for your reference.


Thanks,
Rakesh

Frosty
03-12-2011, 11:02 AM
Public Sub SampleMyStyles
dim mySty as Style
For each mySty in ActiveDocument.Styles
selection.typetext mysty.Name & vbcr
Next
End Sub

Frosty
03-12-2011, 11:09 AM
Actually, let me back up.

1. That looks like client data in your sample doc. You may want to XXX the names and financials from that. Unless you made all of that up.

2. Are you looking just to see what styles are actually in use in the document? The code I posted above will type out all the styles which exist in the document... additional filters would need to be described by you. There is an .InUse property (or something like it), but my recollection is that it is actually a record of all the styles which have *ever* been used in the document, rather than which styles are currently in use.

I believe the same applies for the native functionality of showing which styles are "in use" in the document.

But maybe a little more explanation of what you want to do with this script would help give you a better answer.

Rakesh
03-12-2011, 09:24 PM
Hi Frosty,

Thanks for your kind reply.

While I run the coding it shows the following error with .Name Highlited
selection.typetext mysty.Name & vbcr

Compile error:
Method or data member not found

Why I need the Styles List?
I like to insert some text at the starting of the para based on the styles.

For eg., (assume that the below three lines are the style SOI_H1, SOI_H2, and SOI_H3)

The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.

Based on the style name, the inserting text may vary. If I know the style Names I will replace the text with the below sample coding.

With ActiveDocument.Content.Find
.Style = ActiveDocument.Styles("SOI_H1")
.Text = "([!\n]@[\n])"
.Replacement.Text = "Head1:\1"
.Forward = True
.Format = True
.MatchWildcards = True
.Execute replace:=wdReplaceAll
End With

Output will be like below:

Head1: The quick brown fox jumps over the lazy dog.
Head2: The quick brown fox jumps over the lazy dog.
Head3: The quick brown fox jumps over the lazy dog.

Thanks,
Rakesh

macropod
03-13-2011, 04:09 AM
Hi Rakesh,

To get Frosty's macro to run, you'll need to change:
mySty.Name
to:
mySty.NameLocal

For what you're describing, you probably need something along the lines of:
Sub Demo()
Dim oSty As Style, StrStyList As String, StrStyles As String, i As Integer, StrSty As String
StrStyles = "SOI_H1,SOI_H2,SOI_H3"
StrStyList = ","
With ActiveDocument
For Each oSty In .Styles
StrStyList = StrStyList & oSty.NameLocal & ","
Next
With .Content.Find.Forward = True
.Format = True
.MatchWildcards = True
.Text = "([!^13]{1,}^13)"
'To change the replacement Head number, comment out the next line
.Replacement.Text = "Head1: \1"
For i = 0 To UBound(Split(StrStyles, ","))
StrSty = Split(StrStyles, ",")(i)
If InStr(StrStyList, "," & StrSty & ",") > 0 Then
'To change the replacement Head number, uncomment the next line
'.Replacement.Text = "Head" & Right(StrSty, 1) & ": \1"
.Style = StrSty
.Execute Replace:=wdReplaceAll
End If
Next
End With
End With
End Sub

Rakesh
03-13-2011, 09:58 PM
Hi macropod,

As per your instruction I have changed
mySty.Name
to:
mySty.NameLocal

But it list All Styles in the document, Wherever I need only the style used in the Document

If only 2 Styles used in the Document, that 2 style Names to be list out.


While I run your coding it shows the error in the following line
With .Content.Find.Forward = True
Error as
Compile error:
With object must be user-defined type, Object, or Variant

Once I changed the line as below it worked fine
With ActiveDocument.Content.Find
.Forward = True

Now the Output is like this
Head1: The quick brown fox jumps over the lazy dog.
Head2: The quick brown fox jumps over the lazy dog.
Head3: The quick brown fox jumps over the lazy dog.

As I told you in the previous post, the inserting text may vary based on style name.

If I need the output like the below , what should I need to do. Please guide me.

Head1: The quick brown fox jumps over the lazy dog.
Tip2: The quick brown fox jumps over the lazy dog.
Level3: The quick brown fox jumps over the lazy dog.

Thanks
Rakesh

macropod
03-13-2011, 11:24 PM
Hi Rakesh,
Re:

But it list All Styles in the document, Wherever I need only the style used in the Document
you could reduce the listing by changing Frosty's code to:
Public Sub SampleMyStyles()
Dim mySty As Style
With ActiveDocument
For Each mySty In .Styles
If .Styles(mySty.NameLocal).InUse = True Then
Selection.TypeText mySty.NameLocal & vbCr
End If
Next
End With
End Sub
However, even this is likely to generate a list with custom Styles that haven't been applied to any text. If you really want to limit the output to Styles that have been applied to some text, you'd have to do a 'Find' for the Style in all the document's story ranges and then only output the name if the 'Find' is succesful.

Re:

With .Content.Find.Forward = True
I'm not sure what happened to that bit of code - it should have appeared as:

With .Content.Find
.Forward = True

Rakesh
03-14-2011, 01:54 AM
Hi Paul Edstein,

Thanks for your valuable Time spent for me.

Cheers,
Rakesh