Right, back to the topic at hand. Capitalising the first word of a sentence as you type. No-one seems to want o take up the RegExp challenge, so here's my version. Note that this a starting point rather than a complete solution, in that the English language has tons of exceptions to the basic rules, which this does not handle (although adding to the pattern being looked for is relatively straightforward). However, it should correcty handle most standard cases.
It takes a tiny bit of setting up, because it makes use of a very under-documented but, IMHO, powerful feature of the current Regular Expression library.
Here's the setup. Firstly, you need to add a reference to the Microsoft VBScript Regular Expressions Library (5.5 or later). Secondly, add a class module to your project (example assumes Class1), and drop in the following simple code:
[tt]
Option Explicit
Public Function ReplacerFunction(ParamArray a()) As String
ReplacerFunction = a(1) & UCase(a(2))
End Function
[/tt]
Now make this the default method of the class (Tools/Procedure Attributes/Advanced and set the ProcedureID to (default))
OK, we're all done with the support class. Here's the main capitalisation function:
[tt]
Option Explicit
Public Function SentenceCapper(strText As String) As String
Dim re As RegExp
Dim strUpper As String
Dim myReplacer As Class1
Set re = New RegExp
re.Global = True
re.MultiLine = True
re.Pattern = "(^|""|[\.!\?]\s+)(\w{1})"
Set myReplacer = New Class1
SentenceCapper = re.Replace(strText, myReplacer) ' This is where the feature gets used...
' Clean up to be on safe side
Set myReplacer = Nothing
Set re = Nothing
End Function
[/tt]
Right, you can now pass any string of text to this function and it should return a capitalised version.
To use it to work as you type (as per previous examples in this thread):
[tt]
Option Explicit
Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
Dim oldstart As Long
Dim strUpper As String
With Text1
oldstart = .SelStart
' Keep number of Text1 updates to a minimum...
' If we don't care about this we could have just done:
' .Text=SentenceCapper(.Text)
strUpper = SentenceCapper(.Text)
If strUpper <> .Text Then
.Text = strUpper
.SelStart = oldstart
End If
End With
End Sub
It takes a tiny bit of setting up, because it makes use of a very under-documented but, IMHO, powerful feature of the current Regular Expression library.
Here's the setup. Firstly, you need to add a reference to the Microsoft VBScript Regular Expressions Library (5.5 or later). Secondly, add a class module to your project (example assumes Class1), and drop in the following simple code:
[tt]
Option Explicit
Public Function ReplacerFunction(ParamArray a()) As String
ReplacerFunction = a(1) & UCase(a(2))
End Function
[/tt]
Now make this the default method of the class (Tools/Procedure Attributes/Advanced and set the ProcedureID to (default))
OK, we're all done with the support class. Here's the main capitalisation function:
[tt]
Option Explicit
Public Function SentenceCapper(strText As String) As String
Dim re As RegExp
Dim strUpper As String
Dim myReplacer As Class1
Set re = New RegExp
re.Global = True
re.MultiLine = True
re.Pattern = "(^|""|[\.!\?]\s+)(\w{1})"
Set myReplacer = New Class1
SentenceCapper = re.Replace(strText, myReplacer) ' This is where the feature gets used...
' Clean up to be on safe side
Set myReplacer = Nothing
Set re = Nothing
End Function
[/tt]
Right, you can now pass any string of text to this function and it should return a capitalised version.
To use it to work as you type (as per previous examples in this thread):
[tt]
Option Explicit
Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
Dim oldstart As Long
Dim strUpper As String
With Text1
oldstart = .SelStart
' Keep number of Text1 updates to a minimum...
' If we don't care about this we could have just done:
' .Text=SentenceCapper(.Text)
strUpper = SentenceCapper(.Text)
If strUpper <> .Text Then
.Text = strUpper
.SelStart = oldstart
End If
End With
End Sub