Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / September 2005

Tip: Looking for answers? Try searching our database.

Need help with a looping macro

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jerry Keenan - 24 Sep 2005 15:09 GMT
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
Signature

Jerry Keenan

Edward Thrashcort - 24 Sep 2005 18:21 GMT
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
Jerry Keenan - 24 Sep 2005 21:54 GMT
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

Signature

Jerry Keenan

> It depends how your document is structured.
> Are lines separated by paragraph marks?
[quoted text clipped - 15 lines]
>
> Eddie
Edward Thrashcort - 24 Sep 2005 22:43 GMT
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
Jerry Keenan - 24 Sep 2005 22:52 GMT
Thanks again,

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

Jerry Keenan

> OK, I'll make it fit with what you have recorded  
>
[quoted text clipped - 17 lines]
>
> Eddie
Jerry Keenan - 25 Sep 2005 14:10 GMT
Very cool.  Thanks so much and have a great day.
Signature

Jerry Keenan

> OK, I'll make it fit with what you have recorded  
>
[quoted text clipped - 17 lines]
>
> Eddie
Helmut Weber - 24 Sep 2005 23:31 GMT
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.

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Jerry Keenan - 25 Sep 2005 14:15 GMT
Works great.  Thanks so much.  Hope your weather is as good in Bavaria as it
is in Savannah Georgia today.
Signature

Jerry Keenan

> Hi Jerry,
>
[quoted text clipped - 19 lines]
> One could even do it without rTmp.
> But that's a matter of style.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.