Consulting

Results 1 to 3 of 3

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

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

    Trying to apply two types of word case formatting to TextBox

    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?

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

    Visit my website: http://gregmaxey.com

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

Posting Permissions

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