Jeffery said:
Hi all,
Can anybody tell me how I can do a Format/Change Case "Sentence Case"
on a field I am receiving from a UserForm? I don't want to do a
UpperCase on all the characters I just want to convert the first
letter of the entry to uppercase only. User may enter space as the
first Char on the field.
One way is to insert the string as-is into the document, and then use the
Range.Case property to make it sentence case -- this is the exact equivalent
of using Format > Change Case.
Sub foo1()
Dim myStr As String
' represents string received from userform
myStr = " this is text."
Dim myRange As Range
Set myRange = ActiveDocument.Bookmarks("bk1").Range
With myRange
.Text = myStr
.Case = wdTitleSentence
End With
ActiveDocument.Bookmarks.Add Name:="bk1", Range:=myRange
End Sub
A second way is to search the string for the first non-space character and
call the UCase function on it:
Sub foo2()
Dim myStr As String
' represents string received from userform
myStr = " this is text."
Dim nFirstLetter As Long
nFirstLetter = 1
Do While Mid$(myStr, nFirstLetter, 1) = " "
nFirstLetter = nFirstLetter + 1
Loop
Mid$(myStr, nFirstLetter, 1) = _
UCase$(Mid$(myStr, nFirstLetter, 1))
MsgBox """" & myStr & """"
End Sub
There is no exact equivalent of Format > Change Case for a string in VBA
memory; for that, the text has to be in the document body.
--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.