467,881 Members | 1,202 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,881 developers. It's quick & easy.

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.
Nov 3 '06 #1
  • viewed: 1776
Share:
2 Replies
1GB
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 FileSystemObject
read the text one line at a time and use StrComp(Left(MyStr, 5), "sfpaa") to check for the strings
Nov 3 '06 #2
Expert 8TB
Hi, you might take a look at the FileSystemObject
read the text one line at a time and use StrComp(Left(MyStr, 5), "sfpaa") to check for the strings
Personally I would take what I consider the simpler route...
Expand|Select|Wrap|Line Numbers
  1. Public Sub Load_File()
  2.   Dim Label As String, Text As String
  3.   Dim Array1() As String, Array2() As String, Entries As Long
  4.   Open "TheFile.txt" For Input Access Read Shared As #1
  5.   Do Until EOF(1)
  6.     Line Input #1, Label
  7.     If Left$(Label, 3) = "sfp" Then
  8.       Line Input #1, Text
  9.       Entries = Entries + 1
  10.       ReDim Preserve Array1(1 To Entries)
  11.       ReDim Preserve Array2(1 To Entries)
  12.       Array1(Entries) = Label
  13.       Array2(Entries) = Text
  14.     End If
  15.   Loop
  16.   Close #1
  17. 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.
Nov 5 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Psybar Phreak | last post: by
12 posts views Thread by Raymond Lewallen | last post: by
27 posts views Thread by one man army | last post: by
5 posts views Thread by =?Utf-8?B?Z215ZXJz?= | last post: by
5 posts views Thread by =?Utf-8?B?V2lsbGlhbSBGb3N0ZXI=?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.