Programming email quote processing

O

Ocean

I'm trying to program a simple macro in Word XP, but my Visual Basic
skills are not up to the task.

Basically, all I want is a macro that will take a selected section of
text, and add a ">" character to the beginning of each line. That's all. I
need to be able to process emails that do not have their quoted sections
properly prefixed with the ">" character, and add that in. The macro in
question has to work ONLY on selected text, not the whole document (unless
the whole document is selected, of course).

Obviously, doing this for a single line at a time is easy, as I would
only need the following:

-----------------------------
Sub QuoteSingleLine()

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

End Sub
--------------------------------


But setting this up to process a selection of text is another story.

Can anyone offer me some insight on how to accomplish this seemingly
simple task?

Thanks!
 
M

Mark Tangard

Ocean,

What you want is a simple loop.

I'm assuming this is a text file with hard returns on each line,
otherwise, as you doubtless know, all hell breaks loose. If so,
that actually is the key to it -- each line in what you have is
a *paragraph* as far as Word is concerned. A good thing, since
there's no "line" object in VBA. So you want something like this.

Dim p as Paragraph
For Each p in ActiveDocument.Paragraphs
p.Range.InsertBefore "> "
Next p

Note that this is done with ranges, not the selection. That'll
make the code run faster and keep the screen calm.
 
C

Cumulous128

What do you do when you're talking about a text file where there are
*not* hard returns on each line? There has to be a way to get it done...



Cumulous
 
L

Lars-Eric Gisslén

Cumulous,

Try this:

Dim bCont As Boolean

If Selection.Range.Text <> "" Then
ActiveDocument.Bookmarks.Add "bmTmp", Selection.Range
bCont = True

While bCont
Selection.HomeKey Unit:=wdLine
Selection.TypeText Text:=">"
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.HomeKey Unit:=wdLine
If Selection.Range.End > _
ActiveDocument.Bookmarks("bmTmp").Range.End Or _
Selection.Range.End = ActiveDocument.Range.End - 1 Then
bCont = False
End If
Wend
ActiveDocument.Bookmarks("bmTmp").Delete
End If

Regards,
Lars-Eric
 

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