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

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
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,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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