By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,646 Members | 2,098 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,646 IT Pros & Developers. It's quick & easy.

Find and Process Text in a Loop Query

P: 1
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
Share this Question
Share on Google+
2 Replies


100+
P: 1,646
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 5K+
P: 8,434
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.