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

1 Comment

Add your own comment
  1. May 8, 2008 8:10 am, link

    Ahhhh … the world is now a better place.

Please leave a comment