Need help with a looping macro

J

Jerry Keenan

I need help with writing a "Do Until" macro. I am inserting a text file
(that changes daily) and need to insert spaces at fixed lengths in each line.
I want to start on the first line and stop the macro at the end of the file.
The macro is:

Sub Insert_Spaces()
'
' Insert_Spaces Macro
' Macro recorded 9/7/2005 by keenpar
'
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeText Text:=" "
Selection.MoveRight Unit:=wdCharacter, Count:=7
Selection.TypeText Text:=" "
Selection.MoveRight Unit:=wdCharacter, Count:=16
Selection.TypeText Text:=" "
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.HomeKey Unit:=wdLine
End Sub

Any help will be most appreciated.
Thanks
 
E

Edward Thrashcort

It depends how your document is structured.
Are lines separated by paragraph marks?

If so, you could continue to use the selection object with

Dim aPara as Paragraph
For Each aPara In ActiveDocument.Paragraphs
aPara.Select
'ETC
Next


Alternatively you could use
Do.. Loop until ...
in which case it may be better to use a Range object and test ot for end of
Document.

There are other more elegant methods, I'm sure

Eddie
 
J

Jerry Keenan

Thank you Eddie,

I should have told you I am a real novice at this. If you can't record the
macro, I have a problem. I'm sure your solutions would work, but I need more
hand holding. I would like to see both of your solutions. Here is a sample
file. It does have paragraph marks. Can you show me the macro you would use?

5094765800000000002602431790055555
5091556200000000002554151790000004
5004765800000000002605331790000005
5001556200000000002559151790000006
5000600000000000002572271910000009
5000300000000000002577881910000010
5000600000000000002575271910000011
5000300000000000002575881910000012
5000600000000000002572271910029292
5000300000000000002577881910000010
5000600000000000002575271910000011
5000300000000000002575881910000012
5000600000000000002572271910000009
 
E

Edward Thrashcort

OK, I'll make it fit with what you have recorded

Sub PushSpaces()
Dim aPara As Paragraph
For Each aPara In ActiveDocument.Paragraphs
aPara.Range.Select
With Selection
.Collapse
.MoveRight Unit:=wdCharacter, Count:=1
.TypeText Text:=" "
.MoveRight Unit:=wdCharacter, Count:=7
.TypeText Text:=" "
.MoveRight Unit:=wdCharacter, Count:=16
.TypeText Text:=" "
.MoveDown Unit:=wdLine, Count:=1
.HomeKey Unit:=wdLine
End With
Next
End Sub



Eddie
 
J

Jerry Keenan

Thanks again,

I'm traveling this evening and will try it when I get to Savannah. I really
appreciate your help.
 
H

Helmut Weber

Hi Jerry,

like this:

Sub Macro7()
Dim oPrg As Paragraph
Dim rTmp As Range
For Each oPrg In ActiveDocument.Paragraphs
Set rTmp = oPrg.Range
rTmp.End = rTmp.Start + 1
rTmp.InsertAfter " "
rTmp.End = rTmp.End + 7
rTmp.InsertAfter " "
rTmp.End = rTmp.End + 16
rTmp.InsertAfter " "
Next
End Sub

Make sure, that there is no empty paragraph nowhere.
Not at the doc's start and not at the doc's end.

One could even do it without rTmp.
But that's a matter of style.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Jerry Keenan

Works great. Thanks so much. Hope your weather is as good in Bavaria as it
is in Savannah Georgia today.
 

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