DK-Flag Erik Østergaard - Source code snippets / Kildekode småstykker Go to Home Page
 (Geometric Shapes) Source code snippets Return
  
  
Bottom of This Page

 

VBA

- source code snippets /
- kildekode småstykker

Education

 


(Geometric Shapes) BASIC source code snippets BASIC (Beginner's All-purpose Symbolic Instruction Code) source code snippets / BASIC (Beginner's All-purpose Symbolic Instruction Code) kildekode småstykker

- included the programming languages variants: Basic, VB (Visual Basic), VBA (Visual Basic for Applications), and VBS (VBScript). / - inkluderet programmeringssprog varianterne: Basic, VB (Visual Basic), VBA (Visual Basic for Applications) og VBS (VBScript).

Warning Warning / AdvarselWarning: Don't run the script files without reading them first! /
Warning / AdvarselAdvarsel: Kør ikke script-filerne uden at læse dem først!

VBA - source code snippets / VBA - kildekode småstykker

Change Case / Store/små bogstaver

Description: This is a collection of functions for use with Microsoft Office which Change Case of selected western characters in text etcetera among other things 'Sentence case', 'Title Case', and 'Invert (Toggle) Case'. / Beskrivelse: Dette er en samling af funktioner til brug med Microsoft Office som ændrer Store/små bogstaver af markerede vestlige tegn i tekst og så videre blandt andet 'Første bogstav i sætning med stort', 'Alle ord med stort begyndelsesbogstav' og 'Ombyt store og små bogstaver'.

Overview - Function(s)... / Oversigt - Funktion(er)...

Developed and tested under Microsoft Office Word 2003 - English version and for some functions only or also under Microsoft Office FrontPage 2003 - English version and Microsoft Office Publisher 2003 - English version. / Udviklet og testet under Microsoft Office Word 2003 - engelsk version og for nogle funktioner kun eller også under Microsoft Office FrontPage 2003 - engelsk version og Microsoft Office Publisher 2003 - engelsk version.

1 Implementation: (How to use:) Just paste the following code in a general module in your project: /
1 Implementering: (Sådan bruger du:) Bare indsæt følgende kode i et generelt modul i dit projekt:



' Change Cas>e<...
' Change Case of selected text.
' The shortcut keys are indicated by ">" and "<". Suggestion only.
'
' >S<TORE/små bogstaver
' Ændrer de valgte tegn til store eller små bogstaver.
' Genvejstasterne er angivet med ">" og "<". Kun som forslag.

' All variables must be declared before use.
Option Explicit

Public Function doCapitalize(vInputText As Variant) As Variant
On Error GoTo Err_doCapitalize
    ' This function assumes it's passed a text, and returns
    ' the text capitalized.
    ' Define variables.
    Dim IntValue As Long
    Dim vOutputText As Variant
    Dim vCharacter As Variant
    Dim vCharacterPrevious As Variant
    
    If IsNull(vInputText) = True Then
        doCapitalize = ""
        Exit Function
    End If
    
    vOutputText = ""
    For IntValue = 1 To Len(vInputText) Step 1
        If IntValue = 1 Then
            vOutputText = vOutputText + UCase(Mid(vInputText, IntValue, 1))
        Else
            vCharacter = Mid(vInputText, IntValue, 1)
            vCharacterPrevious = Mid(vInputText, (IntValue - 1), 1)
            Select Case vCharacterPrevious
                Case Is = Chr$(0), Chr$(9), Chr$(10), Chr$(11), Chr$(12), Chr$(13), Chr$(32)
                    ' StrConv Function recognizes these following characters
                    ' as word separators:
                    ' Null, Horizontal tab, Linefeed, Vertical tab,
                    ' Formfeed, Carriage return, and Space.
                    vOutputText = vOutputText + UCase(vCharacter)
                Case Is = "!", "%", "&", "/", "(", ")", "?", "+", "|", ";", ":", ",", ".", "-"
                    ' doCapitalize Function recognizes the above additional
                    ' characters as word separators.
                    vOutputText = vOutputText + UCase(vCharacter)
                Case Is = "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"
                    ' doCapitalize Function recognizes the above additional
                    ' characters as word separators.
                    vOutputText = vOutputText + UCase(vCharacter)
                Case Else
                    vOutputText = vOutputText + LCase(vCharacter)
            End Select
        End If
    Next IntValue
    
    doCapitalize = vOutputText
    
Exit_doCapitalize:
    Exit Function

Err_doCapitalize:
    MsgBox Err.Number & ": " & Err.Description
    Resume Exit_doCapitalize

End Function

Public Function ReplaceString(ByVal BigString As String, NewReplaceString As String, OldReplaceString As String) As String
On Error GoTo Err_ReplaceString
    ' Replaces the string 'OldReplaceString' through the String 'NewReplaceString' in the String
    ' 'BigString'.
    ' Define variables.
    Dim i%                          ' Iterator for loops.
    Dim OldReplLen%                 ' Length of 'OldReplaceString' - Numerical (integer) value.
    Dim BigLen%                     ' Length of 'BigString' - Numerical (integer) value.
    Dim CompareMode As Integer      ' Comparison Mode: Value 0 [Binary - case sensitive] or 1 [Text - case insensitive].
    CompareMode = 0                 ' Comparison Mode: Value 0 [Binary - case sensitive] or 1 [Text - case insensitive].

    If IsNull(BigString) = True Then
        ReplaceString = ""
        Exit Function
    End If

    If NewReplaceString <> OldReplaceString Then
        OldReplLen = Len(OldReplaceString)
        i = 1
        Do
            BigLen = Len(BigString)
            i = InStr(i, BigString, OldReplaceString, CompareMode) ' Search down a string from left to right until finding a string fragment matching the specified value. The index of the first character of the matching string is returned.
            If i <> 0 Then
                BigString = Mid(BigString, 1, i - 1) & NewReplaceString & Mid(BigString, i + OldReplLen, BigLen - i + 1 - OldReplLen)
                i = i + Len(NewReplaceString)
            End If
        Loop Until i = 0
    End If
    ReplaceString = BigString

Exit_ReplaceString:
    Exit Function

Err_ReplaceString:
    MsgBox Err.Number & ": " & Err.Description
    Resume Exit_ReplaceString

End Function

Public Function ReplaceStringExpanded(ByVal BigString As String, NewReplaceString As String, OldReplaceString As String, Match_caseBoolean As Boolean) As String
On Error GoTo Err_ReplaceStringExpanded
    ' Replaces the string 'OldReplaceString' through the String 'NewReplaceString' in the String
    ' 'BigString' by finding text having the given pattern of uppercase and lowercase letters by
    ' specifying the search criteria for 'Match_caseBoolean' - a (boolean) numeric value
    ' of numeric '0' (False) or numeric '1' (True) [Default: True].
    ' Define variables.
    Dim i%                          ' Iterator for loops.
    Dim OldReplLen%                 ' Length of 'OldReplaceString' - Numerical (integer) value.
    Dim BigLen%                     ' Length of 'BigString' - Numerical (integer) value.
    Dim tmpBigStr As String         ' Temporary text of 'BigString'.
    Dim tmpOldReplaceStr As String  ' Temporary text of 'OldReplaceString'.
    Dim CompareMode As Integer      ' Comparison Mode: Value 0 [Binary - case sensitive] or 1 [Text - case insensitive].
    CompareMode = 0                 ' Comparison Mode: Value 0 [Binary - case sensitive] or 1 [Text - case insensitive].

    If IsNull(BigString) = True Then
        ReplaceStringExpanded = ""
        Exit Function
    End If

    If Match_caseBoolean = True Then
        ' True (1).
        tmpBigStr = BigString
        tmpOldReplaceStr = OldReplaceString
    Else
        ' False (0).
        tmpBigStr = LCase(BigString)
        tmpOldReplaceStr = LCase(OldReplaceString)
    End If
    If NewReplaceString <> OldReplaceString Then
        OldReplLen = Len(OldReplaceString)
        i = 1
        Do
            BigLen = Len(BigString)
            i = InStr(i, tmpBigStr, tmpOldReplaceStr, CompareMode) ' Search down a string from left to right until finding a string fragment matching the specified value. The index of the first character of the matching string is returned.
            If i <> 0 Then
                tmpBigStr = Mid(tmpBigStr, 1, i - 1) & NewReplaceString & Mid(tmpBigStr, i + OldReplLen, BigLen - i + 1 - OldReplLen)
                BigString = Mid(BigString, 1, i - 1) & NewReplaceString & Mid(BigString, i + OldReplLen, BigLen - i + 1 - OldReplLen)
                i = i + Len(NewReplaceString)
            End If
        Loop Until i = 0
    End If
    ReplaceStringExpanded = BigString

Exit_ReplaceStringExpanded:
    Exit Function

Err_ReplaceStringExpanded:
    MsgBox Err.Number & ": " & Err.Description
    Resume Exit_ReplaceStringExpanded

End Function

Public Function changeCharInString(vInputText As Variant, vRemoveChar As Variant, vInsertChar As Variant) As Variant
On Error GoTo Err_changeCharInString
    ' Function to change (remove) a character from all places in
    ' a string where it occurs with an other character (or nothing).
    ' Define variables.
    Dim IntValue As Long
    Dim vOutputText As Variant
    Dim vCharacter As Variant

    If IsNull(vInputText) = True Then
        changeCharInString = ""
        Exit Function
    End If

    vOutputText = ""
    For IntValue = 1 To Len(vInputText) Step 1
        vCharacter = Mid(vInputText, IntValue, 1)
        If vCharacter <> vRemoveChar Then
            vOutputText = vOutputText + vCharacter
        Else
            vOutputText = vOutputText + vInsertChar
        End If
    Next IntValue

    changeCharInString = vOutputText
    
Exit_changeCharInString:
    Exit Function

Err_changeCharInString:
    MsgBox Err.Number & ": " & Err.Description
    Resume Exit_changeCharInString

End Function

Public Function doCountAndAdaptDanishCharactersLongMode(vInputText As Variant) As Variant
On Error GoTo Err_doCountAndAdaptDanishCharactersLongMode
    ' This function assumes it's passed a text, and returns
    ' and counts the characters of the text adapting some
    ' Danish characters etcetera to an English alphabet.
    ' For example "Ø" becomes corrected to "Oe" (long mode).
    '
    ' Denne funktion antager, at den får overført en tekst, og returnerer
    ' og tæller tekstens tegn tilpassende nogle danske karakterer
    ' et cetera til et engelsk alfabet.
    ' For eksempel "Ø" bliver rettet til "Oe" (lang måde).
    ' Define variables.
    Dim vOutputText As Variant
    
    If IsNull(vInputText) = True Then
        doCountAndAdaptDanishCharactersLongMode = ""
        Exit Function
    End If
    
    vOutputText = ""
    vOutputText = vInputText
    ' Change (remove) a character from all places in a string
    ' where it occurs with an other character (or nothing).
    ' Mode: Long.
    vOutputText = changeCharInString(vOutputText, "Æ", "Ae")
    vOutputText = changeCharInString(vOutputText, "æ", "ae")
    vOutputText = changeCharInString(vOutputText, "Ø", "Oe")
    vOutputText = changeCharInString(vOutputText, "ø", "oe")
    vOutputText = changeCharInString(vOutputText, "Å", "Aa")
    vOutputText = changeCharInString(vOutputText, "å", "aa")
    
    MsgBox Prompt:="Counted " & CStr(Len(vOutputText)) & " character(s) in selected text.", _
    Title:="Info", buttons:=vbOKOnly
    'MsgBox Prompt:="Optalte " & CStr(Len(vOutputText)) & " tegn i markeret tekst.", _
    Title:="Info", buttons:=vbOKOnly
    doCountAndAdaptDanishCharactersLongMode = vOutputText
    
Exit_doCountAndAdaptDanishCharactersLongMode:
    Exit Function

Err_doCountAndAdaptDanishCharactersLongMode:
    MsgBox Err.Number & ": " & Err.Description
    Resume Exit_doCountAndAdaptDanishCharactersLongMode

End Function

Public Function doCountAndAdaptDanishCharactersShortMode(vInputText As Variant) As Variant
On Error GoTo Err_doCountAndAdaptDanishCharactersShortMode
    ' This function assumes it's passed a text, and returns
    ' and counts the characters the text adapting some
    ' Danish characters etcetera to an English alphabet.
    ' For example "Ø" becomes corrected to "O" (short mode).
    '
    ' Denne funktion antager, at den får overført en tekst, og returnerer
    ' og tæller tekstens tegn tilpassende nogle danske karakterer
    ' et cetera til et engelsk alfabet.
    ' For eksempel "Ø" bliver rettet til "O" (kort måde).
    ' Define variables.
    Dim vOutputText As Variant
    
    If IsNull(vInputText) = True Then
        doCountAndAdaptDanishCharactersShortMode = ""
        Exit Function
    End If
    
    vOutputText = ""
    vOutputText = vInputText
    ' Change (remove) a character from all places in a string
    ' where it occurs with an other character (or nothing).
    ' Mode: Short.
    vOutputText = changeCharInString(vOutputText, "Æ", "E")
    vOutputText = changeCharInString(vOutputText, "æ", "e")
    vOutputText = changeCharInString(vOutputText, "Ø", "O")
    vOutputText = changeCharInString(vOutputText, "ø", "o")
    vOutputText = changeCharInString(vOutputText, "Å", "A")
    vOutputText = changeCharInString(vOutputText, "å", "a")
    
    MsgBox Prompt:="Counted " & CStr(Len(vOutputText)) & " character(s) in selected text.", _
    Title:="Info", buttons:=vbOKOnly
    'MsgBox Prompt:="Optalte " & CStr(Len(vOutputText)) & " tegn i markeret tekst.", _
    Title:="Info", buttons:=vbOKOnly
    doCountAndAdaptDanishCharactersShortMode = vOutputText
    
