How to create Macro that will prefix all lines with a character?

C

Cumulous

Hi! I need a VBA Macro that will process a selected section of text as
Quoted Text - similar to how email programs do it when you Reply to a Plain
Text message.

Basically, it needs to take a selected section of text and prefix every
line with a ">" character, whether the selected lines have a Line Break at
the end, or if they are simply wrapped around the next line.

Creating a Macro that prefixes a single line of text is easy:

-------------------------------

Sub QuoteLine()

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

End Sub

---------------------------------


But I don't know how to alter this code so that it processes a selected
section of text.

Can anyone help?

Thanks!
 
D

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT

Hi Cumulous,

Probably the easiest way to do this is to save the section of text as a .txt
file, accepting the option to terminate each line with a carriage return,
and then to open the file again in Word and use Edit replace to replace each
^p with >^p.

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
C

Cumulous

This is too unwieldy. Users need to do this for sections of text in the
middle of a document. They cannot start copy/pasting sections to a new
document, saving it under a different format, running a macro, copy/pasting
it back, etc.... Their productivity would grind to a screaching halt.

I can't imagine that this is such a hard thing to program. After all,
just about every email program in existence does this - including Outlook
(which uses Word to edit emails).

I have seen far more complex macros in use - is this one really so
difficult to produce?




Cumulous




"Doug Robbins - Word MVP - DELETE UPPERCASE CHARACTERS FROM EMAIL ADDRESS"
 
G

Graham Mayor

This is not as simple as you imagine, as there is no such thing as a 'line'
in Word. A line of text is an arbitrary entity determined by a variety of
factors, not least of which is the current printer driver, and adding your
character will screw up even that formatting; however, you can mark
individual 'lines' using the following bit of code, and if you assign the
macro to a keyboard shortcut or toolbar button, it is a simple process to
mark a block of text, line by line:

With Selection
.HomeKey Unit:=wdLine
.TypeText Text:=">"
.EndKey Unit:=wdLine
.TypeBackspace
.TypeParagraph
.MoveUp Unit:=wdLine, Count:=1
.Style = ActiveDocument.Styles("Normal")
.MoveDown Unit:=wdLine, Count:=1
End With

--
<>>< ><<> ><<> <>>< ><<> <>>< <>>< ><<>
Graham Mayor - Word MVP
E-mail (e-mail address removed)
Web site www.gmayor.com
Word MVP web site www.mvps.org/word
<>>< ><<> ><<> <>>< ><<> <>>< <>>< ><<>
 
L

Lars-Eric Gisslén

Cumulous,

Try this code as a starting point:

Sub PreFixLines()
Dim oRng As Range

Set oRng = Selection.Range

Selection.Collapse

While Selection.Range.End < oRng.End
Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
If Asc(Selection.Text) <> 13 Then
Selection.HomeKey Unit:=wdLine
Selection.TypeText "> "
' Must add a line break so the prefix will not
' be moved to the end of the previous line
Selection.EndKey Unit:=wdLine
Selection.TypeText Chr(11)
Else
' Empty line with just a paragraph mark
Selection.HomeKey Unit:=wdLine
Selection.TypeText "> "
Selection.MoveDown
End If
Wend
End Sub
 
C

Cumulous

The code sample I quoted in my initial message is able to process a
single line. What I need is a macro that will process an entire selected
block of text.

As for your mentioning that there is no such thing as a "line" in Word -
I understand where you are coming from. However, Word is able to understand
the concept because it's right there in the Status Bar on the bottom of the
screen "Ln X" where X is the line you are on. This has nothing to do with
Line Breaks, but simply the visual structure of the document itself as it is
displayed at that moment.

Therefore, is it not possible to write a macro that basically does this
(in plain text instructions):

1. Go to the first line in the selected block of text.
2. Enter "Home Key", "> ", "Down Arrow", "Home Key"
3. Repeat until you reach the bottom of the selected block of text.
4. End Macro


