Cumulous said:
Hi, JGM!
Okay, well to begin with - the reason it was not a big issue for me was
because I had always been told that if we were going to be processing text
that was word wrapped - it would be a true nightmare and not generally worth
the effort and the extreme monetary expense of Advil that would result.
Do not worry... I won't charge you and I enjoyed monkeying around with the
code! No Advil necessary!
That being the case, I resigned myself to that limitation for two
reasons:
1. Everyone agreed that it wasn't worth the effort to get it right.
Well, I maybe old fashioned, but I believe that if it is worth doing at all,
it should be done right!
2. By taking the simpler route - I gained an additional benefit, which was
that text that was word-wrapped would remain word-wrapped. By putting
line-breaks at the end of each line, that has the potential of causing worse
problems depending on what else needs to be done with the text afterwards.
Possibly... Then instead of adding characters to signify "user quoted" text,
it would be better to consider a different approach. Such as changing font
color, adding a border, highlighting, etc.
This way the original text/paragraphs retain their integrity. Otherwise,
whatever type or number of characters you decide to add at the beginning of
the line, as soon as you add characters it is impossible to retain the
orginal text integrity... in the latter case, I do not know which is worse:
Adding ¶ at the end of every line and a special character at the begining of
every line or having your text interspersed with quote characters but
retaining wrapping.... In both cases it is easy to revert to the original
with a find/replace, but the first case looks better visually and acts
exactly like an e-mail software that quotes lines and moves characters to
new lines as necessary... at least that is what my Outlook does...
As such, I am content with your original code - but I would like to know
what needs to be changed to correct that small issue I initially
mentioned.
Try replacing
Selection.Start = oRng.Start
Selection.Collapse
by
Selection.HomeKey Unit:=wdLine
in the code below.
'_______________________________________
CountEnd = 0
Set oRng = Selection.Range
Selection.Collapse
'check if only last line selected and
'if it is not a lone paragraph mark
Selection.EndKey Unit:=wdLine
If Selection.Range.End + 1 = ActiveDocument.Range.End _
And Asc(oRng.Text) <> 13 Then
CountEnd = 1
End If
Selection.Start = oRng.Start
Selection.Collapse
'_______________________________________
Now, that having been said - your current code also has its uses, but it
does seem to be something of a nightmare to accomplish and polish - and I
hate to put anyone else through that immense effort. (In other words - if
you insist on persisting until you get it right, than count me in on being
on-board to help you test. But if you would rather just wash your hands of
the whole thing - you will get no argument from me. Only thanks for all the
effort you have put in.
Regardless of how you want to proceed - I did take a quick look at the
current revision of code (I did not do thorough testing yet, before seeing
how you wanted to proceed). However, I did find a couple of problems:
--------------------------------------------
1. When processing word-wrapped text:
This:
----------------
But more to the point, that was not the problem I was referencing with
your original code. The
problem was not that a line was processed - and the inserted text wrapped
to the line above. The
problem is that the *wrong* line was processed in the first place.
When you selected a line in one of those two situations - the method
your code selects that line
and then collapses that selection moves the cursor to the line *above* when
it is supposed to just
move it to the beginning of that *same* line.
----------------
Turns into this:
----------------
with
your original code.
wrapped
to the line above.
your code selects that
when it is supposed to
----------------
So, every line seems to have the last word put onto a line of its own.
----------------------
1. Impossible to do otherwise, you add two characters to each line... The
line ends up being longer. So, either, as it is the case with my code, you
have extra lines where ever the added characters had an impact, or, as in my
first version, you have at the most only one extra line at the end of the
pargrpah but you have quote symbols throught out the quoted text.
2. When processing text that has line breaks at the end of every line,
*sometimes* it works properly - but I have seen a number of issues crop up:
2a. Additional lines being inserted in between paragraphs
As explained in 1. above.
2b. Blank lines are occasionally skipped insofar as having the "> "
inserted. (These may be the lines inserted listed in 2a)
I tried with all types of situations and could never reproduce this
behaviour.
3b. When selecting a text block that includes empty lines on the
bottom - an extra blank line is added before the last one, which is not
processed. Example:
This (last empty line is selected as well for processing):
----------------------
That has nothing to do with wrapping - it has to do with cursor
placement before the insert.
This is fixed with my fourth version provided below... do not worry it was
very easy to fix.
Now, once again - please do not try to perfect this version of code
unless you *really* want to. (I feel guilty already.
But whether you do or not, I would like to know how to change the
original revised version.
See above, I give you a hint.
Thank you again!
Cumulous
'_______________________________________
Sub QuoteSelection_v4()
'Constant, symbol to signify "Quotation"
Const QuoteMark As String = "> "
'User selected range
Dim oRng As Range
'Each line in range converted in a paragraph
Dim MyPara As Paragraph
'In case range changes when adding paragrpah marks
Dim oRngStart As Integer
'To make sure we do not have an endless loop
'when adding paragraph marks
Dim Count1 As Integer
'To make sure we do not have an endless loop
'when adding QuoteMark
Dim Count2 As Integer
'To check whether paragraph marks have been added,
'in which case the user range has changed
Dim AddedChar As Boolean
'Set initial values
AddedChar = False
Count1 = 0
Count2 = 0
Set oRng = Selection.Range
oRngStart = oRng.Start
Selection.Collapse
'check if only last line selected and
'if it is a lone paragraph mark
Selection.EndKey Unit:=wdLine
If Selection.Range.End + 1 = ActiveDocument.Range.End _
And Asc(oRng.Text) = 13 Then
Count2 = 1
End If
Selection.HomeKey Unit:=wdLine
'If user selection is wrapped...
'Add ¶ to try to keep the text integrity
'as we will add 2 characters to the beginning of each line
'so the final text will be wrapped differently
'and the added QuoteMarks may be lost "inside" the text because
'of the wrapping
Do While Selection.Range.End <= oRng.End
Selection.EndKey Unit:=wdLine
If Selection.Range.End + 1 = ActiveDocument.Range.End Then
Count1 = Count1 + 1
End If
If Count1 > 1 Then Exit Do
Selection.HomeKey Unit:=wdLine
'in case line is made up of a lone line feed character:
If Asc(Selection.Text) = 11 Then
Selection.Text = Chr(13)
Selection.MoveRight wdCharacter, 1
Selection.Delete
End If
Selection.MoveLeft wdCharacter, 1
If Not Selection.Range.Start = 0 Then
If Asc(Selection.Text) <> 13 Then
Selection.Text = Chr(13)
AddedChar = True
End If
Selection.MoveRight wdCharacter, 1
If Asc(Selection.Text) = 11 Then Selection.Delete
End If
Selection.MoveDown
Loop
'Reset range to reflect user selection after adding ¶
If AddedChar Then
oRng.Start = oRngStart
If Not oRng.End = ActiveDocument.Range.End Then
oRng.SetRange oRng.Start, _
oRng.End - 1
End If
End If
'Add QuoteMark
oRng.Select
Selection.Collapse
For Each MyPara In oRng.Paragraphs
MyPara.Range.InsertBefore QuoteMark
Next MyPara
'Check whether the addition of Quotemark has forced lines to
'overflow onto a new line, i.e. a line has become two lines.
'If so, add QuoteMark to the beginning of the new "second" line
Do While Selection.Range.End <= oRng.End
If Selection.Range.End + 1 = ActiveDocument.Range.End Then
Count2 = Count2 + 1
End If
If Count2 > 1 Then Exit Do
Selection.HomeKey Unit:=wdLine
'in case last line is a single ¶
If Asc(Selection.Text) = 13 Then
Selection.TypeText QuoteMark
Else
Selection.MoveRight wdCharacter, 2, wdExtend
If Not Selection.Text = QuoteMark Then
Selection.Collapse
Selection.TypeText Chr(13) & QuoteMark
End If
End If
Selection.MoveDown
Selection.EndKey Unit:=wdLine
Loop
'Reselect original user selection
oRng.Select
End Sub
'_______________________________________
HTH
Cheers!