Exit_doCountAndAdaptDanishCharactersShortMode:
    Exit Function

Err_doCountAndAdaptDanishCharactersShortMode:
    MsgBox Err.Number & ": " & Err.Description
    Resume Exit_doCountAndAdaptDanishCharactersShortMode

End Function

Public Function doCountCharacters(vInputText As Variant) As Variant
On Error GoTo Err_doCountCharacters
    ' This function assumes it's passed a text, and returns
    ' and counts the characters of the text.
    '
    ' Denne funktion antager, at den får overført en tekst, og returnerer
    ' og tæller tekstens tegn.
    ' Define variables.
    Dim vOutputText As Variant
    
    If IsNull(vInputText) = True Then
        doCountCharacters = ""
        Exit Function
    End If
    
    vOutputText = ""
    vOutputText = vInputText
    
    MsgBox Prompt:="Counted " & CStr(Len(vOutputText)) & " character(s) in selected text.", _
    Title:="Info", buttons:=vbOKOnly
    'MsgBox Prompt:="Optalte " & CStr(Len(vOutputText)) & " tegn i markeret tekst.", _
    Title:="Info", buttons:=vbOKOnly
    doCountCharacters = vOutputText
    
Exit_doCountCharacters:
    Exit Function

Err_doCountCharacters:
    MsgBox Err.Number & ": " & Err.Description
    Resume Exit_doCountCharacters

End Function

Public Function doInvertCase(vInputText As Variant) As Variant
On Error GoTo Err_doInvertCase
    ' This function assumes it's passed a text, and returns
    ' the text inverted (toggled).
    ' Define variables.
    Dim IntValue As Long
    Dim vOutputText As Variant
    Dim vCharacter As Variant
    
    If IsNull(vInputText) = True Then
        doInvertCase = ""
        Exit Function
    End If
    
    vOutputText = ""
    For IntValue = 1 To Len(vInputText) Step 1
        vCharacter = Mid(vInputText, IntValue, 1)
        If LCase(vCharacter) = vCharacter Then
            vOutputText = vOutputText + UCase(vCharacter)
        Else
            vOutputText = vOutputText + LCase(vCharacter)
        End If
    Next IntValue
    
    doInvertCase = vOutputText
    
Exit_doInvertCase:
    Exit Function

Err_doInvertCase:
    MsgBox Err.Number & ": " & Err.Description
    Resume Exit_doInvertCase

End Function

Public Function doRandomCase(vInputText As Variant) As Variant
On Error GoTo Err_doRandomCase
    ' This function assumes it's passed a text, and returns
    ' the text cased at random.
    ' Define variables.
    Dim IntValue As Long
    Dim vOutputText As Variant
    Dim vCharacter As Variant
    
    If IsNull(vInputText) = True Then
        doRandomCase = ""
        Exit Function
    End If
    
    vOutputText = ""
    ' Syntax: Randomize [(Number)]. Randomize uses number to
    ' initialize the Rnd function's random-number generator,
    ' giving it a new seed value (an initial value used to
    ' generate pseudorandom numbers). If you omit number, the
    ' value returned by the system timer is used as the new
    ' seed value. If Randomize is not used, the Rnd function
    ' (with no arguments) uses the same number as a seed the
    ' first time it is called, and thereafter uses the last
    ' generated number as a seed value.
    Randomize    ' Initialize random-number generator.
    For IntValue = 1 To Len(vInputText) Step 1
        vCharacter = Mid(vInputText, IntValue, 1)
        ' Syntax: Rnd [(number)]. The Rnd function returns a
        ' value less than 1 but greater than or equal to zero.
        ' The value of number determines how Rnd generates a
        ' random number: For any given initial seed, the same
        ' number sequence is generated because each successive
        ' call to the Rnd function uses the previous number as
        ' a seed for the next number in the sequence. Before
        ' calling Rnd, use the Randomize statement without an
        ' argument to initialize the random-number generator
        ' with a seed based on the system timer.
        If Rnd < 0.5 Then
            vOutputText = vOutputText + LCase(vCharacter)
        Else
            vOutputText = vOutputText + UCase(vCharacter)
        End If
    Next IntValue
    
    doRandomCase = vOutputText
    
Exit_doRandomCase:
    Exit Function

Err_doRandomCase:
    MsgBox Err.Number & ": " & Err.Description
    Resume Exit_doRandomCase

End Function

