Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    64k limit in string functions (Wd2000)

    Some of the string functions in Word2000 VBA seem still limited to less than 64 kByte string length.

    Selection.TypeText will cut off a string longer than 64kB:
    <pre>Selection.TypeText String(66000, "?")</pre>

    I also ran into problems trying to write a string to a file (opened in Binary mode) with Put if the string is longer than 64kB.

    Are limitations like these documented somewhere (Knowledge Base)? Are they still present in Word2002?

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>TIA, Klaus

  2. #2
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: 64k limit in string functions (Wd2000)

    In VBA, fixed-length strings are limited to about 64000 bytes. Not so for non-fixed length strings.

    The implication is that the String function is using a fixed-lengh string for intermediate or final output.

  3. #3
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: 64k limit in string functions (Wd2000)

    Hi Howard, thank you for your reply.

    In my example it is .TypeText, not String(), that only works with strings shorter than 64kByte.
    I don't think an implicit conversion from a non-fixed string to a fixed-length string is involved, since in my example, the output is a lot shorter than 64kB:
    <pre>Selection.TypeText String(65537, "a")</pre>

    outputs a single, lonely "a".

    It seems to be an undocumented limitation in .TypeText, probably to speed this function up (it is a lot faster than .InsertAfter).

    I still hope the above line outputs 65537 a's in Word2002; it would be one more reason to upgrade. Could someone with Word2002 please check that?

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>Thank you very much in advance, Klaus

  4. #4
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: 64k limit in string functions (Wd2000)

    The implication from the following is that the String fuction is working correctly, however, for various lengths, there is an error, implying that fixed-length strings might be used internally for Selection.TypeText.
    However, there is no problem if the Range object is used instead of the Selection object.

    Option Explicit

    Private Sub TestStringFunction()
    Dim strTemp As String
    On Error Resume Next
    ' Debug.Print String(100, "a")
    ' Selection.TypeText String(100, "a")
    ' Debug.Print String(65535, "a")
    ' Selection.TypeText String(65535, "a")
    ' strTemp = String(65535, "a")

    ' Debug.Print String(65536, "a")
    ' Debug.Print String(65537, "a")
    ' Debug.Print String(65538, "a")
    ' Debug.Print String(100000, "a")
    ' Selection.TypeText String(100000, "a")
    ' strTemp = String(100000, "a")
    Selection.Range.InsertAfter String(100000, "a")
    If Err.Number <> 0 Then
    Debug.Print Err.Number & vbCr & Err.Description
    Else
    Debug.Print Len(strTemp)
    End If
    End Sub

  5. #5
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: 64k limit in string functions (Wd2000)

    If it's a VBA issue, then 2002 will be the same because it uses VBA6, same as Word 2000.
    Charlotte

  6. #6
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: 64k limit in string functions (Wd2000)

    Nice research <img src=/S/clapping.gif border=0 alt=clapping width=19 height=23>; but I never got any errors.

    The problem definitely is with .TypeText;
    Selection.Range.InsertAfter String(100000, "a") works,
    Selection.InsertAfter String(100000, "a") works,
    Selection.TypeText String(100000, "a") doesn't.

    None of the three throws an error.

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>Klaus

  7. #7
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: 64k limit in string functions (Wd2000)

    Hi Charlotte,

    I fear so, too <img src=/S/sigh.gif border=0 alt=sigh width=15 height=15>

    My goal was to put very long strings (>1 MByte) into a Word document as quickly as possible.
    Selection.TypeText doesn't seem to be faster than Selection.InsertAfter if I have to split the strings up into 64kByte chunks first.

    The only other alternative I could think of was to write the string into a text file, and then append the text file. But -- as is to be expected -- this is slower still.

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>Klaus

  8. #8
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: 64k limit in string functions (Wd2000)

    I just *did* get an error message "4609: String too long" when trying to split up the string into chunks:
    <pre> While Len(myString) > 65500
    Selection.TypeText Left$(myString, 65500)
    myString = Mid$(myString, 65501)
    Wend
    Selection.TypeText myString</pre>

    With chunks of size 65000 bytes, it works fine, and with chunks of 66000 bytes, it doesn't work (but I also don't get an error message any more) <img src=/S/confused.gif border=0 alt=confused width=15 height=20>

    Seems definitely a bug in Word-VBA that the error 4609 doesn't appear for strings longer 64kByte, but only for strings a bit shorter than 64kByte.

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>Klaus

  9. #9
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: 64k limit in string functions (Wd2000)

    Klaus,

    Just out of curiosity, what is the practical application of inserting long strings like this into a Word document?

    Thanks,
    Gary

  10. #10
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: 64k limit in string functions (Wd2000)

    Hi Gary,

    I'm converting Word-Docs from/to tagged file formats.
    I'm reading XML-files or other tagged files (whole books of several MBytes) into a string variable and parse/edit it with VBA string functions (Split, Join, Replace...). This is a lot faster than opening the text file and using the Word Object Model to make the necessary changes.
    When I've gone down that road as far as possible, I write the string into a document, and do the rest of the conversion with the Object Model (mostly assigning paragraph and character styles to different ranges).
    It's a bit frustrating that parsing/editing the string is pretty fast, but it takes up to several minutes to get the string into a Word document. Since .TypeText is a lot faster than .InsertAfter for short strings, I had hoped to speed up the process with .TypeText, and it did seem to work ... until I noticed I had lost most of my text, that is <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>Klaus

  11. #11
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: 64k limit in string functions (Wd2000)

    Hi Klaus,

    Rather than .TypeText or .InsertAfter, could you use something like:

    NewDoc.Content = strToInsert

    Also, Selection/Range.InsertFile is very fast, but this would require you to save the string variable back to some kind of file first I guess...

    Gary

  12. #12
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: 64k limit in string functions (Wd2000)

    Thank you Gary, I'll try your second suggestion. I checked the first already, and it didn't seem to be (much) faster. For most files I work with (<3 MByte), the speed is acceptable already (<20 sec); formatting the text takes much longer anyhow.

    BTW, I'll report the failure of .TypeText for strings >64kB to Microsoft; seems to be a genuine bug that it messes up the string without giving an error message.

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>Klaus

  13. #13
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: 64k limit in string functions (Wd2000)

    I saw that too.
    Just use the Range object instead of the Selection object.

  14. #14
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: 64k limit in string functions (Wd2000)

    If you want speed, the Range object is at least umpteen times faster than the Selection object.

  15. #15
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: 64k limit in string functions (Wd2000)

    Not necessarily.
    Bugs do get fixed.

Page 1 of 2 12 LastLast

Posting Permissions

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