Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-18-2015, 10:58 PM
Dave T Dave T is offline Find last word in paragraph and delete it Windows 7 64bit Find last word in paragraph and delete it Office 2007
Advanced Beginner
Find last word in paragraph and delete it
 
Join Date: Nov 2014
Location: Australia
Posts: 66
Dave T is on a distinguished road
Default 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:
  • Go to the end of the current paragraph (not line) the cursor is in
  • Find the last space in the paragraph (i.e. go back from the paragraph mark to the first space left)
  • Select the whole last word (including any full stop, etc.) and delete it

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
Reply With Quote
  #2  
Old 05-18-2015, 11:44 PM
gmayor's Avatar
gmayor gmayor is offline Find last word in paragraph and delete it Windows 7 64bit Find last word in paragraph and delete it Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,105
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #3  
Old 05-19-2015, 04:18 AM
gmaxey gmaxey is offline Find last word in paragraph and delete it Windows 7 32bit Find last word in paragraph and delete it Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #4  
Old 05-21-2015, 12:40 AM
Dave T Dave T is offline Find last word in paragraph and delete it Windows 7 64bit Find last word in paragraph and delete it Office 2007
Advanced Beginner
Find last word in paragraph and delete it
 
Join Date: Nov 2014
Location: Australia
Posts: 66
Dave T is on a distinguished road
Default

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
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find last word in paragraph and delete it 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
Find last word in paragraph and delete it Delete bookmark and paragraph which is blank dsjk9190 Word VBA 2 01-22-2015 01:15 AM
Find last word in paragraph and delete it 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 last word in paragraph and delete it Find all email addresses in word document and delete Chayes Word VBA 14 10-22-2013 06:30 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:23 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft