Home Resume Excel Birds

M O R S E   C O D E

	*Passing a string to the translateString function outputs the morse code translation of that string
		expressed as a series of those classic not at all irritating microsoft beeps
	*See bottom of page for an example of a basic graphical use interface
	*This worksheet employs the standard wiring - button - on click event - call function
			
	Option Explicit

	Public Declare Function Beep Lib "kernel32" (ByVal dwFreq As Long, ByVal dwDuration As Long) As Long

	Private Enum Spaces
		Mono = 1
		Tri = 3
		Hepta = 7
	End Enum

	Private Sub beepMono()
		Beep 880, 100
	End Sub

	Private Sub beepTri()
		Beep 880, 300
	End Sub

	Private Sub silentTri()
		Beep 0, 300
	End Sub

	Private Sub beepHepta()
		Beep 880, 700
	End Sub

	Private Sub silentHepta()
		Beep 0, 700
	End Sub

	Public Sub translateString(stringToTranslate As String)
		Dim internationalMorseCodeValues As Dictionary
		Set internationalMorseCodeValues = New Dictionary

		internationalMorseCodeValues.Add "A", ".-"
		internationalMorseCodeValues.Add "B", "-..."
		internationalMorseCodeValues.Add "C", "-.-."
		internationalMorseCodeValues.Add "D", "-.."
		internationalMorseCodeValues.Add "E", "."
		internationalMorseCodeValues.Add "F", "..-."
		internationalMorseCodeValues.Add "G", "--."
		internationalMorseCodeValues.Add "H", "...."
		internationalMorseCodeValues.Add "I", ".."
		internationalMorseCodeValues.Add "J", ".---"
		internationalMorseCodeValues.Add "K", "-.-"
		internationalMorseCodeValues.Add "L", ".-.."
		internationalMorseCodeValues.Add "M", "--"
		internationalMorseCodeValues.Add "N", "-."
		internationalMorseCodeValues.Add "O", "---"
		internationalMorseCodeValues.Add "P", ".--."
		internationalMorseCodeValues.Add "Q", "--.-"
		internationalMorseCodeValues.Add "R", ".-."
		internationalMorseCodeValues.Add "S", "..."
		internationalMorseCodeValues.Add "T", "-"
		internationalMorseCodeValues.Add "U", "..-"
		internationalMorseCodeValues.Add "V", "...-"
		internationalMorseCodeValues.Add "W", ".--"
		internationalMorseCodeValues.Add "X", "-..-"
		internationalMorseCodeValues.Add "Y", "-.--"
		internationalMorseCodeValues.Add "Z", "--.."
		internationalMorseCodeValues.Add "a", ".-"
		internationalMorseCodeValues.Add "b", "-..."
		internationalMorseCodeValues.Add "c", "-.-."
		internationalMorseCodeValues.Add "d", "-.."
		internationalMorseCodeValues.Add "e", "."
		internationalMorseCodeValues.Add "f", "..-."
		internationalMorseCodeValues.Add "g", "--."
		internationalMorseCodeValues.Add "h", "...."
		internationalMorseCodeValues.Add "i", ".."
		internationalMorseCodeValues.Add "j", ".---"
		internationalMorseCodeValues.Add "k", "-.-"
		internationalMorseCodeValues.Add "l", ".-.."
		internationalMorseCodeValues.Add "m", "--"
		internationalMorseCodeValues.Add "n", "-."
		internationalMorseCodeValues.Add "o", "---"
		internationalMorseCodeValues.Add "p", ".--."
		internationalMorseCodeValues.Add "q", "--.-"
		internationalMorseCodeValues.Add "r", ".-."
		internationalMorseCodeValues.Add "s", "..."
		internationalMorseCodeValues.Add "t", "-"
		internationalMorseCodeValues.Add "u", "..-"
		internationalMorseCodeValues.Add "v", "...-"
		internationalMorseCodeValues.Add "w", ".--"
		internationalMorseCodeValues.Add "x", "-..-"
		internationalMorseCodeValues.Add "y", "-.--"
		internationalMorseCodeValues.Add "z", "--.."
		internationalMorseCodeValues.Add "0", "-----"
		internationalMorseCodeValues.Add "1", ".----"
		internationalMorseCodeValues.Add "2", "..---"
		internationalMorseCodeValues.Add "3", "...--"
		internationalMorseCodeValues.Add "4", "....-"
		internationalMorseCodeValues.Add "5", "....."
		internationalMorseCodeValues.Add "6", "-...."
		internationalMorseCodeValues.Add "7", "--..."
		internationalMorseCodeValues.Add "8", "---.."
		internationalMorseCodeValues.Add "9", "----."
		internationalMorseCodeValues.Add ",", "--..--"
		internationalMorseCodeValues.Add ".", ".-.-.-"
		internationalMorseCodeValues.Add "?", "..--.."
		internationalMorseCodeValues.Add """", ".-..-."
		internationalMorseCodeValues.Add ":", "---..."
		internationalMorseCodeValues.Add "'", ".----"
		internationalMorseCodeValues.Add "-", "-....-"
		internationalMorseCodeValues.Add "/", "-..-."
		internationalMorseCodeValues.Add "(", "-.--."
		internationalMorseCodeValues.Add ")", "-.--.-"
		internationalMorseCodeValues.Add "@", ".--.-."
    
		Dim index As Long
		Dim charI As String
		Dim charII As String
		For index = 1 To Len(stringToTranslate)
			charI = Mid(stringToTranslate, index, 1)
			charII = Mid(stringToTranslate, index + 1, 1)
			generateMorseCode charI, internationalMorseCodeValues
			If charII = " " Or charII = "." Or charII = "?" Or charII = "!" Then
				silentHepta
			Else
				silentTri
			End If
		Next index
	End Sub

	Private Sub generateMorseCode(char As String, internationalMorseCodeValues As Dictionary)
		Dim morseCode As String
		Dim charIII As String
		Dim index As Long
    
		morseCode = internationalMorseCodeValues(char)
		For index = 1 To Len(morseCode)
			charIII = Mid(morseCode, index, 1)
			If charIII = "." Then
				beepMono
			Else
				beepTri
			End If
		Next index
	End Sub