Find and Process Text in a Loop Query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Glamdring
    New Member
    • Nov 2006
    • 1

    Find and Process Text in a Loop Query

    I have a text file that has the following format.

    blah
    blah
    blah
    sfpaaa
    "Control Unit"
    blah
    blah
    blah
    sfpaax
    "Heater Control"
    blah
    blah
    sfpaas
    "Gas Control"

    I would like to write a scipt in VB or VBA for Word that sorts through the blah in the text file and picks out anything that begins with sfp and also picks out the descriptive text beneath.

    Ultimately displaying the text in the format below:

    sfpaaa "Control Unit"

    sfpaax "Heater Control"

    sfpaas "Gas Control"

    I realise a loop is involved, I thought of storing the text in 2 arrays. One for the spaxxx text and another for the description beneath it then just print the arrays.

    In VBA Word I can write a scipt that searches for anything beginning with spa but do not know how I can assign my found text to an array.

    Can anyone help?

    Many thanks.

    Glamdring.
  • willakawill
    Top Contributor
    • Oct 2006
    • 1646

    #2
    Originally posted by Glamdring
    I have a text file that has the following format.

    blah
    blah
    blah
    sfpaaa
    "Control Unit"
    blah
    blah
    blah
    sfpaax
    "Heater Control"
    blah
    blah
    sfpaas
    "Gas Control"

    I would like to write a scipt in VB or VBA for Word that sorts through the blah in the text file and picks out anything that begins with sfp and also picks out the descriptive text beneath.

    Ultimately displaying the text in the format below:

    sfpaaa "Control Unit"

    sfpaax "Heater Control"

    sfpaas "Gas Control"

    I realise a loop is involved, I thought of storing the text in 2 arrays. One for the spaxxx text and another for the description beneath it then just print the arrays.

    In VBA Word I can write a scipt that searches for anything beginning with spa but do not know how I can assign my found text to an array.

    Can anyone help?

    Many thanks.

    Glamdring.
    Hi, you might take a look at the FileSystemObjec t
    read the text one line at a time and use StrComp(Left(My Str, 5), "sfpaa") to check for the strings

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Originally posted by willakawill
      Hi, you might take a look at the FileSystemObjec t
      read the text one line at a time and use StrComp(Left(My Str, 5), "sfpaa") to check for the strings
      Personally I would take what I consider the simpler route...
      Code:
      Public Sub Load_File()
        Dim Label As String, Text As String
        Dim Array1() As String, Array2() As String, Entries As Long
        Open "TheFile.txt" For Input Access Read Shared As #1
        Do Until EOF(1)
          Line Input #1, Label
          If Left$(Label, 3) = "sfp" Then
            Line Input #1, Text
            Entries = Entries + 1
            ReDim Preserve Array1(1 To Entries)
            ReDim Preserve Array2(1 To Entries)
            Array1(Entries) = Label
            Array2(Entries) = Text
          End If
        Loop
        Close #1
      End Sub
      This routine (which is completely untested, so good luck) will finish up with all the sfp... lines in Array1 and their corresponding descriptions in Array2, and the count in Entries. If you want the info available elsewhere, you could pass the arrays as parameters, or define them at a higher level (module, or global). The idea is to define them without a specific size, so that they can be redimensioned dynamically using the ReDim statement. Preserve just means not to wipe out what's already in the array.

      This routine probably won't serve your purposes exactly as-is, but should be enough to get you started.

      Comment

      Working...