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 / November 2004

Tip: Looking for answers? Try searching our database.

Easy "Loop" question?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rodney Dangerfield - 18 Nov 2004 01:43 GMT
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
Doug Robbins - 18 Nov 2004 03:24 GMT
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

Signature

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

> Hi. I'm new to VB.  Can anyone help me out?  (I don't get no respect.  No
> respect at all!)
[quoted text clipped - 28 lines]
>
> End Sub
Rodney Dangerfield - 18 Nov 2004 03:59 GMT
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.  

> Use:
>
[quoted text clipped - 38 lines]
> >
> > End Sub
Greg Maxey - 18 Nov 2004 04:22 GMT
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 :-)

Signature

Greg Maxey/Word MVP
A Peer in Peer to Peer Support

> Hi Doug
>
[quoted text clipped - 52 lines]
>>>
>>> End Sub
Jean-Guy Marcil - 18 Nov 2004 05:09 GMT
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
[quoted text clipped - 11 lines]
> 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?"

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

Rodney Dangerfield - 18 Nov 2004 22:45 GMT
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

> Greg Maxey was telling us:
> Greg Maxey nous racontait que :
[quoted text clipped - 38 lines]
> "What do I do with macros sent to me by other newsgroup readers to help me
> out?"

Rate this thread:






 
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.