First letter on each sentence UCase
hi
i need some help to make text in textbox change to first letter on first word in a sentence UCase.
im a trying to do this: You input Text into a Text Box, you click a radio button, then you click a convert text cmd and it changes the text in the text box to first letter on first word in a sentence UCase.
I have done first letter on each word upper case which was
Code:
Option Explicit
Private Sub Command1_Click()
MsgBox makeFirstUCase("hello i aM aaRon")
End Sub
Private Function makeFirstUCase(myStr As String) As String
Dim strArr() As String
Dim i As Integer
strArr = Split(myStr, " ")
For i = 0 To UBound(strArr)
If strArr(i) <> vbNullString Then
strArr(i) = UCase(Left$(strArr(i), 1)) & Right$(strArr(i), Len(strArr(i)) - 1)
End If
makeFirstUCase = makeFirstUCase & strArr(i) & " "
Next
End Function
Please help :)
Re: First letter on each sentence UCase
if this is the result that you want Hello I Am Aaron then you may use this code
vb Code:
Private Function makeFirstUCase(myStr As String) As String
Dim strArr() As String
Dim i As Integer
strArr = Split(myStr, " ")
For i = 0 To UBound(strArr)
If strArr(i) <> vbNullString Then
strArr(i) = UCase(Left$(strArr(i), 1)) & LCase(Mid$(strArr(i), 2))
End If
makeFirstUCase = makeFirstUCase & strArr(i) & " "
Next
End Function
Re: First letter on each sentence UCase
thanks but i wanted the result of:
that dog is very lazy. it should go for a run! why is it tired? to
That dog is very lazy. It should go for a run! Why is it tired?
Re: First letter on each sentence UCase
you need to search any character that can end sentance, followed by space, try like this
vb Code:
pos = instr(text1.text, ". ")
do while pos > 0
text1.text = left(text1.text, pos +1) & ucase (mid(text1.text, pos +2, 1)) & mid(text1, pos +3)
pos = instr(pos +1, text1.text, ". ")
loop
followed by similar loops for ? ! and other possible sentence endings
this will fail for any sentence /word enclosed in quotes, so you may need to test for quotes in each case, also words following an abbreviation may also be an issue
eg Dr. or Mr., though in both those case capitalising the next letter is probably fine, but in some cases may not
Re: First letter on each sentence UCase
You will probably have to check for acronyms too.
Re: First letter on each sentence UCase
you need to first determine what could be the end of the sentence in your case it is "." & "!" & "?" .now u need to loop thru until u find these characters and convert the next letter after a space to ucase (proper sentence structure)
try this code
vb Code:
Private Sub Command1_Click()
MsgBox convTxt("that dog is very lazy. it should go for a run! why is it tired? oh my god are u serious?")
End Sub
Private Function convTxt(strTxt As String) As String 'strTxt
Dim strconst As String
'determine the character that could be the end of a sentence
strconst = ".!?"
Dim strone As String
Dim y As Integer
Dim strTwo As String
Dim x As Integer
'go thru each character
For x = 1 To Len(strconst)
strTwo = Mid$(strconst, x, 1)
y = 0
Do
y = InStr(y + 1, strTxt, strTwo)
strone = Mid(strTxt, y + 2, 1)
If y = 0 Then Exit Do
'convert to ucase
strTxt = Replace(strTxt, strTwo & " " & strone, strTwo & " " & UCase(strone))
Loop
Next
'convert first letter of the sentence to ucase
convTxt = UCase(Left$(strTxt, 1)) & Mid(strTxt, 2)
End Function
Re: First letter on each sentence UCase
Quote:
Originally Posted by aaron1259
thanks but i wanted the result of:
that dog is very lazy. it should go for a run! why is it tired? to
That dog is very lazy. It should go for a run! Why is it tired?
Maybe:
MsgBox StrConv("that dog is very lazy. it should go for a run! why is it tired?", vbProperCase)
Re: First letter on each sentence UCase
Quote:
Originally Posted by TysonLPrice
Maybe:
MsgBox StrConv("that dog is very lazy. it should go for a run! why is it tired?", vbProperCase)
vbProperCase option will capitalize first letter of each word:
"That Dog Is Very Lazy. It Should Go For A Run! Why Is It Tired?"
There must be something like convert to "Sentence case" in MS-Word.
Re: First letter on each sentence UCase
Quote:
Originally Posted by anhn
There must be something like convert to "Sentence case" in MS-Word.
There is.
Short of that, here's another version of a sentence case function I posted last year:
Code:
Public Function SentenceCase(pstrText As String) As String
Dim i As Long
Dim bytArray() As Byte
Dim blnUpper As Boolean
blnUpper = True
bytArray = StrConv(pstrText, vbFromUnicode)
For i = 0 To UBound(bytArray)
Select Case bytArray(i)
Case 33, 46, 63 ' exclamation point, period or question mark
blnUpper = True
Case 65 To 90, 97 To 122
If blnUpper Then
bytArray(i) = Asc(UCase$(Chr$(bytArray(i))))
blnUpper = False
End If
End Select
Next
SentenceCase = StrConv(bytArray, vbUnicode)
Erase bytArray
End Function
Re: First letter on each sentence UCase
Aaron, this code seems to have some promise. Build a form with a command button and a text box.
Code:
Dim MyString As String
Private Sub Command1_Click()
Mid$(MyString, 1, 1) = UCase(Mid$(MyString, 1, 1)) ' Cap first letter
Dim Pointer As Long
Do While Pointer < Len(MyString)
Pointer = Pointer + 1
Select Case Asc(Mid$(MyString, Pointer, 1))
Case 33, 46, 63 ' Found end of a sentence
If Pointer < Len(MyString) Then
Pointer = Pointer + 1
Do While Mid$(MyString, Pointer, 1) = " " And Pointer < Len(MyString)
Pointer = Pointer + 1
Loop
' Cap first letter of next sentence
Mid$(MyString, Pointer, 1) = UCase(Mid$(MyString, Pointer, 1))
End If
Case Else
End Select
Loop
Text1.Text = MyString
End Sub
Private Sub Form_Load()
' Sample collection of sentences
MyString = "cap me... please cap me? now cap me! Don't have to cap me; OK as is!..."
Text1.Text = MyString
End Sub
SentenceCase: Capitalize first letter of each sentence
This is my version. I want to make it as close as possible to an MS-Word's method, it also covers Unicode characters.
Moved to CodeBank.
Re: First letter on each sentence UCase
Terrific, anhn, but I notice that you are starting by converting all text to lower case before you start and that would seem to add a bit of complexity, thus turning the code into somewhat of an expert grammar/spell checker.
To me, that means that if the user wanted caps here and there, such as VB coded variables (e.g., SentenceCase above) that these caps would be eliminated. Please corect me if wrong.
I really do like the way this code handles the non-breaking space, wordwrap, and when the sentence starts immediately on a line after a hard return. I forgot about those instances. :thumb:
Re: First letter on each sentence UCase
Quote:
Originally Posted by Code Doc
Terrific, anhn, but I notice that you are starting by converting all text to lower case before you start and that would seem to add a bit of complexity, thus turning the code into somewhat of an expert grammar/spell checker.
To me, that means that if the user wanted caps here and there, such as VB coded variables (e.g., SentenceCase above) that these caps would be eliminated. Please corect me if wrong.
Any way we use will have some limitations. Without converting to lowercase first such as in case of all UPPERCASE, after calling SentenCase() the text is still in all UPPERCASE. That cannot be called Sentence Case.
Check to see how MS-Word change case to Sentence Case.
Re: First letter on each sentence UCase
If you want to emulate Word, don't forget about the ellipsis. (...) It appears that Word leaves any text after an ellipsis in the same case it started. This won't work if you convert everything to lower case to start.
EDIT: The behavior changes if the text is all uppercase to start. The "leave it as it was" only applies to mixed-case text.
Re: First letter on each sentence UCase
This may be off the wall! But can you not exploit what is used by MS word as this deals with all these issues already ?
Re: First letter on each sentence UCase
Davadvice - You mean an API that Word uses?
Re: First letter on each sentence UCase
My Version...
Try This :)
Code:
Sub ConvertToSentenceCase()
Dim TestString As String, strg2 As String
TestString = "wake me up before you go go. i want to have my breakfast! don't forget."
'test the 1st 10 chars to get the first - incase the selection includes
'some spaces at the beginning.
For X = 1 To 10
strg2 = Mid(TestString, X, 1)
If strg2 Like "[a-zA-Z]" Then
Mid(TestString, X, 1) = UCase(strg2)
Exit For
End If
Next X
'test criteria (.!?) for the end of a sentence, then Capitalize the next letter.
For X = X To Len(TestString)
strg2 = Mid(TestString, X, 1)
If strg2 Like "[.!?]" Then
For Y = X + 1 To X + 10
strg2 = Mid(TestString, Y, 1)
If strg2 Like "[a-zA-Z]" Then
Mid(TestString, Y, 1) = UCase(strg2)
X = Y + 1
Exit For
End If
Next Y
End If
Next X
'This will give you the result
MsgBox TestString
End Sub
Re: First letter on each sentence UCase
Quote:
Originally Posted by GettinBetter
Davadvice - You mean an API that Word uses?
yeah, if the API will allow access to the area responsable for performing this action in Word then is there no way this can be used ?
I know that in Access there is the function to auto spell check then i asume Words API will allow a call to pass a string and return the formated text.
thus no need to reinvent the wheel as such ?
thanks
David