Results 1 to 7 of 7
  1. #1
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Australia
    Posts
    1,294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    clean up strings (Word 2002 XP SR3)

    I'm working on code to clean up strings.

    I'm sending a string to the Word Print dialog to Page Range "Pages".

    In the Print dialog, the "Pages" string has a maxium length of 255 characters.

    However the total length of collected data in the string is unknown.

    Therefore the code's function is use and check the total string, split into smaller strings each with a max length of 255, and clean up the smaller strings.

    I've the following code to clean up the smaller strings, however I'm at a loss on how to code keep checking for max total and spliting the string into smaller strings.

    As the max String is 255, the code checks& counts increments in lots of 255 ie
    255
    510
    765
    1060...etc

    If 1 string with Total < 255 characters - then string is clean. no clean up required

    if string is >255 and <510, then
    1st string, stringA is truncated, and cleaned from last postion ie EndOfString (working backwards)
    all other strings eg stringB, string C etc... are cleaned up from the first postion ie StartofString (working forwards)

    once strings are cleaned up
    send each string to print

    There's probaly a cleaner and neater way to perform this task and I'm open to suggestions.

    sample code attached...

    diana
    Attached Files Attached Files

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: clean up strings (Word 2002 XP SR3)

    I assume that you build the long string in code too. You could perform the check for length 255 there, and print a batch of pages each time the string exceeds, say, 240 characters in length, then start with an empty string again.

  3. #3
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: clean up strings (Word 2002 XP SR3)

    Here's a bit of a different approach:
    o Carve up the long string str into chunks of size (close to) 255 - substr
    o Concatenate these with a pipe | separator to strRanges
    o Split strRanges into an array of strings strDialog(), each suitable for the Print Dialog
    <pre>Option Explicit

    Public Sub String255()
    Dim strRanges As String
    Dim str As String
    Dim substr As String
    Dim strDialog() As String
    Dim i As Long
    Dim RPos As Long

    'sample data string
    str = "p1s2-p16s2,p2s3,p4s3-p15s3,p17s3-p26s3,p32s3,p36s3,p46s3-p47s3,p49s3," _
    & "p53s3,p55s3,p58s3,p60s3-p63s3,p76s3,p82s3-p88s3,p90s3,p93s3-p96s3," _
    & "p101s3,p112s3,p114s3,p117s3-p119s3,p123s3-p126s3,p129s3-p135s3," _
    & "p147s3-p149s3,p155s3,p170s3-p171s3,p177s3-p178s3,p182s3,p185s3," _
    & "p187s3-p188s3,p190s3,p199s3-p200s3,p208s3-p209s3,p215s3-p217s3," _
    & "p219s3-p221s3"

    Do While Len(str) > 255
    substr = Left(str, 255)
    RPos = InStrRev(substr, ",")
    str = Right(str, Len(str) - RPos)
    substr = Left(substr, RPos - 1)
    strRanges = strRanges & "|" & substr
    Loop

    strRanges = strRanges & "|" & str
    strDialog = Split(strRanges, "|")

    For i = LBound(strDialog) + 1 To UBound(strDialog)
    'do printing stuff...
    'test output...
    MsgBox strDialog(i) & vbCrLf & "Length = " & Len(strDialog(i))
    Next

    End Sub
    </pre>

    Not tested beyond the sample data.

    Alan

  4. #4
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: clean up strings (Word 2002 XP SR3)

    (Thinks: Now how can I divert this thread to "Libraries" ....?)

    I use a library function (attached) called strSplitAt to achieve this. I produce a series of (in your case) self-defining comma-delimited strings - where the delimiter is defined as the leftmost character of the string.
    <pre>Function Splitter(str1 As String)
    Dim strResult As String
    While Len(str1) > 0
    Dim strNext As String
    strNext = UW.strSplitAt(str1, ",")
    If Len(strResult & strNext) + 1 > 255 Then
    Debug.Print strResult
    strResult = ""
    Else
    strResult = strResult & "," & strNext
    End If
    Wend
    Debug.Print strResult
    Sub TESTSplitter()
    Dim str1 As String
    'sample data string
    str1 = "p1s2-p16s2,p2s3,p4s3-p15s3,p17s3-p26s3,p32s3,p36s3,p46s3-p47s3,p49s3," _
    & "p53s3,p55s3,p58s3,p60s3-p63s3,p76s3,p82s3-p88s3,p90s3,p93s3-p96s3," _
    & "p101s3,p112s3,p114s3,p117s3-p119s3,p123s3-p126s3,p129s3-p135s3," _
    & "p147s3-p149s3,p155s3,p170s3-p171s3,p177s3-p178s3,p182s3,p185s3," _
    & "p187s3-p188s3,p190s3,p199s3-p200s3,p208s3-p209s3,p215s3-p217s3," _
    & "p219s3-p221s3"
    Call Splitter(str1)
    End Sub</pre>


    A library of string-handling functions is a must; you can d/l a copy of UW.dot from my web site for FREE.
    Attached Files Attached Files

  5. #5
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Australia
    Posts
    1,294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: clean up strings (Word 2002 XP SR3)

    The long strings built using code in a previous module.

    The thought never occured to me to split the long string as its being created.

    Good idea Hans, thanks again

    thanks Alan, neat code...

    i always learn things on this forum...thank you all again

    diana

  6. #6
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: clean up strings (Word 2002 XP SR3)

    A slight modification. As it stands, the print dialog strings will always be less than 255 long. If maximizing the length of such strings is a priority, then the replacement code below accommodates the case of preserving a print dialog string that just happens to be correct at exactly 255 characters long.
    <pre> Do While Len(str) > 255
    substr = Left(str, 255)
    If Mid(str, 256, 1) = "," Then
    str = Right(str, Len(str) - 256)
    Else
    RPos = InStrRev(substr, ",")
    str = Right(str, Len(str) - RPos)
    substr = Left(substr, RPos - 1)
    End If
    strRanges = strRanges & "|" & substr
    Loop
    </pre>

    Alan

  7. #7
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Australia
    Posts
    1,294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: clean up strings (Word 2002 XP SR3)

    thanks Alan

    maximising the lengh of the string isnt a priority...so the first sample code is fine <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

    diana

Posting Permissions

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