Supercharge your PowerPoint productivity with
Supercharge your PPT Productivity with PPTools - Click here to learn more.

Proud member of


Image Export converts PowerPoint slides to high-quality images.

PPT2HTML exports HTML even from PowerPoint 2010 and 2013, gives you full control of PowerPoint HTML output, helps meet Section 508 accessibility requirements

Merge Excel data into PowerPoint presentations to create certificates, awards presentations, personalized presentations and more

Resize your presentations quickly and without distortion

Language Selector switches the text in your presentation from one language to another

FixLinks prevents broken links when you distribute PowerPoint presentations

Shape Styles brings styles to PowerPoint. Apply complex formatting with a single click.

Read a text file into an array

Sometimes it's handy to read a text file into an array, for example to process a list of phrases or quotations.

Here's a routine that will do just that and a test routine to demonstrate how it works and how to use it.

Sub TextFileToArray(ByRef aText As Variant, ByVal sTextFile As String)
' Reads a text file into an array, one line per array element

    Dim FileNum As Integer
    Dim Buffer As String
    ReDim aText(1 To 1)

    FileNum = FreeFile()
    Open sTextFile For Input As FreeFile
    While Not EOF(FileNum)
        Line Input #FileNum, Buffer
        ' Ignore blank lines
        If Trim(Buffer) <> "" Then
            'Call AddAPhrase(aText, Buffer)
            aText(UBound(aText)) = Buffer
            ReDim Preserve aText(1 To UBound(aText) + 1)
        End If
    Close #FileNum

    ' This leaves the array with one bogus empty record at end so
    ReDim Preserve aText(1 To UBound(aText) - 1) As String

End Sub

And you can use this routine to test the one above:

Sub TestThis()
    Dim sTextFile As String
    Dim aText() As String
    Dim x As Long

    sTextFile = "C:\TEMP\TEXT.TXT"

    Call TextFileToArray(aText, sTextFile)

    For x = LBound(aText) To UBound(aText)
        Debug.Print aText(x)

End Sub

See How do I use VBA code in PowerPoint? to learn how to use this example code.

Did this solve your problem? If so, please consider supporting the PPT FAQ with a small PayPal donation.
Page copy protected against web site content infringement by Copyscape Contents © 1995 - 2022 Stephen Rindsberg, Rindsberg Photography, Inc. and members of the MS PowerPoint MVP team. You may link to this page but any form of unauthorized reproduction of this page's contents is expressly forbidden.

Supercharge your PPT Productivity with PPTools

content authoring & site maintenance by
Friday, the automatic faq maker (logo)
Friday - The Automatic FAQ Maker

Read a text file into an array
Last update 14 July, 2016
Created: 14 July, 2016