For Each error

S

Sammy

Hi,

I have the following macro which looks for any paragraph formatted with
Heading 2 style and then underlines the text from the beginning of the
paragraph up until the first period (doesn't underline the period). For some
reason I get a compile error that says "Next without For" but as you can see
I have "Next". I'm trying to test my code to see if this works but I can't
get past this error. Thanks for any help.

Sub ProcessLev2()

Dim myPara As Paragraph

For Each myPara In ActiveDocument.Paragraphs

If myPara.Style = "Heading 2" Then

Selection.Collapse Direction:=wdCollapseStart
Selection.Extend
Selection.Extend Character:="."
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Font.Underline = wdUnderlineSingle

Selection.MoveLeft Unit:=wdCharacter, Count:=1

Next myPara
End Sub
 
M

macropod

Hi Sammy,

You need to insert an 'End If' before the 'Next myPara'. As it stands, the macro treats the 'Next myPara' statement as part of an
as-yet unfinished If test.

Cheers
 
G

Gregory K. Maxey

Sammy,

You are missing and "End If" statement.

You could also use a range instead of the selection:

Sub ProcessLev2()
Dim myPara As Paragraph
Dim oRng As Range
For Each myPara In ActiveDocument.Paragraphs
If myPara.Style = "Heading 2" Then
Set oRng = myPara.Range
oRng.Collapse wdCollapseStart
oRng.MoveEndUntil Cset:=".", Count:=wdForward
oRng.Font.Underline = wdUnderlineSingle
End If
Next
End Sub
 

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