Case SearchRange

L

LEU

I am trying to find all of my Heading styles and move the right indent. I
can't get this macro to work. What am I doing wrong?

Dim SearchRange As Range
Set SearchRange = ActiveDocument.Range
With SearchRange.Find
While .Execute
Select Case SearchRange.Style
Case "Heading 1", "Heading 2", "Heading 3", "Heading 4", _
"Heading 5", "Heading 6", "Heading 7"
SearchRange.ParagraphFormat.RightIndent = InchesToPoints(0.4)
Case Else
'Do Nothing
End Select
Wend
End With
 
H

Helmut Weber

Try:

' ....................
With SearchRange.Find
.Text = Chr(13)
' ....................

as you don't search for anything,
you'll find nothing.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
J

Jay Freedman

You misunderstand how the Range.Find object works. First you set all
the necessary properties of the .Find object -- .Text, .Style,
..Format, .Wrap, and so on -- and then you call .Execute. In your
macro, you never set any properties of SearchRange.Find, so calling
..Execute did nothing at all.

When you want to search for a batch of styles, one way is to put them
into an array, and then loop through the array to search for each one
in turn:


Dim SearchRange As Range
Dim StyleList As Variant
Dim i As Integer

StyleList = Array("Heading 1", "Heading 2", "Heading 3", _
"Heading 4", "Heading 5", "Heading 6", "Heading 7")

For i = 0 To UBound(StyleList)
Set SearchRange = ActiveDocument.Range
With SearchRange.Find
.Text = ""
.Style = ActiveDocument.Styles(StyleList(i))
.Format = True

While .Execute
SearchRange.ParagraphFormat.RightIndent = _
InchesToPoints(0.4)
Wend
End With
Next i

A related way is to put all the search items into a collection. Then
you can use a For Each loop instead of an explicit loop counter:

Dim SearchRange As Range
Dim StyleList As New Collection
Dim thisStyle As Style

With StyleList
.Add ActiveDocument.Styles("Heading 1")
.Add ActiveDocument.Styles("Heading 2")
.Add ActiveDocument.Styles("Heading 3")
.Add ActiveDocument.Styles("Heading 4")
.Add ActiveDocument.Styles("Heading 5")
.Add ActiveDocument.Styles("Heading 6")
.Add ActiveDocument.Styles("Heading 7")
End With

For Each thisStyle In StyleList
Set SearchRange = ActiveDocument.Range
With SearchRange.Find
.Text = ""
.Style = thisStyle
.Format = True

While .Execute
SearchRange.ParagraphFormat.RightIndent = _
InchesToPoints(0.4)
Wend
End With
Next

Finally, it would be _much_ simpler -- no macro at all -- to modify
the definitions of the heading styles. That would affect all the
headings in the document at once, including headings that you add to
the document later (which the macro methods wouldn't affect).

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 

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

Similar Threads

Range headings 0
Converting a word 3
Text replacement 5
Styles help 16
InputBox Help 4
Outline Numbering Question 9
Heading question 5
Unexplained failure of wdFindStop at end of search 0

Top