Public Function doRemoveExtraInternalSpaces(vInputText As Variant) As Variant
On Error GoTo Err_doRemoveExtraInternalSpaces
    ' This function assumes it's passed a text, and returns
    ' the text with extra internal spaces removed.
    ' Define variables.
    Dim IntValue As Long
    Dim vOutputText As Variant
    Dim vCharacter As Variant
    
    If IsNull(vInputText) = True Then
        doRemoveExtraInternalSpaces = ""
        Exit Function
    End If
    
    vOutputText = ""
    For IntValue = 1 To Len(vInputText) Step 1
        vCharacter = Mid(vInputText, IntValue, 1)
        Select Case vCharacter
            Case Is = " "
                If Not Right(vOutputText, 1) = " " Then
                    vOutputText = vOutputText + vCharacter
                End If
            Case Else
                vOutputText = vOutputText + vCharacter
        End Select
    Next IntValue
    
    doRemoveExtraInternalSpaces = vOutputText
    
Exit_doRemoveExtraInternalSpaces:
    Exit Function

Err_doRemoveExtraInternalSpaces:
    MsgBox Err.Number & ": " & Err.Description
    Resume Exit_doRemoveExtraInternalSpaces

End Function

Public Function doRemoveExtraInternalUnderscores(vInputText As Variant) As Variant
On Error GoTo Err_doRemoveExtraInternalUnderscores
    ' This function assumes it's passed a text, and returns
    ' the text with extra internal underscores removed.
    ' Define variables.
    Dim IntValue As Long
    Dim vOutputText As Variant
    Dim vCharacter As Variant

    If IsNull(vInputText) = True Then
        doRemoveExtraInternalUnderscores = ""
        Exit Function
    End If

    vOutputText = ""
    For IntValue = 1 To Len(vInputText) Step 1
        vCharacter = Mid(vInputText, IntValue, 1)
        Select Case vCharacter
            Case Is = "_"
                If Not Right(vOutputText, 1) = "_" Then
                    vOutputText = vOutputText + vCharacter
                End If
            Case Else
                vOutputText = vOutputText + vCharacter
        End Select
    Next IntValue

    doRemoveExtraInternalUnderscores = vOutputText

Exit_doRemoveExtraInternalUnderscores:
    Exit Function

Err_doRemoveExtraInternalUnderscores:
    MsgBox Err.Number & ": " & Err.Description
    Resume Exit_doRemoveExtraInternalUnderscores

End Function

Public Function doReverse(vInputText As Variant) As Variant
On Error GoTo Err_doReverse
    ' This function assumes it's passed a text, and returns
    ' the text reversed.
    ' Define variables.
    Dim IntValue As Long
    Dim vOutputText As Variant
    Dim vCharacter As Variant
    
    If IsNull(vInputText) = True Then
        doReverse = ""
        Exit Function
    End If
    
    vOutputText = ""
    For IntValue = Len(vInputText) To 1 Step -1
        vCharacter = Mid(vInputText, IntValue, 1)
        vOutputText = vOutputText + vCharacter
    Next IntValue
    
    doReverse = vOutputText
    
Exit_doReverse:
    Exit Function

Err_doReverse:
    MsgBox Err.Number & ": " & Err.Description
    Resume Exit_doReverse

End Function

Public Function doReverse2ndVersion(vInputText As Variant) As Variant
On Error GoTo Err_doReverse2ndVersion
    ' This function assumes it's passed a text, and returns
    ' the text reversed. Second Version.
    ' Define variables.
    Dim IntValue As Long
    Dim vOutputText As Variant
    Dim vCharacter As Variant
    Dim inword As Boolean
    Dim word As Variant
    Dim inputTextLen As Long
    
    If IsNull(vInputText) = True Then
        doReverse2ndVersion = ""
        Exit Function
    End If
    
    vOutputText = ""
    inword = False    ' Stores state for 'InWord' - Boolean value.
    word = ""
    inputTextLen = Len(vInputText)
    For IntValue = inputTextLen To 1 Step -1
        vCharacter = Mid(vInputText, IntValue, 1)
        If isWhitespace(vCharacter) = True Or isDelimiter(vCharacter) = True Then
            If inword = True Then
                'MsgBox prompt:="Alert 1: " & word, _
                Title:="Info !", Buttons:=vbInformation + vbOKOnly    ' Only for the purpose of debugging.
                vOutputText = vOutputText + word
                word = ""
                inword = False
            End If
            vOutputText = vOutputText + vCharacter
        Else
            word = word + vCharacter
            inword = True
        End If
        If IntValue = 1 And inword = True Then
            'MsgBox prompt:="Alert 2: " & word, _
            Title:="Info !", Buttons:=vbInformation + vbOKOnly    ' Only for the purpose of debugging.
            vOutputText = vOutputText + word
            word = ""
            inword = False
        End If
    Next IntValue
    
    doReverse2ndVersion = vOutputText
    
Exit_doReverse2ndVersion:
    Exit Function

