Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1469

(VB6) Turn multiline text into String constant

$
0
0
This is an auxiliary code that takes a text from the clipboard and generates code for a constant declaration, then copies it back to the clipboard converted.

Code:

Option Explicit

Private Function GetStringConstantCode(nText As String, nConstantName As String) As String
    Dim s() As String
    Dim c As Long
    Dim s2() As String
    Dim iNumberOfConstants As Long
    Dim iCurrentContantNumber As Long
    Dim n As Long
    Dim c2 As Long
    Dim iConstantsStr() As String
   
    s = Split(Replace(nText, """", """"""), vbCrLf)
    iNumberOfConstants = -Int((-UBound(s) + 1) / 24)
   
    iConstantsStr = Split("")
   
    iCurrentContantNumber = 1
    For c = 0 To UBound(s)
        If (c + 1) Mod 24 = 1 Then
            If c > 0 Then
                iCurrentContantNumber = iCurrentContantNumber + 1
                ReDim Preserve iConstantsStr(UBound(iConstantsStr) + 1)
                iConstantsStr(UBound(iConstantsStr)) = Join(s2, vbCrLf)
            End If
            If (UBound(s) - c) < 23 Then
                n = (UBound(s) - c)
            Else
                n = 23
            End If
            ReDim s2(n)
            c2 = 0
            s2(c2) = "Private Const " & nConstantName & IIf((iNumberOfConstants > 1) And (iCurrentContantNumber < iNumberOfConstants), CStr(iCurrentContantNumber), "") & " As String = " & IIf(iNumberOfConstants > 1 And (iCurrentContantNumber > 1), nConstantName & CStr(iCurrentContantNumber - 1) & " & ", "") & IIf(s(c) <> "", """" & s(c) & """ & ", "") & "vbCrLf & _"
        ElseIf (c2 + 1) = UBound(s2) Then
            c2 = c2 + 1
            s2(c2) = "    """ & s(c) & """ & vbCrLf"
        Else
            c2 = c2 + 1
            s2(c2) = "    """ & s(c) & """ & vbCrLf &" & " _"
        End If
    Next
    ReDim Preserve iConstantsStr(UBound(iConstantsStr) + 1)
    iConstantsStr(UBound(iConstantsStr)) = Join(s2, vbCrLf)
    If Right(iConstantsStr(UBound(iConstantsStr)), 12) = "& vbCrLf & _" Then
        iConstantsStr(UBound(iConstantsStr)) = Left$(iConstantsStr(UBound(iConstantsStr)), Len(iConstantsStr(UBound(iConstantsStr))) - 12)
    End If
   
    GetStringConstantCode = Join(iConstantsStr, vbCrLf)
End Function

Private Sub Command1_Click()
    Dim iConstantName As String
    Dim iText As String
   
    iConstantName = InputBox("Please enter the Name of the constant.", "Constant name", "cConstName")
    If iConstantName = "" Then Exit Sub
   
    iText = Clipboard.GetText
    Clipboard.Clear
    Clipboard.SetText GetStringConstantCode(iText, iConstantName)
End Sub

It does not take into account the VB6 line limitation and it uses one line of code for each line of text.

It could be useful for someone.
Attached Files

Viewing all articles
Browse latest Browse all 1469

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>