Shortcut

G

Greg Maxey

I posted the following in the DocManagement group to format words like Smith as S-M-I-T-H.

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

I thought that I could use Cset:="[A-Z]"
but it didn't work. I suppose that there is nothign wrong with "ABCD..." but was curious to know if there is a better way.

Thanks
 
G

Greg Maxey

I think I answered my own question or at least found a small improvement. I can trim both leading and trailing non-CAP letter characters by setting up a string variable and coding like this:

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

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

I posted the following in the DocManagement group to format words like Smith as S-M-I-T-H.

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

I thought that I could use Cset:="[A-Z]"
but it didn't work. I suppose that there is nothign wrong with "ABCD..." but was curious to know if there is a better way.

Thanks
 
J

Jezebel

Here are two more approaches, that both work on the selection whatever its
scope --

-- method 1

Dim pOld As String
Dim pNew As String
Dim pIndex As Long

pOld = Selection.Range
For pIndex = 1 To Len(pOld) - 1
pNew = pNew & Mid$(pOld, pIndex, 1)
If Mid$(pOld, pIndex, 2) Like "[A-Za-z][A-Za-z]" Then
pNew = pNew & "-"
End If
Next
pNew = pNew & Right$(pOld, 1)
Selection = pNew


-- method 2

with Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "([A-Za-z])([A-Za-z])"
.Replacement.Text = "\1-\2"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
.Execute Replace:=wdReplaceAll
End With

(step through to see why it needs to execute twice)

I think I answered my own question or at least found a small improvement. I
can trim both leading and trailing non-CAP letter characters by setting up a
string variable and coding like this:

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

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

I posted the following in the DocManagement group to format words like Smith
as S-M-I-T-H.

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

I thought that I could use Cset:="[A-Z]"
but it didn't work. I suppose that there is nothign wrong with "ABCD..."
but was curious to know if there is a better way.

Thanks
 
G

Greg Maxey

Jezebel,

It is late and sometimes I sleep. I will look at these further tomorrow.
Thanks.
 
J

Jezebel

hedonist


Greg Maxey said:
Jezebel,

It is late and sometimes I sleep. I will look at these further tomorrow.
Thanks.

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Here are two more approaches, that both work on the selection
whatever its scope --

-- method 1

Dim pOld As String
Dim pNew As String
Dim pIndex As Long

pOld = Selection.Range
For pIndex = 1 To Len(pOld) - 1
pNew = pNew & Mid$(pOld, pIndex, 1)
If Mid$(pOld, pIndex, 2) Like "[A-Za-z][A-Za-z]" Then
pNew = pNew & "-"
End If
Next
pNew = pNew & Right$(pOld, 1)
Selection = pNew


-- method 2

with Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "([A-Za-z])([A-Za-z])"
.Replacement.Text = "\1-\2"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
.Execute Replace:=wdReplaceAll
End With

(step through to see why it needs to execute twice)

I think I answered my own question or at least found a small
improvement. I can trim both leading and trailing non-CAP letter
characters by setting up a string variable and coding like this:

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

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

I posted the following in the DocManagement group to format words
like Smith as S-M-I-T-H.

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

I thought that I could use Cset:="[A-Z]"
but it didn't work. I suppose that there is nothign wrong with
"ABCD..." but was curious to know if there is a better way.

Thanks
 
G

Greg Maxey

Yes, if one should wish to apply such eye ball smashing formating to a
larger bit of text (e.g., War and Peace), then is seems that method 2 would
win the race. Interesting methods all.

Thanks.
 

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

Similar Threads


Top