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

 

Basic

- 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!

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

Change Case / Store/små bogstaver

Description: This is a collection of functions for use with OpenOffice.org 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 OpenOffice.org 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 OpenOffice.org 2.3: Writer - Danish version. / Udviklet og testet under OpenOffice.org 2.3: Writer - dansk 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

Function doCapitalize(sInputText As String) As String
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 sOutputText As String
    Dim sCharacter As String
    Dim sCharacterPrevious As String

    If IsNull(sInputText) = True Then
        doCapitalize = ""
        Exit Function
    End If

    sOutputText = ""
    For IntValue = 1 To Len(sInputText) Step 1
        If IntValue = 1 Then
            sOutputText = sOutputText + UCase(Mid(sInputText, IntValue, 1))
        Else
            sCharacter = Mid(sInputText, IntValue, 1)
            sCharacterPrevious = Mid(sInputText, (IntValue - 1), 1)
            Select Case sCharacterPrevious
                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.
                    sOutputText = sOutputText + UCase(sCharacter)
                Case Is = "!", "%", "&", "/", "(", ")", "?", "+", "|", ";", ":", ",", ".", "-"
                    ' doCapitalize Function recognizes the above additional
                    ' characters as word separators.
                    sOutputText = sOutputText + UCase(sCharacter)
                Case Is = "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"
                    ' doCapitalize Function recognizes the above additional
                    ' characters as word separators.
                    sOutputText = sOutputText + UCase(sCharacter)
                Case Else
                    sOutputText = sOutputText + LCase(sCharacter)
            End Select
        End If
    Next IntValue

    doCapitalize = sOutputText

Exit_doCapitalize:
    Exit Function

Err_doCapitalize:
    MsgBox "Error " & err & ": " & error$ & chr$(13) & _
    "In line: " & Erl & "." & chr$(13) , (0 + 16), "Error / Fejl"
    Resume Exit_doCapitalize

End Function

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 "Error " & err & ": " & error$ & chr$(13) & _
    "In line: " & Erl & "." & chr$(13) , (0 + 16), "Error / Fejl"
    Resume Exit_ReplaceString

End Function

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 "Error " & err & ": " & error$ & chr$(13) & _
    "In line: " & Erl & "." & chr$(13) , (0 + 16), "Error / Fejl"
    Resume Exit_ReplaceStringExpanded

End Function

Function changeCharInString(sInputText As String, sRemoveChar As String, sInsertChar As String) As String
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 sOutputText As String
    Dim sCharacter As String

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

    sOutputText = ""
    For IntValue = 1 To Len(sInputText) Step 1
        sCharacter = Mid(sInputText, IntValue, 1)
        If sCharacter <> sRemoveChar Then
            sOutputText = sOutputText + sCharacter
        Else
            sOutputText = sOutputText + sInsertChar
        End If
    Next IntValue

    changeCharInString = sOutputText

Exit_changeCharInString:
    Exit Function

Err_changeCharInString:
    MsgBox "Error " & err & ": " & error$ & chr$(13) & _
    "In line: " & Erl & "." & chr$(13) , (0 + 16), "Error / Fejl"
    Resume Exit_changeCharInString

End Function

Function doCountAndAdaptDanishCharactersLongMode(sInputText As String) As String
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 sOutputText As String

    If IsNull(sInputText) = True Then
        doCountAndAdaptDanishCharactersLongMode = ""
        Exit Function
    End If

    sOutputText = ""
    sOutputText = sInputText
    ' Change (remove) a character from all places in a string
    ' where it occurs with an other character (or nothing).
    ' Mode: Long.
    sOutputText = changeCharInString(sOutputText, "Æ", "Ae")
    sOutputText = changeCharInString(sOutputText, "æ", "ae")
    sOutputText = changeCharInString(sOutputText, "Ø", "Oe")
    sOutputText = changeCharInString(sOutputText, "ø", "oe")
    sOutputText = changeCharInString(sOutputText, "Å", "Aa")
    sOutputText = changeCharInString(sOutputText, "å", "aa")

'    MsgBox("Counted " & CStr(Len(sOutputText)) & " character(s) in selected text." , (0 + 64), "Info")
    MsgBox("Optalte " & CStr(Len(sOutputText)) & " tegn i markeret tekst." , (0 + 64), "Info")
    doCountAndAdaptDanishCharactersLongMode = sOutputText

Exit_doCountAndAdaptDanishCharactersLongMode:
    Exit Function

Err_doCountAndAdaptDanishCharactersLongMode:
    MsgBox "Error " & err & ": " & error$ & chr$(13) & _
    "In line: " & Erl & "." & chr$(13) , (0 + 16), "Error / Fejl"
    Resume Exit_doCountAndAdaptDanishCharactersLongMode

End Function

Function doCountAndAdaptDanishCharactersShortMode(sInputText As String) As String
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 sOutputText As String

    If IsNull(sInputText) = True Then
        doCountAndAdaptDanishCharactersShortMode = ""
        Exit Function
    End If

    sOutputText = ""
    sOutputText = sInputText
    ' Change (remove) a character from all places in a string
    ' where it occurs with an other character (or nothing).
    ' Mode: Short.
    sOutputText = changeCharInString(sOutputText, "Æ", "E")
    sOutputText = changeCharInString(sOutputText, "æ", "e")
    sOutputText = changeCharInString(sOutputText, "Ø", "O")
    sOutputText = changeCharInString(sOutputText, "ø", "o")
    sOutputText = changeCharInString(sOutputText, "Å", "A")
    sOutputText = changeCharInString(sOutputText, "å", "a")

'    MsgBox("Counted " & CStr(Len(sOutputText)) & " character(s) in selected text." , (0 + 64), "Info")
    MsgBox("Optalte " & CStr(Len(sOutputText)) & " tegn i markeret tekst." , (0 + 64), "Info")
    doCountAndAdaptDanishCharactersShortMode = sOutputText

Exit_doCountAndAdaptDanishCharactersShortMode:
    Exit Function

Err_doCountAndAdaptDanishCharactersShortMode:
    MsgBox "Error " & err & ": " & error$ & chr$(13) & _
    "In line: " & Erl & "." & chr$(13) , (0 + 16), "Error / Fejl"
    Resume Exit_doCountAndAdaptDanishCharactersShortMode

End Function

Function doCountCharacters(sInputText As String) As String
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 sOutputText As String

    If IsNull(sInputText) = True Then
        doCountCharacters = ""
        Exit Function
    End If

    sOutputText = ""
    sOutputText = sInputText

'    MsgBox("Counted " & CStr(Len(sOutputText)) & " character(s) in selected text." , (0 + 64), "Info")
    MsgBox("Optalte " & CStr(Len(sOutputText)) & " tegn i markeret tekst." , (0 + 64), "Info")
    doCountCharacters = sOutputText

Exit_doCountCharacters:
    Exit Function

Err_doCountCharacters:
    MsgBox "Error " & err & ": " & error$ & chr$(13) & _
    "In line: " & Erl & "." & chr$(13) , (0 + 16), "Error / Fejl"
    Resume Exit_doCountCharacters

End Function

Function doInvertCase(sInputText As String) As String
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 sOutputText As String
    Dim sCharacter As String

    If IsNull(sInputText) = True Then
        doInvertCase = ""
        Exit Function
    End If

    sOutputText = ""
    For IntValue = 1 To Len(sInputText) Step 1
        sCharacter = Mid(sInputText, IntValue, 1)
        If LCase(sCharacter) = sCharacter Then
            sOutputText = sOutputText + UCase(sCharacter)
        Else
            sOutputText = sOutputText + LCase(sCharacter)
        End If
    Next IntValue

    doInvertCase = sOutputText

Exit_doInvertCase:
    Exit Function

Err_doInvertCase:
    MsgBox "Error " & err & ": " & error$ & chr$(13) & _
    "In line: " & Erl & "." & chr$(13) , (0 + 16), "Error / Fejl"
    Resume Exit_doInvertCase

End Function

Function doRandomCase(sInputText As String) As String
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 sOutputText As String
    Dim sCharacter As String

    If IsNull(sInputText) = True Then
        doRandomCase = ""
        Exit Function
    End If

    sOutputText = ""
    ' 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(sInputText) Step 1
        sCharacter = Mid(sInputText, 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
            sOutputText = sOutputText + LCase(sCharacter)
        Else
            sOutputText = sOutputText + UCase(sCharacter)
        End If
    Next IntValue

    doRandomCase = sOutputText

Exit_doRandomCase:
    Exit Function

Err_doRandomCase:
    MsgBox "Error " & err & ": " & error$ & chr$(13) & _
    "In line: " & Erl & "." & chr$(13) , (0 + 16), "Error / Fejl"
    Resume Exit_doRandomCase

End Function

Function doRemoveExtraInternalSpaces(sInputText As String) As String
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 sOutputText As String
    Dim sCharacter As String

    If IsNull(sInputText) = True Then
        doRemoveExtraInternalSpaces = ""
        Exit Function
    End If

    sOutputText = ""
    For IntValue = 1 To Len(sInputText) Step 1
        sCharacter = Mid(sInputText, IntValue, 1)
        Select Case sCharacter
            Case Is = " "
                If Right(sOutputText, 1) <> " " Then
                    sOutputText = sOutputText + sCharacter
                End If
            Case Else
                sOutputText = sOutputText + sCharacter
        End Select
    Next IntValue

    doRemoveExtraInternalSpaces = sOutputText

Exit_doRemoveExtraInternalSpaces:
    Exit Function

Err_doRemoveExtraInternalSpaces:
    MsgBox "Error " & err & ": " & error$ & chr$(13) & _
    "In line: " & Erl & "." & chr$(13) , (0 + 16), "Error / Fejl"
    Resume Exit_doRemoveExtraInternalSpaces

End Function

Function doRemoveExtraInternalUnderscores(sInputText As String) As String
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 sOutputText As String
    Dim sCharacter As String

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

    sOutputText = ""
    For IntValue = 1 To Len(sInputText) Step 1
        sCharacter = Mid(sInputText, IntValue, 1)
        Select Case sCharacter
            Case Is = "_"
                If Right(sOutputText, 1) <> "_" Then
                    sOutputText = sOutputText + sCharacter
                End If
            Case Else
                sOutputText = sOutputText + sCharacter
        End Select
    Next IntValue

    doRemoveExtraInternalUnderscores = sOutputText

Exit_doRemoveExtraInternalUnderscores:
    Exit Function

Err_doRemoveExtraInternalUnderscores:
    MsgBox "Error " & err & ": " & error$ & chr$(13) & _
    "In line: " & Erl & "." & chr$(13) , (0 + 16), "Error / Fejl"
    Resume Exit_doRemoveExtraInternalUnderscores

End Function

Function doReverse(sInputText As String) As String
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 sOutputText As String
    Dim sCharacter As String

    If IsNull(sInputText) = True Then
        doReverse = ""
        Exit Function
    End If

    sOutputText = ""
    For IntValue = Len(sInputText) To 1 Step -1
        sCharacter = Mid(sInputText, IntValue, 1)
        sOutputText = sOutputText + sCharacter
    Next IntValue

    doReverse = sOutputText

Exit_doReverse:
    Exit Function

Err_doReverse:
    MsgBox "Error " & err & ": " & error$ & chr$(13) & _
    "In line: " & Erl & "." & chr$(13) , (0 + 16), "Error / Fejl"
    Resume Exit_doReverse

End Function

Function doReverse2ndVersion(sInputText As String) As String
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 sOutputText As String
    Dim sCharacter As String
    Dim inword As Boolean
    Dim word As String
    Dim inputTextLen As Long
    
    If IsNull(sInputText) = True Then
        doReverse2ndVersion = ""
        Exit Function
    End If
    
    sOutputText = ""
    inword = False    ' Stores state for 'InWord' - Boolean value.
    word = ""
    inputTextLen = Len(sInputText)
    For IntValue = inputTextLen To 1 Step -1
        sCharacter = Mid(sInputText, IntValue, 1)
        If isWhitespace(sCharacter) = True Or isDelimiter(sCharacter) = True Then
            If inword = True Then
                'MsgBox("Alert 1: " & word , (0 + 64), "Info !")    ' Only for the purpose of debugging.
                sOutputText = sOutputText + word
                word = ""
                inword = False
            End If
            sOutputText = sOutputText + sCharacter
        Else
            word = word + sCharacter
            inword = True
        End If
        If IntValue = 1 And inword = True Then
            'MsgBox("Alert 2: " & word , (0 + 64), "Info !")    ' Only for the purpose of debugging.
            sOutputText = sOutputText + word
            word = ""
            inword = False
        End If
    Next IntValue
    
    doReverse2ndVersion = sOutputText
    
Exit_doReverse2ndVersion:
    Exit Function

Err_doReverse2ndVersion:
    MsgBox "Error " & err & ": " & error$ & chr$(13) & _
    "In line: " & Erl & "." & chr$(13) , (0 + 16), "Error / Fejl"
    Resume Exit_doReverse2ndVersion

End Function

Function doReverseOnlyWords(sInputText As String) As String
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 sOutputText As String
    Dim sCharacter As String
    Dim inword As Boolean
    Dim word As String
    Dim inputTextLen As Long
    
    If IsNull(sInputText) = True Then
        doReverseOnlyWords = ""
        Exit Function
    End If
    
    sOutputText = ""
    inword = False    ' Stores state for 'InWord' - Boolean value.
    word = ""
    inputTextLen = Len(sInputText)
    For IntValue = 1 To inputTextLen Step 1
        sCharacter = Mid(sInputText, IntValue, 1)
        If isWhitespace(sCharacter) = True Or isDelimiter(sCharacter) = True Then
            If inword = True Then
                'MsgBox("Alert 1: " & word , (0 + 64), "Info !")    ' Only for the purpose of debugging.
                sOutputText = sOutputText + doReverse(word)    ' Reverse the text.
                word = ""
                inword = False
            End If
            sOutputText = sOutputText + sCharacter
        Else
            word = word + sCharacter
            inword = True
        End If
        If IntValue = inputTextLen And inword = True Then
            'MsgBox("Alert 2: " & word , (0 + 64), "Info !")    ' Only for the purpose of debugging.
            sOutputText = sOutputText + doReverse(word)    ' Reverse the text.
            word = ""
            inword = False
        End If
    Next IntValue
    
    doReverseOnlyWords = sOutputText
    
Exit_doReverseOnlyWords:
    Exit Function

Err_doReverseOnlyWords:
    MsgBox "Error " & err & ": " & error$ & chr$(13) & _
    "In line: " & Erl & "." & chr$(13) , (0 + 16), "Error / Fejl"
    Resume Exit_doReverseOnlyWords

End Function

Function doReverseWords(sInputText As String) As String
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 sOutputText As String
    Dim sCharacter As String
    Dim inword As Boolean
    Dim word As String
    Dim inputTextLen As Long
    
    If IsNull(sInputText) = True Then
        doReverseWords = ""
        Exit Function
    End If
    
    sOutputText = ""
    inword = False    ' Stores state for 'InWord' - Boolean value.
    word = ""
    inputTextLen = Len(sInputText)
    For IntValue = inputTextLen To 1 Step -1
        sCharacter = Mid(sInputText, IntValue, 1)
        If isWhitespace(sCharacter) = True Or isDelimiter(sCharacter) = True Then
            If inword = True Then
                'MsgBox("Alert 1: " & word , (0 + 64), "Info !")    ' Only for the purpose of debugging.
                sOutputText = sOutputText + doReverse(word)    ' Reverse the text.
                word = ""
                inword = False
            End If
            sOutputText = sOutputText + sCharacter
        Else
            word = word + sCharacter
            inword = True
        End If
        If IntValue = 1 And inword = True Then
            'MsgBox("Alert 2: " & word , (0 + 64), "Info !")    ' Only for the purpose of debugging.
            sOutputText = sOutputText + doReverse(word)    ' Reverse the text.
            word = ""
            inword = False
        End If
    Next IntValue
    
    doReverseWords = sOutputText
    
Exit_doReverseWords:
    Exit Function

Err_doReverseWords:
    MsgBox "Error " & err & ": " & error$ & chr$(13) & _
    "In line: " & Erl & "." & chr$(13) , (0 + 16), "Error / Fejl"
    Resume Exit_doReverseWords

End Function

Function doSentenceCase(sInputText As String) As String
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 sOutputText As String
    Dim sCharacter As String
    Dim sCharacterPrevious As String
    Dim sCharacterSecondPrevious As String

    If IsNull(sInputText) = True Then
        doSentenceCase = ""
        Exit Function
    End If

    sOutputText = ""
    For IntValue = 1 To Len(sInputText) Step 1
        If IntValue = 1 Then
            sOutputText = sOutputText + UCase(Mid(sInputText, IntValue, 1))
        ElseIf IntValue = 2 Then
            sCharacter = Mid(sInputText, IntValue, 1)
            sCharacterPrevious = Mid(sInputText, (IntValue - 1), 1)
            Select Case sCharacterPrevious
                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.
                    sOutputText = sOutputText + UCase(sCharacter)
                Case Is = "!", "?", "."
                    ' In start string position = 2 only:
                    ' This Function recognizes the above additional
                    ' characters as sentence separators.
                    sOutputText = sOutputText + UCase(sCharacter)
                Case Else
                    sOutputText = sOutputText + LCase(sCharacter)
            End Select
        Else
            sCharacter = Mid(sInputText, IntValue, 1)
            sCharacterPrevious = Mid(sInputText, (IntValue - 1), 1)
            sCharacterSecondPrevious = Mid(sInputText, (IntValue - 2), 1)
            Select Case sCharacterPrevious
                Case Is = Chr$(10), Chr$(12), Chr$(13)
                    ' This Function recognizes these following characters
                    ' as sentence separators:
                    ' Linefeed, Formfeed, and Carriage return.
                    sOutputText = sOutputText + UCase(sCharacter)
                Case Is = Chr$(0), Chr$(32)
                    ' This Function recognizes these following characters
                    ' as part of sentence separators:
                    ' Null and Space.
                    Select Case sCharacterSecondPrevious
                        Case Is = "!", "?", "."
                            ' This Function recognizes the above additional
                            ' characters as part of sentence separators.
                            sOutputText = sOutputText + UCase(sCharacter)
                        Case Else
                            sOutputText = sOutputText + LCase(sCharacter)
                    End Select
                Case Else
                    sOutputText = sOutputText + LCase(sCharacter)
            End Select
        End If
    Next IntValue

    doSentenceCase = sOutputText

Exit_doSentenceCase:
    Exit Function

Err_doSentenceCase:
    MsgBox "Error " & err & ": " & error$ & chr$(13) & _
    "In line: " & Erl & "." & chr$(13) , (0 + 16), "Error / Fejl"
    Resume Exit_doSentenceCase

End Function

Function isDelimiter(sInputCh As String) 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(sInputCh) = True Then
        isDelimiter = False
        Exit Function
    End If
    
    Select Case sInputCh
        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 "Error " & err & ": " & error$ & chr$(13) & _
    "In line: " & Erl & "." & chr$(13) , (0 + 16), "Error / Fejl"
    Resume Exit_isDelimiter

End Function

Function isDelimiter2(sInputCh As String) 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(sInputCh) = True Then
        isDelimiter2 = False
        Exit Function
    End If
    
    Select Case sInputCh
        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 "Error " & err & ": " & error$ & chr$(13) & _
    "In line: " & Erl & "." & chr$(13) , (0 + 16), "Error / Fejl"
    Resume Exit_isDelimiter2

End Function

Function isDelimiterNumber(s_InputCh As String) 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(s_InputCh) = True Then
        isDelimiterNumber = False
        Exit Function
    End If
    
    sInputCh = CStr(s_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 "Error " & err & ": " & error$ & chr$(13) & _
    "In line: " & Erl & "." & chr$(13) , (0 + 16), "Error / Fejl"
    Resume Exit_isDelimiterNumber

End Function

Function isWhitespace(sInputCh As String) 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(sInputCh) = True Then
        isWhitespace = False
        Exit Function
    End If
    
    Select Case sInputCh
        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 "Error " & err & ": " & error$ & chr$(13) & _
    "In line: " & Erl & "." & chr$(13) , (0 + 16), "Error / Fejl"
    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 VBA here: / Se tilsvarende kildekode for VBA her:

[VBA] Change Case / [VBA] 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