#1
|
|||
|
|||
Find last word in paragraph and delete it
Hello All,
I do not know how to do this so I am after some help… I am after a macro that will:
For example: The quick brown fox. The macro would delete the word fox including the full stop. I am not fussed if it also deletes the space (leaving 'brown' without any space after it). Any help appreciated. Regards, Dave T |
#2
|
||||
|
||||
Code:
Sub DeleteLastWord() Dim oRng As Range 'set a range to the paragraph the cursor is in Set oRng = Selection.Paragraphs(1).Range 'omit the paragraph break character oRng.End = oRng.End - 1 'Collapse the range to its end oRng.Collapse 0 'Move backwards to the first space oRng.MoveStartUntil Chr(32), wdBackward 'Optionally include that space oRng.Start = oRng.Start - 1 'Delete the range oRng.Delete lbl_Exit: Set oRng = Nothing Exit Sub End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
Here is another approach:
Code:
Sub ScratchMacro() 'A basic Word macro coded by Greg Maxey Do Selection.Paragraphs(1).Range.Characters.Last.Previous.Delete Loop Until Selection.Paragraphs(1).Range.Characters.Last.Previous _ Like "[" & Chr(32) & "," & Chr(160) & "," & Chr(9) & "]" 'Space, non-breaking space or tab. lbl_Exit: Exit Sub End Sub |
#4
|
|||
|
|||
Hello Graham & Greg,
I must admit I follow both of your posts on this and other forums with great interest. To have both of you reply to my post is very much appreciated. I will have a closer look at what each of you has suggested. Both work as requested. Thanks to both of you for replying. Regards, Dave T |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Why when I paste text into word, blank space appears after a paragraph & I can't delete it? | AWD | Word | 3 | 10-16-2018 01:10 PM |
can't delete paragraph mark at end of document | kb | Word | 10 | 10-06-2017 02:32 PM |
Delete bookmark and paragraph which is blank | dsjk9190 | Word VBA | 2 | 01-22-2015 01:15 AM |
Word thinks 1 sentence is 2 paragraphs and won't let me delete the extra paragraph symbol | jrasicmark | Word | 1 | 12-05-2014 01:50 AM |
Find all email addresses in word document and delete | Chayes | Word VBA | 14 | 10-22-2013 06:30 AM |