Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-27-2023, 03:25 PM
wardw wardw is offline How to select the word the insertion point is in? Windows 7 64bit How to select the word the insertion point is in? Office 2007
Advanced Beginner
How to select the word the insertion point is in?
 
Join Date: Oct 2012
Posts: 60
wardw is on a distinguished road
Default How to select the word the insertion point is in?

I'd like to create a macro that deletes the word the insertion point is in plus any punctuation after it, then capitalizes the word after that. E.g., "However, frogs are in the pond" changes to "Frogs are in the pond". Foolishly, I tried to get ChatGPT to write the macro, which deteriorated into constant errors and time wasted. It couldn't seem to select the word the insertion point was in, plus the following punctuation. It kept overshooting or undershooting.



I checked some VBA info on selecting text, but couldn't find how to select just the word the insertion point is in. Is there a piece of VBA code that can do that?
Reply With Quote
  #2  
Old 08-27-2023, 04:53 PM
Guessed's Avatar
Guessed Guessed is offline How to select the word the insertion point is in? Windows 10 How to select the word the insertion point is in? Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

This code selects the current word. If the word is followed by a space it also selects that. If it is followed by punctuation, it doesn't select the punctuation.
Code:
Sub GetIt()
  Dim aWord As Range
  Set aWord = Selection.Words(1)
  aWord.Select
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 08-27-2023, 11:07 PM
gmayor's Avatar
gmayor gmayor is offline How to select the word the insertion point is in? Windows 10 How to select the word the insertion point is in? Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,106
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

The following should select the word the cursor is in, plus any following punctuation and sets the next character as upper case. You can add to the punctuation as required.

Code:
Sub Macro1()
Const sList As String = " ,.!?"
Dim oWord As Range
    Set oWord = Selection.Words(1)
    With oWord
        .MoveEndWhile sList
        .Text = ""
        .End = .End + 1
        .Case = wdUpperCase
    End With
    Set oWord = Nothing
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
  #4  
Old 08-28-2023, 10:03 AM
wardw wardw is offline How to select the word the insertion point is in? Windows 7 64bit How to select the word the insertion point is in? Office 2007
Advanced Beginner
How to select the word the insertion point is in?
 
Join Date: Oct 2012
Posts: 60
wardw is on a distinguished road
Default

Thanks to you both, Andrew and Graham. Both macros worked perfectly as described. I'd like to embarrass ChatGPT with the solutions, but that would be mean ;-)
Reply With Quote
Reply

Tags
selecting text



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to select the word the insertion point is in? Cursor location on first click -- insertion point stevec5088 Word 1 12-14-2016 02:45 PM
How to select the word the insertion point is in? Move Insertion Point with VBA Grasor Word VBA 2 10-11-2016 08:50 PM
Word 2013 Insertion Point stops blinking NeilSC Word 0 12-13-2013 12:19 PM
How to select the word the insertion point is in? Insertion point not working pmahoney14 Word 1 05-24-2012 04:17 PM
How to select the word the insertion point is in? wildly flashing insertion point with spinning wheel joannetk Word 4 03-10-2011 04:26 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:39 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