Link to home
Start Free TrialLog in
Avatar of Cindy Giovanetti
Cindy GiovanettiFlag for United States of America

asked on

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

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!
Avatar of Joe Winograd
Joe Winograd
Flag of United States of America image

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
Avatar of Cindy Giovanetti

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Subodh Tiwari (Neeraj)
Subodh Tiwari (Neeraj)
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oh my.  Thank you for that bit of code, Subodh!  That looks very helpful!
You're welcome Cindy! Glad it worked as desired.
I added that bit of code to many of my preexisting macros.  :)
Great! Glad you found it useful. :)