Results 1 to 5 of 5
  1. #1
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    VBA Open / Read (Word XP)

    I want to assign the complete text of a text file to a variable. I know how to get an available file number and Open a file to write text. How would I go about getting all of the text in a TXT file and assigning it to strText, for example.

    Thanks, Richard Barrett

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

    Re: VBA Open / Read (Word XP)

    Read the file line by line, and concatenate the lines to the string variable.

    Alternatively, use Scripting.FileSystemObject to open a text file in a TextStream object and use its ReadAll method. See ReadAll Method for the syntax and an example.

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: VBA Open / Read (Word XP)

    If this operation will be conducted many times or your source file is very large, the FileSystemObject's TextStream.Read() method might be faster. This old article has the story: 4GuysFromRolla.com - Efficiently Reading Large Text Files. (The code samples there are VBScript, tested in an ASP page, but the method is the same.)

  4. #4
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Re: VBA Open / Read (Word XP)

    Thank you. I didn't recall that the FileSystemObject head read / readall methods. I seem to recall using the FSO some time ago (to move and copy files), and on some users' PC it wouldn't run. If Admin had turned of Windows Search feature for some users, those users didn't have access to FSO. Do you have any similar experience with this?

    Regards,
    Richard

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

    Re: VBA Open / Read (Word XP)

    As far as I know, Windows Search is not related to Scripting, but some system admins turn off scripting for security reasons. They might well turn off both together.

Posting Permissions

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