Consulting

Results 1 to 6 of 6

Thread: Trying to apply two types of word case formatting to same TextBox Content Control

  1. #1
    VBAX Mentor
    Joined
    Aug 2020
    Location
    Hampshire
    Posts
    395
    Location

    Trying to apply two types of word case formatting to same TextBox Content Control

    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

  2. #2
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Mentor
    Joined
    Aug 2020
    Location
    Hampshire
    Posts
    395
    Location
    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.

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    Quote Originally Posted by HTSCF Fareha View Post
    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.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  6. #6
    VBAX Mentor
    Joined
    Aug 2020
    Location
    Hampshire
    Posts
    395
    Location
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •