1 Reply Latest reply on Aug 8, 2013 7:11 PM by Artem Taturevych

    How to assign each line of the text file into array?

    Jinhong Chong

      Sub ReadAsciiFile()

       

       

          Dim sFileName As String

          Dim iFileNum As Integer

          Dim sBuf As String

       

          sFileName = "C:\Users\hong\Desktop\msg.txt"

       

       

          If Len(Dir$(sFileName)) = 0 Then

              Exit Sub

          End If

       

       

          iFileNum = FreeFile()

          Open sFileName For Input As iFileNum

       

       

          Do While Not EOF(iFileNum)

              Line Input #iFileNum, sBuf

              Debug.Print sBuf

          Loop

       

       

          Close iFileNum

       

          MsgBox (sBuf)

       

      End Sub

        • Re: How to assign each line of the text file into array?
          Artem Taturevych

          Check this:

          Sub ReadAsciiFile()

           

              Dim arr() As String

             

              Dim sFileName As String

              Dim iFileNum As Integer

              Dim sBuf As String

             

              sFileName = "C:\Users\hong\Desktop\msg.txt"

           

              If Len(Dir$(sFileName)) = 0 Then

                  Exit Sub

              End If

           

              iFileNum = FreeFile()

              Open sFileName For Input As iFileNum

           

              Dim i As Integer

              i = -1

              Do While Not EOF(iFileNum)

                  i = i + 1

                  ReDim Preserve arr(i)

                  Line Input #iFileNum, sBuf

                  arr(i) = sBuf

                  Debug.Print sBuf

              Loop

           

              Close iFileNum

           

              MsgBox sBuf

           

          End Sub

          ____________________________________________________

          Regards,

          Artem Taturevych, Application Engineer at Intercad (Australia)

           

          translationXpert – add-in to translate SolidWorks models

          myIntercad an integrated tool for SolidWorks Professionals

          LinkedIn SolidWorks API Education Group