473,386 Members | 1,606 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.

Calculation based on specific word in a text file using Visual Basic

36
I am in the process of modifying some code that reads a .txt file after it has been created from .pdf (this works great) and find a specific line in the text file. Once I have found this line (it will be in every text file but on a different line) I then want to use the line directly below it to complete a calculation. I can also go about it like this: I can go to the end of the text file (no clue as to how to program this) and subtract 6 from the line count to do the calculation on the correct line. The line I am trying to get at is always the 6th line from the bottom, but varies from the top of the file.

Currently I have something like this:
If InStr(lineNo, "Credits Debits Difference") Then
lineNo = lineNo + 1
For Each i In Split(line)
If (IsNumeric(i)) Then
totalItems = totalItems + i
End If
Next
End If

Credit Debit Difference is the text I am looking for in the txt file. lineNo is just a variable I set up to advance and read each line in the text file.

Thank you for any suggestions.
Feb 3 '10 #1
13 3148
vb5prgrmr
305 Expert 100+
Expand|Select|Wrap|Line Numbers
  1. Dim FileContents As String, LineArray() As String
  2. Dim FNumb As Integer, FName As String
  3.  
  4. FName = "PathFileName"
  5. FNumb = FreeFile
  6.  
  7. Open FName For Input As #FNumb
  8. FileContents = Input(FileLen(FName, #FNumb)
  9. Close #FNumb
  10.  
  11. LineArray = Split(FileContents, vbNewLine)
  12.  
  13. 'line to work with = LineArray(UBound(LineArray) - 6)
  14.  


Good Luck
Feb 4 '10 #2
brat33
36
*****if replied twice - sorry!******
Makes perfect sense to me...BUT I am still having a hard time implementing it. This is what I have now:

dim lineArray() as string, lineContent as string
dim tempLine as string, fNumb as integer
dim itemsFile

itemsFile = txtFilePath
fNumb = FreeFile

'open same file in another instance for a second reading
open itemsFile for input as #fNumb
lineContent = input(FileLen(itemsFile, #fNumb)
close #fNumb

'read each line of file into new line in the array.
lineArray = Split(linecontent, vbNewLine)

'goto end of array and back up 6 lines to get to correct line with totals
tempLine = lineArray(UBound(lineArray) - 6)

'split the line totals into seperate values
For Each i In Split(tempLine)
if (IsNumeric(i)) then
'add values into total field
totalItems = totalItems + i
End if
Next

'close out 2nd occurance of file
itemsFile.close


This is all placed inside of a while loop that is reading the file for the initial time. My if statement above opens the same file into a completely different named object. I am assuming that the 2nd time the file is opened it can be read in it's entirety.
When I run this, nothing happens. I have put in place message boxes to see if it will even pop them up, and nothing happens. No error messages pop up either. Something is wrong and I just cannot get my mind around it yet. If I remove/comment out the above code, then the program will run and create the message boxes and even a blank e-mail that I asked it to do at the end. This is the one piece of code that is not functioning as I would like it to. This code also has all separate variables EXCEPT for the totalItems, which was declared at the top of program.
Thank you for any additional suggestions you may have! I know what it should do, I just can not get it written correctly. I am sure I will feel stupid when I am told "oh your missing a comma", but I am at my wits end with this one!
Feb 5 '10 #3
Guido Geurs
767 Expert 512MB
dear,
there is a ")" missing in:

'open same file in another instance for a second reading
open itemsFile for input as #fNumb
lineContent = input(FileLen(itemsFile, #fNumb)close #fNumb


must be:

lineContent = Input(FileLen(itemsFile), #fNumb)



br,
Feb 5 '10 #4
brat33
36
Thank you for that! unfortunatlely that did not resolve my issue. It still will not run.

Is it even possible to take 1 text file - read it into a location1 and perform a while loop based on each line in location1 and not being at end of file; at the same time, inside the loop reopen the same text file into location2 and perform some steps, then close out of file location2 while returning to location1. I know this sounds confusing, but I can not think of another way to perform these steps.
Feb 5 '10 #5
Guido Geurs
767 Expert 512MB
dear,

attached is a working example.

br,
Attached Files
File Type: zip data from line in txtfile.zip (2.0 KB, 134 views)
Feb 5 '10 #6
Guido Geurs
767 Expert 512MB
dear,

If you want the 6e last line in a txt file you must use the code=

Expand|Select|Wrap|Line Numbers
  1.   tempLine = lineArray(UBound(lineArray) - 5)
Ubound is an index and not the linenumber !
The indexes starts at 0 and ends at (linenumber -1)

A file with 100 lines gives:

line 93 => index 92
line 94 => index 93
line 95 => index 94 (-6)
line 96 => index 95 (-5)
line 97 => index 96 (-4)
line 98 => index 97 (-3)
line 99 => index 98 (-2)
line 100 => index 99 (-1)

so ubound= 99
the 6e last line (line 95) = index 94 = ubound-5 = 99-5 =94

see attachment

br,
Attached Files
File Type: zip data from line in txtfile_v2.zip (6.1 KB, 97 views)
Feb 5 '10 #7
brat33
36
I am starting to feel like I felt my first semester in College. Really I do have a degree in programming...it may not seem like it, but I do :)

Code - AGAIN:
Set totalsFile = fso.OpenTextFile(txtFilePath, 1)
While Not totalsFile.AtEndOfStream
line = totalsFile.Readline
If (usingSCO = True) Then
If (lineNo = 13) Then
If (InStr(line, " ") > 0) Then
totalAmount = totalAmount + Replace(Split (line) (0), "$", "")
Else
totalAmount = totalAmount + Replace(line, "$", "")
End If
End If
If (InStr(line, "Item Count (CR/DB)") > 0) Then
For Each i In Split(line)
If (IsNumeric(i)) Then
totalItems = totalItems + i
End If
Next
End If
Else
If (lineNo = 13) Then
Dim tempArray
tempArray = Split(line)
totalAmount = totalAmount + tempArray(UBound(tempArray) - 1)
End If

if (inStr(,line,"Credits Debits Difference") > 0 ) then
dim lineArray()
dim itemLine
dim fileBuff
Set fileBuff = fso.OpenTextFile(txtFilePath, 1)
lineArray = Split(fileBuff, vbCrLF)
itemLine = lineArray(uBound(lineArray))
For Each i In Split(itemLine)
if (IsNumeric(i)) Then
totalItems = totalItems + i
End If
Next
End If

End If
lineNo = lineNo + 1
Wend
totalsFile.Close

Code in bold is where I am having problems. Everything else works great! If I take out the bold code, and just set my totalItems to a random number it works great as well. ERROR is "Subscript Out Of Range".
I read a pdf file, convert it to a text file , and read that text file while performing some tasks. Once inside the while loop, I am trying to open the SAME text file into a different location to be read and get to the 6th line from the bottom of the file, add the two numbers on that line together and be done. This is the last step in the logic before the file gets closed and the totals are written to a generated e-mail. I have tried different open methods, different replace, instr, and if statements, all to no avail. I can not get it coded to work. I am using Microsoft Development Environment to code this. I will attach a copy of the text file as well this time.
Attached Files
File Type: txt BatchTotals_00002.txt (492 Bytes, 395 views)
Feb 8 '10 #8
Guido Geurs
767 Expert 512MB
dear,

there is a "," to much in the line:

Expand|Select|Wrap|Line Numbers
  1. if (inStr(,line,"Credits Debits Difference") > 0 ) then
must be:

Expand|Select|Wrap|Line Numbers
  1. if (inStr(line,"Credits Debits Difference") > 0 ) then
Are You using VB6 ?
Normally You can detect such errors in VB because they are RED (see Attachment GIF with a screencapture of VB editor).
The editor colors are set in Tools - Options (see attached GIF)

The brackets and ">O" is also not needed in the if..endif.
It can be writen as:

Expand|Select|Wrap|Line Numbers
  1. If InStr(Line, "Credits Debits Difference") Then
Please use the "Wrap CODE tags..." = > the # button in the menu of the BYTES replay editor.
How to: select ALL the lines with code and click on the # in the menu.

This is much easier to read for us and we don't have to unravel Your code again to see the structure.

br,
Attached Images
File Type: jpg Error 1.jpg (14.7 KB, 129 views)
File Type: jpg Settings_Errordetection.jpg (18.2 KB, 196 views)
Feb 9 '10 #9
Guido Geurs
767 Expert 512MB
dear,

In Your code is the line=

Expand|Select|Wrap|Line Numbers
  1.                itemLine = lineArray(UBound(lineArray))
But this is the last line of the file !!!.
must this not be the 6e last line with data ?? or=

Expand|Select|Wrap|Line Numbers
  1.                itemLine = lineArray(UBound(lineArray)-5)

br,
Feb 9 '10 #10
Guido Geurs
767 Expert 512MB
dear,

When I run Your program, Line 13 = line with NOTHING between :

Count 141 150 Diff .00 .00 190,677.70 190,677.70 .00

User Name


Your counter "lineNo" is at the END of the code so these are the steps:

You reed line 1 in the "line" and LineNo =0
You add 1 to lineNo => is now 1
You reed the second line but the counter is still at 1 !!!

You check for line 13 (LineNo=13) but the "Line" has the data of line 14 = BLANCO

The linecounter must be at the beginning of the code: direct before of after the reeding of the var "Line" like this:

Expand|Select|Wrap|Line Numbers
  1.    Set totalsFile = fso.OpenTextFile(txtFilePath, 1)
  2.       lineNo = 0
  3.       While Not totalsFile.AtEndOfStream
  4.          Line = totalsFile.Readline
  5.          lineNo = lineNo + 1
  6.          If usingSCO Then

br,
Feb 9 '10 #11
Guido Geurs
767 Expert 512MB
dear,

the line :

Expand|Select|Wrap|Line Numbers
  1.                lineArray = Split(fileBuff, vbCrLf)
gives the errror = Run-Time error '438' Object doesn't support this property or method !!

Because you can't split a link to a file (= fileBuff).
There is no data in it.

Why reading the file twice ???
I see now what you are trying to do with the TXT file.
I will see if I can write the code in a different way and using an array with the lines in it.

br,
Feb 9 '10 #12
Guido Geurs
767 Expert 512MB
dear,

I have rewritten the code in which I read the file once in an array.(see attachment)
Are the results what you want?

br,
Attached Files
File Type: zip data from line in txtfile_v4.zip (8.7 KB, 107 views)
Feb 9 '10 #13
brat33
36
Thank you very much for your assistance on this issue! It is now working like a champ! Took a few days off to work on other items, and came back to it, and what do ya know - works like I intended it to in the first place! Thank you again!
Feb 15 '10 #14

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

Similar topics

1
by: Gary | last post by:
Hi, there, 1. I would like to open a word document in a MDI environment. Every time, a new window outside my MDI poped out and show the word document. Is there a method I can use to specify the...
12
by: enak | last post by:
I have found some code that shows how to convert an html form to Word. The message said to simply put the following in the Page_load: Response.ContentType = "application/ms-word"...
4
by: DAMIANO | last post by:
come posso dare una stringa di testo da visual basic a una pagina word gią esistente? Io per aprire il file Word uso Set wb = CreateObject("Word.Basic") wb.ChDefaultDir "C:\", 0 wb.FileOpen...
3
by: tigrrgrr42 | last post by:
I am working(vb.net03and05) with word documents stored in a sql db and I am currently bringing them from a byte array into a temp file to pop into word and make word do its thing as a com object. ...
10
by: ljh | last post by:
Google ran dry on me while looking for .Net examples of how to compare 2 Word Docs. Has anyone here seen anything like that?
10
by: Petr Jakeš | last post by:
I have a standard 12-key mobile phone keypad connected to my Linux machine as a I2C peripheral. I would like to write a code which allows the text entry to the computer using this keypad (something...
7
by: vbnetdev | last post by:
My boss wants this done in a day. I would be happy with a week! Anyway, I have a dataset filled with data and need to populate an MS word chart with it when writing a report. Any tutorials or...
3
by: Dale Reed | last post by:
We're having a lot of problems getting visual studio to calculate even the most basic arithmetic correctly. For example: Dim MyValue AS Double = 2.155 * 100 This should give me 215.5 right?...
2
by: trezraven | last post by:
I am creating a template in Word 2007 using Visual Basic Editor. I am pulling information from an Access database. I know this is probably a stupid question, but for the life of me I can't figure out...
7
by: giladp1 | last post by:
I found Albert Kallal's great "Super easy Word Merge" code in his site at: http://www.members.shaw.ca/AlbertKallal/msaccess/msaccess.html Thanks Albert so much for sharing this. I am looking...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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.