Err_doReverse2ndVersion:
    MsgBox Err.Number & ": " & Err.Description
    Resume Exit_doReverse2ndVersion

End Function

Public Function doReverseOnlyWords(vInputText As Variant) As Variant
On Error GoTo Err_doReverseOnlyWords
    ' This function assumes it's passed a text, and returns
    ' only the words in the text reversed.
    ' Define variables.
    Dim IntValue As Long
    Dim vOutputText As Variant
    Dim vCharacter As Variant
    Dim inword As Boolean
    Dim word As Variant
    Dim inputTextLen As Long
    
    If IsNull(vInputText) = True Then
        doReverseOnlyWords = ""
        Exit Function
    End If
    
    vOutputText = ""
    inword = False    ' Stores state for 'InWord' - Boolean value.
    word = ""
    inputTextLen = Len(vInputText)
    For IntValue = 1 To inputTextLen Step 1
        vCharacter = Mid(vInputText, IntValue, 1)
        If isWhitespace(vCharacter) = True Or isDelimiter(vCharacter) = True Then
            If inword = True Then
                'MsgBox prompt:="Alert 1: " & word, _
                Title:="Info !", Buttons:=vbInformation + vbOKOnly    ' Only for the purpose of debugging.
                vOutputText = vOutputText + doReverse(word)    ' Reverse the text.
                word = ""
                inword = False
            End If
            vOutputText = vOutputText + vCharacter
        Else
            word = word + vCharacter
            inword = True
        End If
        If IntValue = inputTextLen And inword = True Then
            'MsgBox prompt:="Alert 2: " & word, _
            Title:="Info !", Buttons:=vbInformation + vbOKOnly    ' Only for the purpose of debugging.
            vOutputText = vOutputText + doReverse(word)    ' Reverse the text.
            word = ""
            inword = False
        End If
    Next IntValue
    
    doReverseOnlyWords = vOutputText
    
Exit_doReverseOnlyWords:
    Exit Function

Err_doReverseOnlyWords:
    MsgBox Err.Number & ": " & Err.Description
    Resume Exit_doReverseOnlyWords

End Function

Public Function doReverseWords(vInputText As Variant) As Variant
On Error GoTo Err_doReverseWords
    ' This function assumes it's passed a text, and returns
    ' the words in the text reversed.
    ' Define variables.
    Dim IntValue As Long
    Dim vOutputText As Variant
    Dim vCharacter As Variant
    Dim inword As Boolean
    Dim word As Variant
    Dim inputTextLen As Long
    
    If IsNull(vInputText) = True Then
        doReverseWords = ""
        Exit Function
    End If
    
    vOutputText = ""
    inword = False    ' Stores state for 'InWord' - Boolean value.
    word = ""
    inputTextLen = Len(vInputText)
    For IntValue = inputTextLen To 1 Step -1
        vCharacter = Mid(vInputText, IntValue, 1)
        If isWhitespace(vCharacter) = True Or isDelimiter(vCharacter) = True Then
            If inword = True Then
                'MsgBox prompt:="Alert 1: " & word, _
                Title:="Info !", Buttons:=vbInformation + vbOKOnly    ' Only for the purpose of debugging.
                vOutputText = vOutputText + doReverse(word)    ' Reverse the text.
                word = ""
                inword = False
            End If
            vOutputText = vOutputText + vCharacter
        Else
            word = word + vCharacter
            inword = True
        End If
        If IntValue = 1 And inword = True Then
            'MsgBox prompt:="Alert 2: " & word, _
            Title:="Info !", Buttons:=vbInformation + vbOKOnly    ' Only for the purpose of debugging.
            vOutputText = vOutputText + doReverse(word)    ' Reverse the text.
            word = ""
            inword = False
        End If
    Next IntValue
    
    doReverseWords = vOutputText
    
Exit_doReverseWords:
    Exit Function

Err_doReverseWords:
    MsgBox Err.Number & ": " & Err.Description
    Resume Exit_doReverseWords

End Function

