{"id":833,"date":"2011-07-11T20:20:30","date_gmt":"2011-07-12T01:20:30","guid":{"rendered":"http:\/\/anvil-of-time.com\/wordpress\/?p=833"},"modified":"2021-03-10T20:39:34","modified_gmt":"2021-03-11T01:39:34","slug":"vba-reading-a-file-into-an-array-and-appending-to-a-file","status":"publish","type":"post","link":"https:\/\/anvil-of-time.com\/programming\/vba-reading-a-file-into-an-array-and-appending-to-a-file\/","title":{"rendered":"VBA &#8211; Reading a file into an array and Appending to a file"},"content":{"rendered":"<p>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 &#8216;PROCESSED&#8217; by the second routine.<\/p>\n<pre name=\"code\" class=\"VB\">Public Sub icsFileToArray(ByVal FileName As String, ByRef TheArray As Variant)\n&#039;\nDim oFSO\nSet oFSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)\nDim oFSTR As Scripting.TextStream\nDim lCtr As Long\n\nSet oFSTR = oFSO.OpenTextFile(FileName, 1, 0) &#039; for reading\n\nDo While Not oFSTR.AtEndOfStream\n    ReDim Preserve TheArray(lCtr) As String\n    TheArray(lCtr) = oFSTR.ReadLine\n    lCtr = lCtr + 1\n    DoEvents\nLoop\noFSTR.Close\nErrorHandler:\nSet oFSTR = Nothing\nEnd Sub\n\n\nPublic Sub icsWriteToFile(ByVal FileName As String)\n&#039;\nDim oFSO\nSet oFSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)\nDim oFSTR As Scripting.TextStream\n&#039;Dim ret As Long\nDim lCtr As Long\n\nSet oFSTR = oFSO.OpenTextFile(FileName, 8, 0) &#039; for appending\noFSTR.Write &quot;PROCESSED&quot;\noFSTR.Close\nErrorHandler:\nSet oFSTR = Nothing\nEnd Sub<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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 &#8216;PROCESSED&#8217; by the second routine. Public Sub icsFileToArray(ByVal FileName As&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[54,10,85],"tags":[55,56,16],"_links":{"self":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts\/833"}],"collection":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/comments?post=833"}],"version-history":[{"count":4,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts\/833\/revisions"}],"predecessor-version":[{"id":1967,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts\/833\/revisions\/1967"}],"wp:attachment":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/media?parent=833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/categories?post=833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/tags?post=833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}