PDA

View Full Version : Removing final newline in last paragraph marker



wallander
09-05-2011, 02:14 AM
Hi there,

I need to copy several paragraphs of text from a word document upon the pressing of a button. This text is then pasted into a website, into a text box.
Due to the way paragraphs seem to work, when copying the last paragraph into the website text box, another line is inserted at the bottom, i.e. the last line of text is not the last line in the pasted into text box. - Hope that is clear -?
I think it is because the paragraph character has a trailing space included in it - ? Please could someone kindly tell me how to remove this last space or perhaps its a carriage return of some type - but I dont think so.

e.g pasted text last line

abcdefghijkl = last line of text in website text box
!cursor is here, where the ! mark is.




Sub Select_Paragraphs()
Dim rngParagraphs As Range
Set rngParagraphs = ActiveDocument.Range( _
Start:=ActiveDocument.Paragraphs(2).Range.Start, _
End:=ActiveDocument.Paragraphs(16).Range.End)
rngParagraphs.Copy
End Sub



thank you for any help.

Talis
09-05-2011, 12:24 PM
After the line rngParagraphs.Copy put this:
With rngParagraphs
If .Characters.Last = " " Or .Characters.Last = vbCr Then
.MoveEnd Unit:=wdCharacter, Count:=-1
End With
End If

Talis
09-06-2011, 12:31 PM
Oops! That should be before rngParagraphs.Copy

macropod
09-09-2011, 05:48 AM
Variation on a theme:
Sub Select_Paragraphs()
Dim rngParagraphs As Range
Set rngParagraphs = ActiveDocument.Range( _
Start:=ActiveDocument.Paragraphs(2).Range.Start, _
End:=ActiveDocument.Paragraphs(16).Range.End - 1)
With rngParagraphs
While .Characters.Last = " " Or .Characters.Last = Chr(160)
.MoveEnd Unit:=wdCharacter, Count:=-1
Wend
.Copy
End With
End Sub