Public Function doSentenceCase(vInputText As Variant) As Variant
On Error GoTo Err_doSentenceCase
    ' This function assumes it's passed a text, and returns
    ' the text sentenced (first character of each selected text
    ' sentence upper case and the rest lower case).
    ' Define variables.
    Dim IntValue As Long
    Dim vOutputText As Variant
    Dim vCharacter As Variant
    Dim vCharacterPrevious As Variant
    Dim vCharacterSecondPrevious As Variant
    
    If IsNull(vInputText) = True Then
        doSentenceCase = ""
        Exit Function
    End If
    
    vOutputText = ""
    For IntValue = 1 To Len(vInputText) Step 1
        If IntValue = 1 Then
            vOutputText = vOutputText + UCase(Mid(vInputText, IntValue, 1))
        ElseIf IntValue = 2 Then
            vCharacter = Mid(vInputText, IntValue, 1)
            vCharacterPrevious = Mid(vInputText, (IntValue - 1), 1)
            Select Case vCharacterPrevious
                Case Is = Chr$(0), Chr$(10), Chr$(12), Chr$(13), Chr$(32)
                    ' In start string position = 2 only:
                    ' This Function recognizes these following characters
                    ' as sentence separators:
                    ' Null, Linefeed, Formfeed, Carriage return, and Space.
                    vOutputText = vOutputText + UCase(vCharacter)
                Case Is = "!", "?", "."
                    ' In start string position = 2 only:
                    ' This Function recognizes the above additional
                    ' characters as sentence separators.
                    vOutputText = vOutputText + UCase(vCharacter)
                Case Else
                    vOutputText = vOutputText + LCase(vCharacter)
            End Select
        Else
            vCharacter = Mid(vInputText, IntValue, 1)
            vCharacterPrevious = Mid(vInputText, (IntValue - 1), 1)
            vCharacterSecondPrevious = Mid(vInputText, (IntValue - 2), 1)
            Select Case vCharacterPrevious
                Case Is = Chr$(10), Chr$(12), Chr$(13)
                    ' This Function recognizes these following characters
                    ' as sentence separators:
                    ' Linefeed, Formfeed, and Carriage return.
                    vOutputText = vOutputText + UCase(vCharacter)
                Case Is = Chr$(0), Chr$(32)
                    ' This Function recognizes these following characters
                    ' as part of sentence separators:
                    ' Null and Space.
                    Select Case vCharacterSecondPrevious
                        Case Is = "!", "?", "."
                            ' This Function recognizes the above additional
                            ' characters as part of sentence separators.
                            vOutputText = vOutputText + UCase(vCharacter)
                        Case Else
                            vOutputText = vOutputText + LCase(vCharacter)
                    End Select
                Case Else
                    vOutputText = vOutputText + LCase(vCharacter)
            End Select
        End If
    Next IntValue
    
    doSentenceCase = vOutputText
    
Exit_doSentenceCase:
    Exit Function

Err_doSentenceCase:
    MsgBox Err.Number & ": " & Err.Description
    Resume Exit_doSentenceCase

End Function

Public Function isDelimiter(vInputCh As Variant) As Boolean
On Error GoTo Err_isDelimiter
    ' Validates the character passed to the function.
    ' Returns a Boolean value - if found: true (1) else false (0).
    ' Delimiter characters. Common delimiters include commas, forward
    ' or backward slashes, periods, and so on.
    
    If IsNull(vInputCh) = True Then
        isDelimiter = False
        Exit Function
    End If
    
    Select Case vInputCh
        Case Is = Chr$(34)
            ' This Function recognizes these following characters
            ' as delimiter characters:
            ' Quote.
            isDelimiter = True
        Case Is = ",", "?", "-", ".", "\", "/", ";", ":", "(", ")", "|", "+", "&", "%", "!", "[", "]", "{", "}", "=", "<", ">"
            ' This Function recognizes the above additional
            ' characters as delimiter characters.
            isDelimiter = True
        Case Else
            isDelimiter = False
    End Select

Exit_isDelimiter:
    Exit Function

Err_isDelimiter:
    MsgBox Err.Number & ": " & Err.Description
    Resume Exit_isDelimiter

End Function

Public Function isDelimiter2(vInputCh As Variant) As Boolean
On Error GoTo Err_isDelimiter2
    ' Validates the character passed to the function.
    ' Returns a Boolean value - if found: true (1) else false (0).
    ' Delimiter characters. Common delimiters include commas, forward
    ' or backward slashes, periods, and so on.
    
    If IsNull(vInputCh) = True Then
        isDelimiter2 = False
        Exit Function
    End If
    
    Select Case vInputCh
        Case Is = "!", "?", "."
            ' This Function recognizes the above
            ' characters as delimiter characters.
            isDelimiter2 = True
        Case Else
            isDelimiter2 = False
    End Select

Exit_isDelimiter2:
    Exit Function

Err_isDelimiter2:
    MsgBox Err.Number & ": " & Err.Description
    Resume Exit_isDelimiter2

End Function

Public Function isDelimiterNumber(v_InputCh As Variant) As Boolean
On Error GoTo Err_isDelimiterNumber
    ' Validates the character passed to the function.
    ' Returns a Boolean value - if found: true (1) else false (0).
    ' Delimiter characters. Common delimiters include commas, forward
    ' or backward slashes, periods, and so on.
    ' Define variables.
    Dim sInputCh As String
    
    If IsNull(v_InputCh) = True Then
        isDelimiterNumber = False
        Exit Function
    End If
    
    sInputCh = CStr(v_InputCh)    ' Force number to a string.
    Select Case sInputCh
        Case Is = "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"
            ' This Function recognizes the above
            ' characters as delimiter characters.
            isDelimiterNumber = True
        Case Else
            isDelimiterNumber = False
    End Select

