Using FSO

daverunt

Well-known Member
Joined
Jul 9, 2009
Messages
1,946
Office Version
  1. 2013
Platform
  1. Windows
Hi,

I am using FSO in the following way to read the content of 2 files into 2 string variables.

Is this correct or can I create a FSO just once and read both files into separate variables in the same code block?

Also when writing data to a file is complete the file is normally closed. Can I read the file content to a variable without closing the file?
- Either using FSO or Print?

Code:
'Create FileSystemObject for reading Arr.txt file content to strArr
    Set fso = CreateObject("Scripting.FileSystemObject")
    oFile = CurrSampleFile
    Set f = fso.OpenTextFile(oFile, 1)
        strArrI = f.ReadAll
        f.Close
        Set ts = Nothing
             
'Create FileSystemObject for reading ArrT.txt file content to strArrT
    Set fso = CreateObject("Scripting.FileSystemObject")
    oFile = TempSampleFile
    Set f = fso.OpenTextFile(oFile, 1)
        strArrT = f.ReadAll
        f.Close
        Set ts = Nothing
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
You only need one fso variable there. You can simply remove the statements to close the file if you need it open for something else.
 
Upvote 0
Thanks very much Rory. I'll give it a go - tomorrow.
 
Upvote 0

Forum statistics

Threads
1,213,558
Messages
6,114,297
Members
448,564
Latest member
ED38

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top