PDA

View Full Version : How to process text files (not just the body of an Outlook message)



johngalt47
08-05-2014, 04:12 PM
I know how to program the basics of VBA but my problem is how to create an effective algorithm that works for various situations. For example, my current problem is how to process the text of emails of the following form:

1. The emails are inconsistent in the number of lines and the data.
2. The first word following the first line consisting of double dashes can be either DRAWING, DOCUMENT, ISO or TAG
3. I want to extract the rows of data like 'RV-99200" below.
4. What I have tried to do is something like

msglines = split(item.body,vbcrlf)
for each msgline in msglines
strings = split(msgline," ")
if strings(0) = "TAG" then
do one thing
else if strings(0) = "=" then
do something else

etc, etc

It just isn't working for me so I'm wondering if someone has a better strategy.

TIA

"To view this electronic transmittal and its electronic documents, click the link below.
--
--

-------------------------------------------------------------
EVERYTHING below is a text representation of the link supplied above.
-------------------------------------------------------------
XXX Job Number: E0886 Client Job Number: C71024

C71024 SHU PROJECT EXECUTE


INQUIRY REQ TO PROCUREMENT PIMS REF #: 619 was Issued on 07/17/14 From Job Number: E0886

REQUISITION NBR: I0009 REV:

GLOBAL TRANS. #: 619
--------------------------------------------------------------------------------------------------------------------------
Transmittal Description:
RELIEF VALVES - I0009
--------------------------------------------------------------------------------------------------------------------------
Transmittal Caption:
The listed Inquiry Requisition is hereby transmitted to Purchasing. Please issue
an Inquiry to the listed vendors.
--------------------------------------------------------------------------------------------------------------------------
Remarks:
=============================================================
TAG TAG TAG TAG TAG TAG
--------------------------------------------------------------------------------------------------------------------------
RV-99200
RV-99203
RV-99204
RV-99205
RV-99206
=============================================================
D I S T R I B U T I O N 4.3*

XXX XXXXX
-------------------------------------------------- --------------------------------------------------

westconn1
08-06-2014, 03:23 AM
so you just want the five lines starting RV?
do you need any other information?


msglines = split(item.body,vbcrlf)
for each msgline in msglines
if left(msgline, 3) = "RV-" then strresult = strresult & trim(msgline) & vbnewline
nextthe above should put all the RV lines into a string, which you can then do with as you wish

if your requirement is more complex than this simplistic approach, please give more detail

johngalt47
08-06-2014, 06:54 AM
In this instance, yes, that is what I want to extract.
What I'm really looking for though is a general solution. Most of the text will not begin with "RV-". I need a way to start extracting the lines of text as a result of some trigger and to stop as a result of a second trigger. For now, the trigger to start is the first double dashed line and the trigger to stop is the second double dashed line.

Thanks.

westconn1
08-06-2014, 02:45 PM
use a boolean variable to determine if you are passing trigger, in this case same trigger to start and stop, like

if left(msgline, 1) = "=" then mybool = not mybool
if mybool then strresult = strresult & trim(msgline) & vbnewline some adjustment maybe required to this code for correct result

johngalt47
08-06-2014, 06:11 PM
This is the beginning of something that works:

Sub Parse_text()


Dim x, y, z As Integer


Dim InSTring(500) As String


Dim start As Integer


Dim Dashed As Boolean


Open "c:\Project\message 3.txt" For Input As #1


x = 0
y = 0
z = 0


Dashed = False


While Not EOF(1)
x = x + 1
Line Input #1, InSTring(x)

If Left(InSTring(x), 1) = "=" And Dashed = False Then
y = x
Dashed = True
End If

If Left(InSTring(x), 1) = "=" And x > y Then
z = x
End If


Wend
'Debug.Print x


Close #1




For start = y + 3 To z - 1
Debug.Print InSTring(start)
Next


End Sub

westconn1
08-07-2014, 02:49 AM
Line Input #1, InSTring(x)easier to read whole file into array, then loop the array, as many times as required

myarr = split(input(lof(1), 1), vbnewline) ' where 1 is the open filenumber