In the following code, if [yourfile].txt is 32767 bytes or less, all is as expected. But increase the length to 32768 and nothing gets passed to Word and no error is raised:
set objFso= CreateObject("Scripting.FileSystemObject")
set objWord= CreateObject("Word.Application")
objWord.Visible= true
folder = [some convenient location]
set objDoc = objWord.Documents.Open(folder & "\[blank doc1].doc")
set objSelection= objword.Selection
set objFile_R = objFso.OpenTextFile(folder & "\[your file].txt",1,true)
textstring=objFile_R.ReadAll
objFile_R.Close
objSelection.TypeText textstring
An easy way to construct a test file of 32768 bytes:
Open Notepad and type a single character
Then:
Ctrl+A
Ctrl+C
Ctrl+V
Ctrl+V
Do this 13 more times and you will have 32768 bytes.
Is this a documented issue?
Is there an elegant solution (other than passing substrings not exceeding 32767 bytes)?
Thanks
Piglette
Word TypeText Oddity
-
- Administrator
- Posts: 78241
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Word TypeText Oddity
Welcome to Eileen's Lounge!
This appears to be a bug in the TypeText method: the string length is taken modulo 32,768. If you increase the size of the text file to, say, 32,868 characters, only the first 100 (=32,868-32,768) will be passed into the Word document. As an alternative, you could use
objSelection.InsertAfter textstring
This does work correctly with strings of 32,768 characters or more.
This appears to be a bug in the TypeText method: the string length is taken modulo 32,768. If you increase the size of the text file to, say, 32,868 characters, only the first 100 (=32,868-32,768) will be passed into the Word document. As an alternative, you could use
objSelection.InsertAfter textstring
This does work correctly with strings of 32,768 characters or more.
Best wishes,
Hans
Hans
-
- Lounger
- Posts: 35
- Joined: 20 Feb 2010, 05:05
Re: Word TypeText Oddity
Hans:
Thanks very much. I will try the InsertAfter method next time I hit this issue (I already worked around it this time by feeding Word substrings of length 32767 bytes plus the remainder)
Do you have a preferred source of documentation regarding the Excel and Word object models?
Best wishes
Lukas
Thanks very much. I will try the InsertAfter method next time I hit this issue (I already worked around it this time by feeding Word substrings of length 32767 bytes plus the remainder)
Do you have a preferred source of documentation regarding the Excel and Word object models?
Best wishes
Lukas
-
- Administrator
- Posts: 78241
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Word TypeText Oddity
This particular feature (or bug) isn't documented in the Word VBA reference material, as far as I know.
I generally use the built-in Word and Excel VBA help, or look up the equivalent information on MSDN:
Microsoft Word 2003 Visual Basic Reference / Microsoft Excel 2003 Visual Basic Reference
Word 2007 Object Model Reference / Excel 2007 Object Model Reference
I generally use the built-in Word and Excel VBA help, or look up the equivalent information on MSDN:
Microsoft Word 2003 Visual Basic Reference / Microsoft Excel 2003 Visual Basic Reference
Word 2007 Object Model Reference / Excel 2007 Object Model Reference
Best wishes,
Hans
Hans