473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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
2 1906
willakawill
1,646 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
Killer42
8,435 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

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

Similar topics

0
by: Psybar Phreak | last post by:
hi all i have an array of Process objects, each elements has an id (process.id), and arrival time (process.at), processing time (process.pt) and a boolean indicating whether the process has...
2
by: Daniel | last post by:
I use an Access database to basically take data exports, import them, manipulate the data, and then turn them into exportable reports. I do this using numerous macros, and queries to get the data...
12
by: Raymond Lewallen | last post by:
How to wait for a process to stop completion is my goal. Obviously, the looping while waiting for the HasExited property is not a solution.. but thats the best I can come up off the top of my...
7
by: MgGuigg | last post by:
Hello all, This is my first time posting a question to this forum, so here is hoping I am following protocol. I am scraping the rust off my old Basic programming skills, and have just recently...
27
by: one man army | last post by:
Hi All- I am new to PHP. I found FAQTS and the php manual. I am trying this sequence, but getting 'no zip string found:'... PHP Version 4.4.0 $doc = new DomDocument; $res =...
5
by: =?Utf-8?B?Z215ZXJz?= | last post by:
Hello, I am attempting to start a cmd.exe process and pass several .vbs scripts (with additional parameters) and then read the output from the scripts and make "notes" in a DataTable (the...
6
by: nickvans | last post by:
Hi All. I have a form with several text boxes for a user to enter part numbers and quantities: partnum1, partnum2, partnum3 / qty1, qty2, qty3 etc. I have a loop set up in VBA which goes...
5
by: =?Utf-8?B?V2lsbGlhbSBGb3N0ZXI=?= | last post by:
Good evening all, I am trying to write a process that uses a while loop to cycle multiple files from an array throught the StreamReader Process. The whole thing works using: Dim...
27
HaLo2FrEeEk
by: HaLo2FrEeEk | last post by:
I've got a program that downloads a set of thumbnail images specified in a remote XML file. There can be up to 30 thumbnails downloaded. I'm using a foreach loop iterating through an XmlNodeList of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.