Exit_isDelimiterNumber:
    Exit Function

Err_isDelimiterNumber:
    MsgBox Err.Number & ": " & Err.Description
    Resume Exit_isDelimiterNumber

End Function

Public Function isWhitespace(vInputCh As Variant) As Boolean
On Error GoTo Err_isWhitespace
    ' Validates the character passed to the function.
    ' Returns a Boolean value - if found: true (1) else false (0).
    '   This Function recognizes these following characters
    '   as whitespace characters or separators:
    '     Chr$(32): matches space
    '     Chr$(10): matches linefeed
    '     Chr$(13): matches carriage return
    '     Chr$(9) : matches horizontal tab
    '     Chr$(12): matches form-feed
    '     Chr$(11): matches vertical tab
    '     Chr$(8) : matches backspace
    ' Whitespace is usually defined as blank, or space, characters, and
    ' tabs. It may also include carriage returns and linefeeds, as well
    ' as certain other nondisplaying characters.
    ' NotInUse: Chr$(11),
    
    If IsNull(vInputCh) = True Then
        isWhitespace = False
        Exit Function
    End If
    
    Select Case vInputCh
        Case Is = Chr$(0), Chr$(8), Chr$(9), Chr$(10), Chr$(12), Chr$(13), Chr$(32)
            ' This Function recognizes these following characters
            ' as whitespace characters or separators:
            ' Null, Backspace, Horizontal Tab, Linefeed, Formfeed, Carriage return, and Space.
            isWhitespace = True
        Case Else
            isWhitespace = False
    End Select

Exit_isWhitespace:
    Exit Function

Err_isWhitespace:
    MsgBox Err.Number & ": " & Err.Description
    Resume Exit_isWhitespace

End Function

Conventions used for: Source code syntax highlighting. / Regler brugt til: Kildekode syntaks fremhævning.

2 Implementation: (How to use:) Now you can call the function name_of_desired_function(desired_function's_parameter(s)) from your project. /
2 Implementering: (Sådan bruger du:) Nu kan du kalde funktionen ønskede_funktions_navn(ønskede_funktions_parameter(re)) fra dit projekt.

or... / eller...

You can download a version of the source code from my Download page here: / Du kan downloade en version af kildekoden fra min Download side her:

[Basic] Download Change Case: ChCase.zip / [Basic] Download Store/små bogstaver: ChCase.zipOpen this link in new window / Åben dette link i nyt vindue

or... / eller...

See similar source code for JavaScript here: / Se tilsvarende kildekode for JavaScript her:

Change Case / Store/små bogstaver

See similar source code for Basic here: / Se tilsvarende kildekode for Basic her:

[Basic] Change Case / [Basic] Store/små bogstaver


(Geometric Shapes) Source code snippetsSource code snippets / Kildekode småstykker

- often included as functions for use in modules with program code, macros, and scripts etcetera. / - mange gange inkluderet som funktioner til brug i moduler med programkode, makroer og scripts og så videre.

The code might need some minor tweaks to run in your application. / Koden kan behøve nogle mindre ændringer for at kunne afvikles i dit anvendelsesområde.

Warning / Advarsel Licence: Free to use, but please share improvements. No warranty - use at own risk. /
Warning / Advarsel Licens: Fri brug, men del venligst forbedringer. Ingen garanti - brug på eget ansvar.

Warning: Don't run the script files without reading them first!
Total absence of any guarantee, warranty, or responsibility for script files, the script(s), the files they may produce, or the effects the script(s) or script-produced files may have. The script(s) is, after all, plain text. The burden is on the person using the script(s) to examine the script source code and determine whether or not a script is usable and safe. Operating systems and browsers are constantly changing. What works today may not work tomorrow!

Advarsel: Kør ikke script-filerne uden at læse dem først!
Totalt fravær af nogen form for garanti, garanti eller ansvar for script-filer, scriptet(scriptene), de filer, de kan producere eller de virkninger, scriptet(scriptene) eller scriptproducerede filer kan have. Scriptet(Scriptene) er, trods alt, almindelig tekst. Byrden er på brugeren af scriptet(scriptene) til at undersøge script kildekoden og afgøre, hvorvidt et script er brugbart og sikkert. Operativsystemer og browsere er under konstant forandring. Hvad fungerer i dag, fungerer muligvis ikke i morgen!


   Top of This Page
   Return
   Go to Home Page