Automating Hyphenation Between Letters

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I frequently have to spell out words when I transcribe. For example, the
name Smith, when spelled out in a document, becomes S-M-I-T-H. How can I
automate the hyphens or dashes between the letters? I can't think of a macro
to do this. I can't change the document into a form because it is generated
by a company-supplied template. It's a formatting thing, I guess, but I
can't find anything in Word that touches on how to format a single word, only
styles. Thank you.
 
Type the word without the hyphens, then with the cursor in the word, run

Dim pWord As Word.Range
Dim pIndex As Long
Dim pNewWord As String

Set pWord = Selection.Words(1)
pNewWord = pWord.Characters(1)
For pIndex = 2 To pWord.Characters.Count
pNewWord = pNewWord & "-" & pWord.Characters(pIndex)
Next

Selection.Words(1) = pNewWord


Word's VBA concept of "word" is a little wierd, so you'll get odd results of
the word contains non-letters.
 
Thank you for a quick and thorough reply. Please forgive my ignorance,
though. How do I "run" this sequence? I am a long-time Word user, but am
just now needing to automate things and don't know what this means. Thank
you again.
 
This macro will do it for you if you select the name you want formated:

Sub myFormat()
Dim oRng As Range
Dim oChr As Word.Range
Set oRng = Selection.Range
oRng.MoveEndUntil Cset:="ABCDEFGHIJKLMNOPQRSTUVWXYZ", _ Count:=wdBackward
oRng.Text = UCase(oRng.Text)
Set oChr = oRng.Characters.First
Do
oChr.InsertAfter "-"
Set oChr = oChr.Next
Loop Until oChr = oRng.Characters.Last
End Sub
 
Sorry,

I was working with CAPs and have a line out of order. Try this.

Sub myFormat()
Dim oRng As Range
Dim oChr As Word.Range
Set oRng = Selection.Range
oRng.Text = UCase(oRng.Text)
oRng.MoveEndUntil Cset:="ABCDEFGHIJKLMNOPQRSTUVWXYZ", _
Count:=wdBackward
Set oChr = oRng.Characters.First
Do
oChr.InsertAfter "-"
Set oChr = oChr.Next
Loop Until oChr = oRng.Characters.Last
End Sub
 
OK, I see how to add the VB commands to make a macro and it works pretty
well. I will need to experiment with it to see how to get the best results.
You are a lifesaver. One question, though. How do I assign it to my
keyboard so that I don't have to find it and click on "run" every time?

Thanks so much AGAIN!
 
Have a bad time of it. The few steps are:
Sub myFormat()
Dim oRng As Range
Dim oChr As Word.Range
Dim oString As String
oString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
'Define the range
Set oRng = Selection.Range
'Convert to ALL CAPS
oRng.Text = UCase(oRng.Text)
'Trim oRng to remove trailing and leading spaces, etc.
oRng.MoveEndUntil Cset:=oString, Count:=wdBackward
oRng.MoveStartUntil Cset:=oString, Count:=wdForward
'Insert the hyphens
Set oChr = oRng.Characters.First
Do
oChr.InsertAfter "-"
Set oChr = oChr.Next
Loop Until oChr = oRng.Characters.Last
End Sub
 
Thank you Greg and Jezebel. Misson Accomplished! You are both great and
very helpful. Wish I had your knowledge! I am off to post again. Take care.
 
Wow, I've been using these macros for hyphenating names and it works great.
OK, gurus, how about if I want to hyphante phone numbers such as typing
3606992323 and having it default to 360-699-2323 or a birthday like 022772
and having it default to 02/27/72? Is this too big of a wish list? I am
SSOOOO happy with these two macros. You guys are awesome!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top