Case switching

I have a bad habit of hitting the CapsLock key and when I type code, the shift key does the opposite, so instead of typing m_bSomeVariable, I will look up and see that I have typed M_BsOMEvARIABLE. DevStudio provides <Shift><U> and <Ctrl><Shift><U> to lower and upper case a selection, but nothing provides a "SwapCase" function. The following macro will do the swap. I assigned it to <Alt><U>, but that is entirely up to you ....


Sub SwapCase()
	' DESCRIPTION: Macro to swap the case of selected words.
	'
	' David Little
	' COADE, Inc.
	' Houston, TX
	' dlittle@coade.com
	' 5OCT98
	'
	str = ActiveDocument.Selection.Text
	str1 = ""
	i = 1
	Do While (i < Len(str)+1)
		If (Mid(str, i, 1) = LCase(Mid(str, i, 1))) Then
			str1 = str1 + UCase(Mid(str, i, 1))
		Else
		str1 = str1 + LCase(Mid(str, i, 1))
		End If
		i = i + 1
	Loop
	ActiveDocument.Selection.Text = str1
End Sub


Comments

  • SwapCase macro updated for VS.NET

    Posted by Legacy on 07/08/2002 12:00am

    Originally posted by: David Little

    VS.NET has moved all the VB objects around, and I have updated the SwapCase macro for the new environment.  It works just like the old one..
    
    

    Sub SwapCase()
    'DESCRIPTION: Macro to swap the case of selected words.
    '
    ' David Little
    ' COADE, Inc.
    ' Houston, TX
    ' dlittle@coade.com
    ' 5OCT98
    ' Modified for VS.NET 8JULY2002
    '
    Dim objSel As TextSelection = DTE.ActiveDocument.Selection
    If objSel.IsEmpty Then
    Return ' nothing selected
    End If
    Dim str As String = objSel.Text
    Dim str1 As String = ""
    Dim i As Integer = 1
    Do While (i < Len(str) + 1)
    If (Mid(str, i, 1) = LCase(Mid(str, i, 1))) Then
    str1 = str1 + UCase(Mid(str, i, 1))
    Else
    str1 = str1 + LCase(Mid(str, i, 1))
    End If
    i = i + 1
    Loop
    objSel.Insert(str1, vsInsertFlags.vsInsertFlagsContainNewText)
    End Sub

    Reply
  • replacing instead of adding to

    Posted by Legacy on 12/26/2001 12:00am

    Originally posted by: Max MacLean

    When I ran the above macro, it appended the converted
    string to the end of the origanal string. I added

    ActiveDocument.Selection.Delete
    just before the
    ActiveDocument.Selection.Text = strNew

    Peace and Happiness


    Reply
  • Enhancement required...

    Posted by Legacy on 10/23/2001 12:00am

    Originally posted by: dmaly


    Is it possible to SELECT back the converted area ?

    Reply
  • Alternate Macro

    Posted by Legacy on 05/31/2001 12:00am

    Originally posted by: George

    Here is an alternate macro to swap the case of the selected text that doesn't use as many calls to the string functions:
    
    

    Sub SwapCase()
    ' DESCRIPTION: Swap the case of the current selection.

    strText = ActiveDocument.Selection.Text

    For i = 1 To Len(strText)

    ch = Asc(Mid(strText, i, 1))

    If (ch >= 65 And ch <= 90) Or (ch >= 97 And ch <= 122) Then ch = ch Xor 32

    strNew = strNew + Chr(ch)

    Next

    ActiveDocument.Selection.Text = strNew

    End Sub

    Reply
  • Case Switching with ease 1.0

    Posted by Legacy on 01/20/1999 12:00am

    Originally posted by: Farhan Noor Qureshi

    This macro is a very handy tool for me. Thanks David!

    The main problem is not about caps lock's wrong position on the key board, rather it is the programmer's attention on his work.

    Very good solution to a very annoying problem.


    Farhan Noor Qureshi

    Reply
  • Case swap macro comment

    Posted by Legacy on 11/30/1998 12:00am

    Originally posted by: Filip Dossche

    I have the same typing problem so this is really a neat bit of code, quite small and not really very difficult but "why did'nt I think of that myself !!"

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • The explosion in mobile devices and applications has generated a great deal of interest in APIs. Today's businesses are under increased pressure to make it easy to build apps, supply tools to help developers work more quickly, and deploy operational analytics so they can track users, developers, application performance, and more. Apigee Edge provides comprehensive API delivery tools and both operational and business-level analytics in an integrated platform. It is available as on-premise software or through …

  • Live Event Date: September 16, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you starting an on-premise-to-cloud data migration project? Have you thought about how much space you might need for your online platform or how to handle data that might be related to users who no longer exist? If these questions or any other concerns have been plaguing you about your migration project, check out this eSeminar. Join our speakers Betsy Bilhorn, VP, Product Management at Scribe, Mike Virnig, PowerSucess Manager and Michele …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds