PDA

View Full Version : [SOLVED:] Trying to apply two types of word case formatting to TextBox



HTSCF Fareha
03-10-2021, 03:52 AM
I'm hoping that there is a way to format a user's input in a TextBox to firstly change each word to ProperCase, then locate the last word in the same TextBox and make that UpperCase.

This is required for the content control named "Keys" in the following:


Sub FillForm()
Dim oCtrl As Control
Dim oCC As ContentControl
Dim lngIndex As Long
Dim strTC As String

With m_oFrm

For Each oCtrl In .Controls

Select Case TypeName(oCtrl)
Case "TextBox"

If oCtrl.Name = "txtDescription" Then
Set oCC = ActiveDocument.SelectContentControlsByTag("Description").Item(1)
oCC.Range.Text = StrConv(oCtrl.Text, vbProperCase)

ElseIf oCtrl.Name = "txtKeys" Then
Set oCC = ActiveDocument.SelectContentControlsByTag("Keys").Item(1)
oCC.Range.Text = StrConv(oCtrl.Text, vbProperCase)

Else
ActiveDocument.SelectContentControlsByTag(Replace(oCtrl.Name, "txt", "")).Item(1).Range.Text = oCtrl.Text
End If

End Select
Next oCtrl
End With

lbl_Exit:
Exit Sub
End Sub

I don't know if there is a way of applying the two to a single Range?

gmaxey
03-10-2021, 11:05 AM
Something like this maybe:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim strDemo As String
Dim oRng As Range
strDemo = "cats dogs pigs goats and sheep"
Set oRng = ActiveDocument.SelectContentControlsByTitle("Demo").Item(1).Range
oRng.Text = StrConv(strDemo, vbProperCase)
oRng.Words.Last = UCase(oRng.Words.Last)
lbl_Exit:
Exit Sub
End Sub

HTSCF Fareha
03-10-2021, 02:48 PM
Greg, many thanks for this. Very much appreciated indeed.

I managed to modify your suggested code so that it fitted with my sub.


Sub FillForm()
Dim oCtrl As Control
Dim oCC As ContentControl
Dim lngIndex As Long
Dim strTC As String
Dim oRng As Range

With m_oFrm

For Each oCtrl In .Controls

Select Case TypeName(oCtrl)
Case "TextBox"

If oCtrl.Name = "txtDescription" Then
Set oCC = ActiveDocument.SelectContentControlsByTag("Description").Item(1)
oCC.Range.Text = StrConv(oCtrl.Text, vbProperCase)

ElseIf oCtrl.Name = "txtKeys" Then
Set oRng = ActiveDocument.SelectContentControlsByTag("Keys").Item(1).Range
oRng.Text = StrConv(oCtrl.Text, vbProperCase)
oRng.Words.Last = UCase(oRng.Words.Last)

Else
ActiveDocument.SelectContentControlsByTag(Replace(oCtrl.Name, "txt", "")).Item(1).Range.Text = oCtrl.Text
End If

End Select
Next oCtrl
End With

lbl_Exit:
Exit Sub
End Sub