Move to next line

  • Thread starter Dominique Feteau
  • Start date
D

Dominique Feteau

I have a program that is supposed to look for some text, move to the next
line and then insert a continuous page break. Problem is I can't get it to
move to the next line. The line I'm using "MoveDown Unit:=wdLine" gives me
an error. Any suggestions??

Dominique

Sub Macro2()
With ActiveDocument.Content.Find
.Text = "END OF REPORT"
Do While .Execute(Forward:=True) = True
With .Parent
If .End = ActiveDocument.Content.End Then
.MoveDown Unit:=wdLine
.InsertBreak Type:=wdSectionBreakContinuous
Exit Do
Else
.MoveDown Unit:=wdLine
.InsertBreak Type:=wdSectionBreakContinuous
.Move Unit:=wdParagraph, Count:=1
End If
End With
Loop
End With
End Sub



--- news://freenews.netfront.net/ - complaints: (e-mail address removed) ---
 
J

Jay Freedman

The major problem is the use of ActiveDocument.Content as the object
being manipulated by the code. That object cannot be moved, resized,
or otherwise changed -- it just represents the whole document.

If you want to use .MoveDown Unit:=wdLine then you must use the
Selection object instead.

Once that problem is avoided, you'll find that there are a few less
serious problems that will make the macro produce wrong results. For
example, if you search for any string that doesn't end with a
paragraph mark (represented in VBA by the constant vbCr), then the end
of the found object can't ever equal the end of the document, because
the document always has a final paragraph mark.

Another: If you find the string and then just move down one line, and
if the next line has text in it, the insertion point lands somewhere
in the middle of the line instead of at its beginning. You need to
collapse the Selection to get it to go to the right place. There are a
couple of other such tweaks in the following code:

Sub Macro2a()
Selection.HomeKey wdStory
With Selection.Find
.Text = "END OF REPORT"
Do While .Execute(Forward:=True) = True
With .Parent
If .End = ActiveDocument.Content.End - 1 Then
.Collapse wdCollapseEnd
.InsertBreak Type:=wdSectionBreakContinuous
Exit Do
Else
.Collapse wdCollapseStart
.MoveDown Unit:=wdLine
.InsertBreak Type:=wdSectionBreakContinuous
.Move Unit:=wdParagraph, Count:=1
End If
End With
Loop
End With
End Sub

--
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

Top