VBA – Reading a file into an array and Appending to a file

Posted on Monday, July 11th, 2011 at 8:20 pm in

Both of these routines can be called from Outlook Macros/Scripts to process files. In my case I am using them to open iCalendar (ICS) files and place the contents into a string array. The calling method then processes the information then flags the file as ‘PROCESSED’ by the second routine.

Public Sub icsFileToArray(ByVal FileName As String, ByRef TheArray As Variant)
'
Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")
Dim oFSTR As Scripting.TextStream
Dim lCtr As Long

Set oFSTR = oFSO.OpenTextFile(FileName, 1, 0) ' for reading

Do While Not oFSTR.AtEndOfStream
    ReDim Preserve TheArray(lCtr) As String
    TheArray(lCtr) = oFSTR.ReadLine
    lCtr = lCtr + 1
    DoEvents
Loop
oFSTR.Close
ErrorHandler:
Set oFSTR = Nothing
End Sub


Public Sub icsWriteToFile(ByVal FileName As String)
'
Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")
Dim oFSTR As Scripting.TextStream
'Dim ret As Long
Dim lCtr As Long

Set oFSTR = oFSO.OpenTextFile(FileName, 8, 0) ' for appending
oFSTR.Write "PROCESSED"
oFSTR.Close
ErrorHandler:
Set oFSTR = Nothing
End Sub
You might also be interested in

Top