I need code that will Capitalize the first letter of every word

Posted on 2006-04-27
Last Modified: 2010-05-01

Can someone please supply me with vb code that will take a string and convert it so that the first letter of each word is capitalized - regardless of the case of the original string?

Question by:SAbboushi
    LVL 44

    Accepted Solution

    Hello SAbboushi,

    you could use StrConv
    StrConv("this is a test",vbProperCase)

    will produce
    This Is A Test

    hope this helps a bit
    LVL 44

    Expert Comment

    however if you want to prevent lowercasing the rest of the string use the tip here
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    If you want to preserve the existing case of the other letters then:

    Private Sub Command1_Click()
        Text1.Text = MyProperCase(Text1.Text)
    End Sub

    Private Function MyProperCase(ByVal str As String) As String
        Dim words As Variant
        words = Split(str, " ")
        Dim i As Integer
        For i = 0 To UBound(words)
            words(i) = UCase(Left(words(i), 1)) & Mid(words(i), 2)
        Next i
        MyProperCase = Join(words, " ")
    End Function

    Author Comment

    Thanks for your help.
    LVL 44

    Expert Comment

    glad i could help and thanks for the grade :)

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now