For Each Cycle

S

Sasa

I gor problem. I cannot define For Each Cycle to iteriate
through each Heading style in document. I found
HeadingStyles class in object browser but when I put it
into my definition it wont work.

Please help.
If You can also wrote correct definition, I'll be
apreciated.

Thank you.
 
J

Jonathan West

Hi Sasa,

Could you describe in a bit more detail what you are trying to achieve?

Are you trying to modify each of the heading styles, or each of the
paragraphs in the document that is formatted using a heading style?

--
Regards
Jonathan West - Word MVP
MultiLinker - Automated generation of hyperlinks in Word
Conversion to PDF & HTML
http://www.multilinker.com
 
M

Mark Tangard

Hi Sasa,

The HeadingStyles collection isn't a collection of the Word
styles that begin with 'Heading'; it's composed of any added
(non-built-in) heading styles used to generate a table of
contents.

To iterate through the standard heading styles you'll need
to cycle through the styles collection and filter out the
others:

Dim s As Style
For Each s In ActiveDocument.Styles
If Left(s.NameLocal, 7) = "Heading" Then
'
' <----your code here
'
End If
Next s
 
S

Sasa

I am trying to make For cylcle which will cycle as many
times as is the count of styles called "Heading 1-..." in
document.

I think something like:

For i = 1 to ActiveDocument.HeadingStyles.Count
..
..
..
Next

But it seems to not be working.
Thank you for your interest.
 
M

Mark Tangard

This posting has already had 2 replies, in this very group,
on this very thread.

There seems to be an epidemic of people who post questions
and don't bother to read the replies that people take time
to compose and post.

Sasa, if you want help here, you will need to pay much closer
attention to the activity in the group.
 
S

Sasa

Sorry, my mistake. For Each cycle was not so helpful to
solve the problem. I thought that For Cycle is much
helpful.

Here is the code but, as I said the For cycle is not
working. Thats why I need your help to solve this.

Dim i As Integer
Dim Hdng1 As Style
Set Hdng1 = ActiveDocument.Styles("Heading 1")
Dim Hdng2 As Style
Set Hdng2 = ActiveDocument.Styles("Heading 2")
Dim Hdng3 As Style
Set Hdng3 = ActiveDocument.Styles("Heading 3")
Dim Hdng4 As Style
Set Hdng4 = ActiveDocument.Styles("Heading 4")


For i = 1 To "HeadingStyles.Count"
Selection.GoTo What:=wdGoToHeading, Which:=wdGoToNext,
Count:=1, Name:=""

Select Case Selection.Style
Case "Heading 1"
Selection.Style = Hdng2
Case "Heading 2"
Selection.Style = Hdng3
Case "Heading 3"
Selection.Style = Hdng4
Case "Heading 4"
Selection.Style = Hdng5
End Select
Next

My regards.
Sasa
 
M

Mark Tangard

What is the goal of the macro? Are you just trying to
reassign each Heading X paragraph to a new style HdngX?

If that's it, this should work (note: untested code):

Dim p as Paragraph, newstyle as String
For Each p In ActiveDocument.Paragraphs
If Left(p.Style.NameLocal, 7) = "Heading" Then
newstyle = "Hdng" & Right(p.Style.NameLocal, 1)
p.Style = ActiveDocument.Styles(newstyle)
End If
Next p
 

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