Home All Groups Group Topic Archive Search About
Author
7 Mar 2009 6:37 PM
chint
I have a string of unspecific length, I want wrap it at every 30 character.

how to add vbcrlf to every 30 characters?

thanks,

Author
7 Mar 2009 7:04 PM
dpb
chint wrote:
> I have a string of unspecific length, I want wrap it at every 30 character.
>
> how to add vbcrlf to every 30 characters?

Since you're increasing the string length, you can't just insert; just
make a loop and copy 30-character substrings to a new string adding the
control code at each loop iteration.

You'll have to test for short string at end and decide on boundary
condition as whether it also needs crlf or not...

--
Author
7 Mar 2009 7:47 PM
chint
I am not increasing the string length, it fixed. but I cant predict its
length. the string is a long encrypted text.
Author
7 Mar 2009 8:41 PM
Larry Serflaten
"chint" <u***@server.com> wrote
> I am not increasing the string length, it fixed. but I cant predict its
> length. the string is a long encrypted text.

Adding CrLf requires more characters to be added, if you want the
result returned in a string.  They will not be the same length.

Do you want the result in a string, or just to print it somewhere?

To print, you can just loop through the string

For idx = 1 to Len(MyString) step 30
  Print Mid(MyString, idx, 30)
Next


LFS
Author
7 Mar 2009 7:19 PM
Richard Mueller [MVP]
"chint" <u***@server.com> wrote in message
news:eKFjNP1nJHA.500@TK2MSFTNGP06.phx.gbl...
>I have a string of unspecific length, I want wrap it at every 30 character.
>
> how to add vbcrlf to every 30 characters?
>

Wrapping the string is not difficult. For example:

strValue = "This is a long string for test purposes to see if we can get
word wrapping to work properly in this environment in answer to a newsgroup
question."

intLen = Len(strValue)
strOutput = Left(strValue, 30)
Do Until intLen < 31
  strValue = Mid(strValue, 31)
  strOutput = strOutput & vbCrLf & Left(strValue, 30)
  intLen = Len(strValue)
Loop

Debug.Print strOutput

However, this cuts words in the middle. If this is not acceptable, then the
code must parse for words, perhaps using the Split function, and figure out
how many "words" will fit on each line. Even this may not be perfect.

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--
Author
7 Mar 2009 7:44 PM
Richard Mueller [MVP]
Show quote Hide quote
"Richard Mueller [MVP]" <rlmueller-nospam@ameritech.nospam.net> wrote in
message news:ugZ3Nn1nJHA.5832@TK2MSFTNGP06.phx.gbl...
>
> "chint" <u***@server.com> wrote in message
> news:eKFjNP1nJHA.500@TK2MSFTNGP06.phx.gbl...
>>I have a string of unspecific length, I want wrap it at every 30
>>character.
>>
>> how to add vbcrlf to every 30 characters?
>>
>
> Wrapping the string is not difficult. For example:
>
> strValue = "This is a long string for test purposes to see if we can get
> word wrapping to work properly in this environment in answer to a
> newsgroup question."
>
> intLen = Len(strValue)
> strOutput = Left(strValue, 30)
> Do Until intLen < 31
>  strValue = Mid(strValue, 31)
>  strOutput = strOutput & vbCrLf & Left(strValue, 30)
>  intLen = Len(strValue)
> Loop
>
> Debug.Print strOutput
>
> However, this cuts words in the middle. If this is not acceptable, then
> the code must parse for words, perhaps using the Split function, and
> figure out how many "words" will fit on each line. Even this may not be
> perfect.
>
> --
> Richard Mueller
> MVP Directory Services
> Hilltop Lab - http://www.rlmueller.net
> --
>

A better solution that only wraps at spaces in the string. No line should be
more than 30 characters:

strValue = "This is a long string for test purposes to see if we can get
word wrapping to work properly in this environment in answer to a newsgroup
question."
' strValue = "This is a short string"

arrWords = Split(strValue, " ")

