Easy "Loop" question?

R

Rodney Dangerfield

Hi. I'm new to VB. Can anyone help me out? (I don't get no respect. No
respect at all!)

I've got a simple Microsoft Word macro (see below) that inserts quotation
marks at the start and end of a line of text. For instance, it will change:
my line of text
into . . .
"my line of text"

But now I've got a document with about 400 lines of text.
So I need to have the macro LOOP (is that what it's called?) . . . so it
keeps REPEATING ITSELF until it reaches the BOTTOM of the Word document.
Then it stops.

I realize this is probably an easy question for most folks in this group . .
.. I'm grateful for your help. - Jim

HERE'S THE MACRO:

Sub InsertQuotes()
'
' InsertQuotes Macro
' Macro recorded 11/16/2004 by Jim
'
Selection.TypeText Text:=""""
Selection.EndKey Unit:=wdLine
Selection.TypeText Text:=""""
Selection.MoveRight Unit:=wdCharacter, Count:=1

End Sub
 
D

Doug Robbins

Use:

Dim i As Long, pararange As Range
For i = 1 To ActiveDocument.Paragraphs.Count
Set pararange = ActiveDocument.Paragraphs(i).Range
pararange.End = pararange.End - 1
pararange.InsertBefore Chr(34)
pararange.InsertAfter Chr(34)
Next i


--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
 
R

Rodney Dangerfield

Hi Doug

Thanks for the reply. But I have ZERO programming training . . . so I
don't understand what you've told me to do. Should I just "cut-n-paste" the
stuff you wrote INTO my existing macro? If yes, WHERE should it go? If no,
you'll have to show me what to do.
 
G

Greg Maxey

I am sure Doug would get back to you shortly, but if you don't want to wait
you can delete everything in your existing macro except the Sub and End Sub
lines and paste Doug's code in it's place.

Think about what you asked. For each paragraph in the document "i" for the
first paragraph "1" until the "count of paragraphs" (Word will count how
many there are all the way to the end) set a range (basically the same as
you selecting one at a time), with that range back up one space to exclude
the paragraph mark, then insert the " which is a character code 34 before
and after the range. Move on to the next paragraph and stop when i = count.

HTH.

BTW, I'm a novice at VBA myself, so if someone else comes along and tweaks
my explanation a bit then we both migth learn something :)
 
J

Jean-Guy Marcil

Greg Maxey was telling us:
Greg Maxey nous racontait que :
I am sure Doug would get back to you shortly, but if you don't want
to wait you can delete everything in your existing macro except the
Sub and End Sub lines and paste Doug's code in it's place.

Think about what you asked. For each paragraph in the document "i"
for the first paragraph "1" until the "count of paragraphs" (Word
will count how many there are all the way to the end) set a range
(basically the same as you selecting one at a time), with that range
back up one space to exclude the paragraph mark, then insert the "
which is a character code 34 before and after the range. Move on to
the next paragraph and stop when i = count.
HTH.

BTW, I'm a novice at VBA myself, so if someone else comes along and
tweaks my explanation a bit then we both migth learn something :)

I just want to add that Doug's code will iterate the paragraphs collection
in the document. You asked about iterating through each line.
That is not the same thing as a paragraph can span many lines.
Of course, it can be that your document consists of nothing but one-line
paragraphs, in which case Doug's macro, along with Greg's explanations, will
be the ticket.

On the other hand, if you really wanted to wrap each line in quotes, even
the lines within a multi-line paragraph, that can be very hairy, especially
for a beginner.
(For a clue as why it can be hairy, just think about the fact that adding
quotes to a line adds characters to this line, which in turn can cause the
wrapping to change, with words being moved to the next line as soon as you
add one single quote character. Even if your document consists of one-line
paragraphs, it may well be that some of them happen to fit exactly within
the margins, and as soon as you add the first quote, the last word on the
line will be wrapped to the next line, thus adding a line...)

Meanwhile, have a look at:
http://word.mvps.org/faqs/macrosvba/CreateAMacro.htm
for an article titled:
"What do I do with macros sent to me by other newsgroup readers to help me
out?"

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
R

Rodney Dangerfield

Thanks , guys (Doug, Greg, Jean) . . . I'm amazed I can get such "expert"
advice in these groups! Your solutions were terrific . . . and I definitely
learned some things.

Very good.

Bye

Jim
 

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