Help with a Word macro that capitalizes the first letter of a word

Cindy Giovanetti
Cindy Giovanetti used Ask the Experts™
on
I need a bit of code for a Word macro that will capitalize the first letter of a word if it's not already capitalized.  But if it is already capitalized, then I don't want it changed.

My current macro uses Shift-F3, which is Word's toggle between cap/not cap.  It works great when the target word is lower case, but if the word is already capped, it lower cases it, which is not what I want.

So, here's the situation in which I would use it.  Say there's a run-on sentence like this:

Mary likes apples Tom likes oranges.

I want to put my cursor after the s in apples and run the macro.  The macro should type a period, move over one space, add a second space, select the T in Tom, and capitalize it if it's not capitalized and leave it alone if it is capitalized.  Then I want the cursor to end up just to the left of the T in Tom.  

And here's my current code:

Sub insertaperiod()
'
' insertaperiod Macro
'
'
    Selection.MoveLeft Unit:=wdCharacter, Count:=1
    Selection.MoveRight Unit:=wdWord, Count:=1
    Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    Selection.Range.Case = wdNextCase
    Selection.MoveLeft Unit:=wdCharacter, Count:=2
    Selection.TypeText Text:="."
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.TypeText Text:=" "
End Sub

Thank you to this very helpful group!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Joe WinogradDeveloper
Fellow 2017
Most Valuable Expert 2018

Commented:
Hi Cindy,
We can leverage your new AutoHotkey Hyper (or Meh) solution again.

Make a new hotkey in your Hyper.ahk file with this in it:

!^+#k::
ClipSave:=Clipboard ; save clipboard
Clipboard:="" ; clear clipboard
Send .{Right}{Space} ; send a period, right arrow, space
Send +{Right}^c ; copy the next character to the clipboard
ClipWait ; wait for char to appear on clipboard
ClipChar:=Clipboard
If ClipChar is lower ; it is lower case
  StringUpper,ClipChar,ClipChar ; make it upper case
Send %ClipChar% ; paste over char
Clipboard:=ClipSave ; restore clipboard
Return

Open in new window

I chose "k" this time for Kapitalize, since you're already using "c". As you know by now, Hyper+k will do it. Regards, Joe
Cindy GiovanettiTranscription

Author

Commented:
OK, I'm going to admit that I didn't use this method.  But your method inspired me to look for another method that was simpler.  Word has an all caps option under the Font menu, and I went with that.
Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015
Commented:
Or you may simply replace the following line in your existing code...

Selection.Range.Case = wdNextCase

Open in new window


WITH THIS

Selection.Text = StrConv(Selection.Text, vbUpperCase)

Open in new window

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Cindy GiovanettiTranscription

Author

Commented:
Oh my.  Thank you for that bit of code, Subodh!  That looks very helpful!
Subodh Tiwari (Neeraj)Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015

Commented:
You're welcome Cindy! Glad it worked as desired.
Cindy GiovanettiTranscription

Author

Commented:
I added that bit of code to many of my preexisting macros.  :)
Subodh Tiwari (Neeraj)Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015

Commented:
Great! Glad you found it useful. :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial