Results 1 to 12 of 12

Thread: (VB6) Turn multiline text into String constant

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,996

    (VB6) Turn multiline text into String constant

    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.

    It could be useful for someone.
    Attached Files Attached Files

  2. #2
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,746

    Re: (VB6) Turn multiline text into String constant

    There is a limit on the length of each line, and I hope to maximize the full character string in each line.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,996

    Re: (VB6) Turn multiline text into String constant

    Quote Originally Posted by xiaoyao View Post
    There is a limit on the length of each line, and I hope to maximize the full character string in each line.
    Ah, OK, that's exactly what my script does not do.
    I've thought about adding that option, but I didn't need it at the time.

    Each line can be up to 2024 characters, then you need to split.

  4. #4
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,746

    Re: (VB6) Turn multiline text into String constant

    Code:
    Function StrToVbCode_(ByVal Str As String, VarName As String, Optional ByConst As Boolean) As String
    Dim CodeArr() As String, Arr() As String, UB As Long, I As Long
    Str = Replace(Str, Chr(34), Chr(34) & Chr(34))
    While Right(Str, 2) = vbCrLf
        Str = Left(Str, Len(Str) - 2)
    Wend
    
    
    Arr = Split(Str, vbCrLf)
    UB = UBound(Arr)
    ReDim CodeArr(UB)
    If ByConst Then
        CodeArr(0) = "Private Const " & VarName & " As String = "
    Else
        CodeArr(0) = "Dim " & VarName & " As String : " & VarName & " = "
    End If
    CodeArr(0) = CodeArr(0) & Chr(34) & Arr(0) & Chr(34) & IIf(UB = 0, "", " & vbcrlf &  _")
    
    For I = 1 To UB - 1
        CodeArr(I) = Chr(34) & Arr(I) & Chr(34) & " & VbCrlf & _"
    Next
    If UB > 0 Then
        If UB = 1 Then I = 1
        CodeArr(I) = Chr(34) & Arr(I) & Chr(34)
    End If
    StrToVbCode_ = Join(CodeArr, vbCrLf)
    End Function

  5. #5
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,746

    Re: (VB6) Turn multiline text into String constant

    Code:
    'FROM CODE:
    Dim a
    Dim b
    Dim c
    a = "2234333"
    'afasfa
    b = "aafasdfa" & Chr(34) '"
    b = "aafasdfa" & Chr(34) '""
    
    'VBCODE:
      Dim cConstName3 As String
      cConstName3 = "Dim a" _
      & vbCrLf & "Dim b" _
      & vbCrLf & "Dim c" _
      & vbCrLf & "a = ""2234333""" _
      & vbCrLf & "'afasfa" _
      & vbCrLf & "b = ""aafasdfa"" & Chr(34) '""" _
      & vbCrLf & "b = ""aafasdfa"" & Chr(34) '"""""
    Code:
    Function StrToVbCode_(ByVal Str As String, VarName As String, Optional ByConst As Boolean) As String
    Dim CodeArr() As String, Arr() As String, UB As Long, I As Long
    Str = Replace(Str, Chr(34), Chr(34) & Chr(34))
    While Right(Str, 2) = vbCrLf
        Str = Left(Str, Len(Str) - 2)
    Wend
    
    
    Arr = Split(Str, vbCrLf)
    UB = UBound(Arr)
    ReDim CodeArr(UB)
    If ByConst Then
        CodeArr(0) = "Private Const " & VarName & " As String = "
    Else
        CodeArr(0) = "  Dim " & VarName & " As String" & vbCrLf & "  " & VarName & " = "
    End If
    CodeArr(0) = CodeArr(0) & Chr(34) & Arr(0) & Chr(34) & IIf(UB = 0, "", " _")
    
    For I = 1 To UB - 1
        CodeArr(I) = "  & vbcrlf & " & Chr(34) & Arr(I) & Chr(34) & " _"
    Next
    If UB > 0 Then
        If UB = 1 Then I = 1
        CodeArr(I) = "  & vbcrlf & " & Chr(34) & Arr(I) & Chr(34)
    End If
    StrToVbCode_ = Join(CodeArr, vbCrLf)
    End Function

  6. #6
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,746

    Re: (VB6) Turn multiline text into String constant

    I feel that this code is much neater. Do you have obsessive-compulsive disorder?

    Code:
    Private Const cConstName As String = "" _
      & "Dim a" & vbCrLf _
      & "Dim b" & vbCrLf _
      & "Dim c" & vbCrLf _
      & "a = ""2234333""" & vbCrLf _
      & "'afasfa" & vbCrLf _
      & "b = ""aafasdfa"" & Chr(34) '""" & vbCrLf _
      & "b = ""aafasdfa"" & Chr(34) '"""""
    
      Dim cConstName3 As String: cConstName3 = "" _
      & "Dim a" & vbCrLf _
      & "Dim b" & vbCrLf _
      & "Dim c" & vbCrLf _
      & "a = ""2234333""" & vbCrLf _
      & "'afasfa" & vbCrLf _
      & "b = ""aafasdfa"" & Chr(34) '""" & vbCrLf _
      & "b = ""aafasdfa"" & Chr(34) '"""""
    Code:
    Function StrToVbCode_(ByVal Str As String, VarName As String, Optional ByConst As Boolean) As String
        Dim CodeArr() As String, Arr() As String, UB As Long, I As Long
        Str = Replace(Str, Chr(34), Chr(34) & Chr(34))
        While Right(Str, 2) = vbCrLf
            Str = Left(Str, Len(Str) - 2)
        Wend
        Arr = Split(Str, vbCrLf)
        UB = UBound(Arr)
        ReDim CodeArr(UB + 1)
        If ByConst Then
            CodeArr(0) = "Private Const " & VarName & " As String = "
        Else
            CodeArr(0) = "  Dim " & VarName & " As String : " & VarName & " = "
        End If
        CodeArr(0) = CodeArr(0) & " """" _"
        
        For I = 0 To UB - 1
            CodeArr(I + 1) = "  & " & Chr(34) & Arr(I) & Chr(34) & " & vbCrLf _"
        Next
            CodeArr(UB + 1) = "  & " & Chr(34) & Arr(I) & Chr(34)
        StrToVbCode_ = Join(CodeArr, vbCrLf)
    End Function

  7. #7
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,746

    Re: (VB6) Turn multiline text into String constant

    This is the way with the least code
    【String line breaks "_" are spliced, and cannot exceed 25 lines, otherwise an error occurs, and the prompt "Too many line continuation flags"】

    Code:
    Function StrToVbCode_(ByVal Str As String, VarName As String, Optional ByConst As Boolean) As String
        Str = Replace(Str, Chr(34), Chr(34) & Chr(34))
        While Right(Str, 2) = vbCrLf
            Str = Left(Str, Len(Str) - 2)
        Wend
        StrToVbCode_ = IIf(ByConst, "Private Const " & VarName & " As String = ", "  Dim " & VarName & " As String : " & VarName & " = ") _
        & Chr(34) & Chr(34) & " _" & vbCrLf & "  & " & Chr(34) _
        & Replace(Str, vbCrLf, Chr(34) & " & vbCrLf _" & vbCrLf & "  & " & Chr(34)) _
        & Chr(34)
    End Function
    Last edited by xiaoyao; May 14th, 2021 at 05:06 AM.

  8. #8
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,746

    Re: (VB6) Turn multiline text into String constant

    Dim a
    Dim b
    Dim c
    a = "2234333"
    'afasfa
    b = "aafasdfa22"
    b = "aafasdfa" & Chr(34)

    to vbcode:
    Code:
     Dim Sarr(6) As String
    Sarr(0) = "Dim a"
    Sarr(1) = "Dim b"
    Sarr(2) = "Dim c"
    Sarr(3) = "a = ""2234333"""
    Sarr(4) = "'afasfa"
    Sarr(5) = "b = ""aafasdfa22"""
    Sarr(6) = "b = ""aafasdfa"" & Chr(34)"
    'Msgbox Join(Sarr,vbCrLf)
    
    Function StrToVbCodeArr(ByVal Str As String, VarName As String) As String
        'The code to convert a multi-line string into a VB6 array
        Dim Arr() As String, Ub As Long, I As Long, CodeArr() As String
        Str = Replace(Str, Chr(34), Chr(34) & Chr(34))
        Str = Replace(Str, Chr(9), Chr(34) & " & chr(9) & " & Chr(34))
        Arr = Split(Str, vbCrLf)
        Ub = UBound(Arr)
        ReDim CodeArr(-1 To Ub + 1)
        CodeArr(-1) = "Dim " & VarName & "(" & Ub & ") As String"
        For I = 0 To Ub
            CodeArr(I) = VarName & "(" & I & ") = " & Chr(34) & Arr(I) & Chr(34)
        Next
        CodeArr(Ub + 1) = "'Msgbox Join(" & VarName & ",vbCrLf)"
        StrToVbCodeArr = Join(CodeArr, vbCrLf)
    End Function

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,996

    Re: (VB6) Turn multiline text into String constant

    Quote Originally Posted by Eduardo- View Post
    Ah, OK, that's exactly what my script does not do.
    I've thought about adding that option...
    Now it has the option.

  10. #10
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,746

    Re: (VB6) Turn multiline text into String constant

    Quote Originally Posted by Eduardo- View Post
    Now it has the option.
    I Don't understand. Where did you update?

    The main problem is your code, I feel very strange, I can't understand it.

  11. #11
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,746

    Re: (VB6) Turn multiline text into String constant

    In the past I've used multi-line text concatenation, mostly for web post submissions. For example, data collection, crawlers automatically release information. You may need to post five to ten different addresses from login to operation. The data of each post is different. Key, value, equivalent to JSON data. a=1&b=k&c=33 Some data are fixed, and some are constantly changing. I couldn't think of a better way, so I went with an array of strings.
    dim postArr()
    redim postArr(1)
    postArr(0)="a=" & "1"
    postArr(1)="b=" & bvalue

  12. #12

    Thread Starter
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,996

    Re: (VB6) Turn multiline text into String constant

    Quote Originally Posted by xiaoyao View Post
    I Don't understand. Where did you update?
    In the project attached to the first post.

    Quote Originally Posted by xiaoyao View Post
    The main problem is your code, I feel very strange, I can't understand it.
    It is not commented.
    It is intended as a tool to be used if needed, but just that. It is in the Codebank because exes are not allowed.

    I usually comment the code the second time, when I try to figure what I've done the fist time and need to understand it.
    But the first time I'm just focused on making it to work. And when it is done, I usually don't feel like it to spend more time re-studying the code and making comments.

    The logic is this:
    Convert all non-printable characters to VB code (vbTab, vbCrLf, etc.)
    Split into lines that do not occupy more than 1024 characters each.
    Split into several constants because one can only have a limited number of line continuations.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width