Visual Studio Macro for Sentence Case

Wednesday, May 7th, 2008 at 4:10 pm

I can’t believe I couldn’t find this, so I wrote one:

    Sub SentenceCase()
        Dim textSelection As EnvDTE.TextSelection
        Dim newString As String = ""
        Dim firstCase As Boolean = False

        textSelection = CType(DTE.ActiveDocument.Selection(), EnvDTE.TextSelection)
        For Each ch As Char In textSelection.Text.ToCharArray()
            If Not firstCase And Char.IsLetter(ch) Then
                ch = Char.ToUpper(ch)
                firstCase = True
            ElseIf firstCase And Char.IsLetter(ch) Then
                ch = Char.ToLower(ch)
            End If
            newString = newString + ch
        Next

        textSelection.Text = newString
    End Sub