That doesn't seem like it should be that impossible. I have the
structure to enter the keys and text that are listed in Step 2. I just don't
know how to implement the processing of a selected block of text.

It has to be possible somehow - even if it's some form of:

1. Set FirstLine to the Line Number of the first line in the selection
2. Set LastLine to the Line Number of the last line in the selection
3. Set TotalLines to (LastLine - FirstLine)
4. And then start implementing Step 2 from the first section above while
incrementing a counter until you have reached what should be the last line.


It can't be impossible, if Outlook can do it, can it?



Cumulous
 
C

Cumulous

When I try that code in Word 2003, I get an endless cycle of blank lines
with just the ">" character until Word crashes.



Cumulous
 
L

Lars-Eric Gisslén

Cumulous,

Works for me on both Word 2000 and 2003. Have you tried to set a break point
in the code and step through it and verify where the problem is?
 
C

Cumulous

Here is what your code is doing:

1. I start off by selecting a section of text. Then I run the macro.

2. The cursor goes to the beginning of the first line and adds "> ".

3. The cursor then goes to the end of the line.

4. An empty line is added.

5. The empty line is selected.

6. The cursor goes to the beginning of the EMPTY line that was just added
and adds "> ".

7. Steps 4-6 repeat endlessly.




Cumulous
 
D

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT

If that is the case, each line of your text must already be terminated with
a carriage return or a manual line break.

The following modified macro should work, though you may end up with a
couple of lines that wrap without the > added to the front of them. It
therefore maybe best to strip off all of the carriage returns or manual line
breaks at the end of the line so that you are dealing with "word-wrapped"
text.

Sub PreFixLines()
Dim oRng As Range
Set oRng = Selection.Range
Selection.Collapse
While Selection.Range.End < oRng.End
Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
If Asc(Selection.Text) <> 13 Then
Selection.HomeKey Unit:=wdLine
Selection.TypeText "> "
' Must add a line break so the prefix will not
' be moved to the end of the previous line
'Selection.EndKey Unit:=wdLine
'Selection.TypeText Chr(11)
Selection.MoveDown
Selection.HomeKey Unit:=wdLine
Else
' Empty line with just a paragraph mark
Selection.HomeKey Unit:=wdLine
Selection.TypeText "> "
Selection.MoveDown
End If
Wend
End Sub


--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.
Hope this helps
Doug Robbins - Word MVP
 
G

Graham Mayor

The code pre-supposes that your document is formatted in paragraphs each of
which occupies more than one line, and it is in the paragraphs where the
problems lie. If the text comprises a range of lines each terminated with a
paragraph mark (or line feed mark) then the job is somewhat simpler and can
be handled in the way you require. However, the conversation has moved on
while I have been sleeping and the later suggestions should do what you
require.


--
<>>< ><<> ><<> <>>< ><<> <>>< <>>< ><<>
Graham Mayor - Word MVP
E-mail (e-mail address removed)
Web site www.gmayor.com
Word MVP web site www.mvps.org/word
<>>< ><<> ><<> <>>< ><<> <>>< <>>< ><<>
 
C

Cumulous

Ah! Now that does the job nicely. I don't mind needing to adjust for
the odd line break. That, I could code in. But that modified bit of code
is exactly what I needed to fill the void in my limited coding knowledge.

Thank you, Doug!



Cumulous



"Doug Robbins - Word MVP - DELETE UPPERCASE CHARACTERS FROM EMAIL ADDRESS"
 
C

Cumulous

Actually, there is one remaining problem with this revised code.

When the initial selection goes to the very end of the document - the
Macro keeps adding "> " to the end in an endless cycle until Word crashes.

Basically, the code tells it to move down a line - but it has nowhere to
go, so the characters keep being entered endlessly.

Is this a difficult problem to address?



Cumulous



"Doug Robbins - Word MVP - DELETE UPPERCASE CHARACTERS FROM EMAIL ADDRESS"
 

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