strOutput = ""
strLine = ""
For Each strWord In arrWords
    If (Len(strLine & strWord) > 30) Then
        If (strOutput = "") Then
            strOutput = strLine
        Else
            strOutput = strOutput & vbCrLf & strLine
        End If
        strLine = ""
    End If
    If (strLine = "") Then
        strLine = strWord
    Else
        strLine = strLine & " " & strWord
    End If
Next

If (strLine <> "") Then
    If (strOutput = "") Then
        strOutput = strLine
    Else
        strOutput = strOutput & vbCrLf & strLine
    End If
End If

Call MsgBox(strOutput)

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--
Author
7 Mar 2009 8:34 PM
Larry Serflaten
"chint" <u***@server.com> wrote
> I have a string of unspecific length, I want wrap it at every 30 character.
>
> how to add vbcrlf to every 30 characters?

Another interesting challenge.  Here is one way that avoids the creation
of temporary strings.  (useful for very, very long strings...)

Ex:    ChopAt 30, MyString

As noted elsewhere, if you want to chop the string at word boundries,
(or included CrLf's) more processing would be needed.

HTH
LFS


Sub ChopAt(ByVal Length, ByRef Source As String)
Dim pos As Long

  ' Validate input
  If (Len(Source) <= Length) _
  Or (Length < 1) _
  Then
    Exit Sub
  End If

  ' Find chop count
  pos = Len(Source) \ Length

  ' Add space (2 per vbCrLf)
  Source = Source & Space$(pos + pos)

  ' Start from end
  pos = pos * Length + 1

  ' Work toward beginning
  While pos > 1
    Mid(Source, pos + 2) = Mid(Source, pos)
    Mid(Source, pos) = vbCrLf
    pos = pos - Length
  Wend

  ' Trim tail (if desired)
  If Right$(Source, 2) = vbCrLf Then
    Source = Left$(Source, Len(Source) - 2)
  End If

End Sub
Author
7 Mar 2009 8:41 PM
Nobody
"chint" <u***@server.com> wrote in message
news:eKFjNP1nJHA.500@TK2MSFTNGP06.phx.gbl...
>I have a string of unspecific length, I want wrap it at every 30 character.
>
> how to add vbcrlf to every 30 characters?

Many use Mod for this, example "If i Mod 30 = 0 Then". However, it maybe
preferable to use DrawTextEx() which automatically prints text in a specific
rectangle and wrap as needed, so you can use variable width font. You can
add the flag DT_WORDBREAK if you want wordwrap.
Author
7 Mar 2009 10:40 PM
Vinchenzo vinç
"chint" <u***@server.com> escribió en el mensaje de noticias
news:eKFjNP1nJHA.500@TK2MSFTNGP06.phx.gbl...
>I have a string of unspecific length, I want wrap it at every 30 character.
>
> how to add vbcrlf to every 30 characters?


    Sorry, the question is not clear enough (to me).
    For long strings, the VBScript regular expressions engine is faster and
powerfull than the VBA Strings functions.

    If you want to *cut* a string at every X chars, you can use the pattern
«any character X times»: "(.{NumberOfChars})" and replace the ocurrences
with the first group and the specified delimiter.

    If you want a *wrap text* like solution wich do not cut a word, you can
use the pattern «any number of chars from 1 to the max length especified,
not followed by a non-space character»:
    "(.{1, NumberOfChars}(?!\S))\s?"

    Try both patterns to see if one of them fits your request:

'********************
Public Function RegExpSplit( _
                ByVal InputString As String, _
                ByVal DelimiterString As String, _
                Optional ByVal MaxWrapLength As Long = 1) As String

    If MaxWrapLength < 1 Then
        RegExpSplit = InputString '...actually you should raise an error
    Else
        With CreateObject("VBScript.RegExp")
            .Global = True: .MultiLine = True
            '.Pattern = "(.{" & MaxWrapLength & "})" '// Exactly n chars
            .Pattern = "(.{1," & MaxWrapLength & "}(?!\S))\s?"
            RegExpSplit = .Replace(InputString, "$1" & DelimiterString)
        End With
    End If
End Function
'********************

    MsgBox RegExpSplit(YourLongStringVar, vbCrLf, 30)



--
    Regards
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
( ! ) http://groups.google.com/group/microsoft.public.vb.general.discussion
( i ) http://www.microsoft.com/communities/conduct/default.mspx
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Author
7 Mar 2009 11:58 PM
Mike Williams
"Vinchenzo vinç" <Vinç@newsgroup.nospam> wrote in message
news:%23GnFOX3nJHA.4540@TK2MSFTNGP04.phx.gbl...

> For long strings, the VBScript regular expressions engine
> is faster and powerfull than the VBA Strings functions.

Will that work on machines where scripting has been disabled and where
scrrun.dll is not available and is removed by the system whenever it sees
something attempting to install it?

Mike
Author
8 Mar 2009 5:13 AM
Vinchenzo vinç
"Mike Williams" <M***@WhiskyAndCoke.com> escribió en el mensaje de noticias
news:%23XQDAC4nJHA.1172@TK2MSFTNGP04.phx.gbl...
> "Vinchenzo vinç" <Vinç@newsgroup.nospam> wrote in message
> news:%23GnFOX3nJHA.4540@TK2MSFTNGP04.phx.gbl...
>
>> For long strings, the VBScript regular expressions engine
>> is faster and powerfull than the VBA Strings functions.
>
> Will that work on machines where scripting has been disabled and where
> scrrun.dll is not available and is removed by the system whenever it sees
> something attempting to install it?

    Yes.



--
    Regards
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
( ! ) http://groups.google.com/group/microsoft.public.vb.general.discussion
( i ) http://www.microsoft.com/communities/conduct/default.mspx
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Author
8 Mar 2009 6:05 AM
mayayana
> >> For long strings, the VBScript regular expressions engine
> >> is faster and powerfull than the VBA Strings functions.
> >
> > Will that work on machines where scripting has been disabled and where
> > scrrun.dll is not available and is removed by the system whenever it
sees
> > something attempting to install it?
>
>     Yes.
>

   Probably not. RegExp is designed for scripting. It's
in vbscript.dll, which is part of the Windows Script
Host. If scripting is disabled it's likely vbscript.dll has
been removed, and/or unregistered, since it's not a
critical file.

   If it were me I'd think it was rather
unprofessional that a compiled, installed, EXE wanted
to use a scripting library ...especially just to parse a
string.

   And while a lot of network admins use the WSH,
many are also vehemently opposed to it. I think that issue
dates back to the browser wars and Active Desktop: When
Microsoft married IE to the operating system and added
the ability for script to create COM objects, they ruined
IE security forever, creating a whole new category of
security issues by having "active content", where before
there had only been DOM scripting. Many people were very
unhappy about that and rejected the idea of allowing the
same script used in webpages to have powers on the system.
Partly they rejected for security reasons and partly out of
protest against Microsoft's strategy.
   Which basically left 2 options: Disable script on Windows
wherever possible, or treat IE as a Desktop automation
tool that has no place online. (Personally I think the latter
approach makes more sense, but it's not hard to find admins
who choose the former method.)


   The code here might be of interest, if the OP
really has a gigantic string to deal with. (I haven't
looked at it closely, but VBSpeed does have some
interesting stuff.) -
http://www.xbeat.net/vbspeed/c_WordWrap.htm
Author
8 Mar 2009 4:46 AM
Rick Rothstein
If you are looking to break your line at 30 characters length per line, but
you only want breaks to occur at spaces and not in the middle of words, you
might find this old posting of mine to be of some interest. Look at my first
response in this thread...

http://groups.google.com/group/microsoft.public.vb.general.discussion/browse_thread/thread/bc0cfcf9f7cd0f15?hl=en&q=vbcr+vblf+group:*vb*+author:rothstein

--
Rick (MVP - Excel)


Show quoteHide quote
"chint" <u***@server.com> wrote in message
news:eKFjNP1nJHA.500@TK2MSFTNGP06.phx.gbl...
>I have a string of unspecific length, I want wrap it at every 30 character.
>
> how to add vbcrlf to every 30 characters?
>
> thanks,
>
>