Move cursor to the end of a line, insert text, loop through each r

J

Jak

MS Office 2003

Hi All

I am putting together a macro and I am really stuck on how to get the cursor
to move to the end of a line in Word. I have set the cursor to go to the
begining of line 1 and enter some text ie.

Selection.GoTo What:=wdGoToLine, Which:=wdGoToFirst, Count:=1, Name:=""
Selection.Find.ClearFormatting
Selection.TypeText Text:="myArray = Array("

I now need to go to the end of line 1 and then insert some text and then
move down a line and continue to insert the text ie

Selection.TypeText Text:=", _"
Selection.MoveDown Unit:=wdLine, Count:=1

The MoveRight ie

Selection.MoveRight Unit:=wdWord, Count:=30, Extend:=wdExtend

I have tried but length of the line may vary and the code would fail. Any
help with a loop that finds the end of line 1, enters text and then moves 1
line down and inserts the text until the last line is reached is what I am
hopeing to achieve.

Any assistance with this would be greatly appreciated.

Jak
 
S

Shasur

Hi Jak

Here is a hint

Sub Move_Line()


'Selection.GoTo What:=wdGoToLine, Which:=wdGoToFirst, Count:=1, Name:=""
' Selection.Find.ClearFormatting
' Selection.TypeText Text:="myArray = Array("
'
'
'Selection.TypeText Text:=", _"
Dim TotalLines As Long
Dim iLine As Long
Selection.HomeKey wdStory, wdMove

TotalLines = ActiveDocument.ComputeStatistics(wdStatisticLines, False)
For iLine = 1 To TotalLines
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.MoveStart wdLine
Selection.MoveLeft wdCharacter, 1
Selection.TypeText "<End Of Line>"
Selection.MoveRight wdCharacter, 1
If Selection.Bookmarks.Exists("\EndOfDoc") Then Exit Sub
Next iLine


'Selection.MoveRight Unit:=wdWord, Count:=30, Extend:=wdExtend


End Sub

Cheers
 
J

Jak

Hi Shasur

Thanks for the code. I had a slight problem in that it added the text
insertions every other line. I removed the following line:

Selection.MoveDown Unit:=wdLine, Count:=1

and it added the text to each line as required.

Many thanks for the quick reply and code, your a star.

Jak
 

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