PDA

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



HTSCF Fareha
03-11-2021, 07:28 AM
Okay, I'm sure that there is a basic solution to this, but I've hit a complete brick wall that I cannot seem to knock down.

I have a scenario where a single TextBox might have two different types of input. These being "Yet to be determined.", which is the default and then any other entry. Both are targeted to the same Content Control.

The issue that I'm having is that the "Yet to be determined." needs to be formatted as per sentence, although the other option needs to be propercase with the last word uppercase (as per previous post 'Trying to apply two types of word case formatting to TextBox').

The nearest I get is the below, which shows the default as required, but "ignores" the second formatting case altogether.


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)

If 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)

ElseIf oCtrl.Name = "txtKeys" And InStr(1, "Yet to be determined.") Then
Set oRng = ActiveDocument.SelectContentControlsByTag("Keys").Item(1).Range
oRng.Text = StrConv(oCtrl.Text, vbLowerCase)
oRng.Words.First = UCase(oRng.Words.First)
End If

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

gmayor
03-12-2021, 02:40 AM
Try the following


For Each oCtrl In .Controls
If TypeName(oCtrl) = "TextBox" Then
If oCtrl.Name = "txtDescription" Then
Set oCC = ActiveDocument.SelectContentControlsByTag("Description").Item(1)
oCC.Range.Text = StrConv(oCtrl.Text, vbProperCase)
End If
If oCtrl.Name = "txtKeys" Then
If InStr(1, LCase(oCtrl.Text), "yet to be determined") > 0 Then
Set oRng = ActiveDocument.SelectContentControlsByTag("Keys").Item(1).Range
oRng.Text = StrConv(oCtrl.Text, vbLowerCase)
oRng.Words.First = UCase(oRng.Words.First)
Else
Set oRng = ActiveDocument.SelectContentControlsByTag("Keys").Item(1).Range
oRng.Text = StrConv(oCtrl.Text, vbProperCase)
oRng.Words.Last = UCase(oRng.Words.Last)
End If
Else
ActiveDocument.SelectContentControlsByTag(Replace(oCtrl.Name, "txt", "")).Item(1).Range.Text = oCtrl.Text
End If
End If
Next oCtrl

HTSCF Fareha
03-12-2021, 03:04 PM
Sorry Graham, but "Description" results in no format applied, "Keys" if it is left as is produces "Yet To Be Determined", although if alternative text is input this comes out as required.

I've been trying to fathom out why but have got no further forward.

gmaxey
03-12-2021, 07:13 PM
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim strTest As String
Dim oRng As Range
strTest = "Yet to be determined"
Set oRng = ActiveDocument.SelectContentControlsByTag("Keys").Item(1).Range
If InStr(strTest, "Yet to be determined") = 1 Then
oRng.Text = StrConv(strTest, vbLowerCase)
oRng.Words(1).Characters.First = UCase(oRng.Words(1).Characters.First)
Else
oRng.Text = StrConv(strTest, vbProperCase)
oRng.Words.Last = UCase(oRng.Words.Last)
End If
lbl_Exit:
Exit Sub
End Sub

gmayor
03-12-2021, 09:54 PM
Sorry Graham, but "Description" results in no format applied, "Keys" if it is left as is produces "Yet To Be Determined", although if alternative text is input this comes out as required.
I've been trying to fathom out why but have got no further forward.Post the document/template with the code and the userform.

HTSCF Fareha
03-13-2021, 12:27 PM
Many thanks to you both for your input.

This sub now works perfectly!


Sub FillForm()
Dim oCtrl As Control
Dim oCC As ContentControl
Dim oRng As Range
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 oRng = ActiveDocument.SelectContentControlsByTag("Description").Item(1).Range
oRng.Text = StrConv(oCtrl.Text, vbProperCase)
oRng.Words.Last = UCase(oRng.Words.Last)

ElseIf oCtrl.Name = "txtKeys" Then
Set oRng = ActiveDocument.SelectContentControlsByTag("Keys").Item(1).Range
If InStr(oCtrl.Text, "Yet to be determined.") = 1 Then
oRng.Text = StrConv(oCtrl.Text, vbLowerCase)
oRng.Words(1).Characters.First = UCase(oRng.Words(1).Characters.First)
Else
oRng.Text = StrConv(oCtrl.Text, vbProperCase)
oRng.Words.Last = UCase(oRng.Words.Last)
End If

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