Solved

Extraxting parts of text with VB

Posted on 1998-12-29
4
164 Views
Last Modified: 2010-05-03
I am working on a text document in Word97.
I want to extract e.g. the sixth to the sixteenth letter/number on each line, and delete the rest of the text.
what I mean; if my line starts like this:
Annual988988010101INSTOCK, i want the remaining part to be
l988988010, similar for all lines of the document.
0
Comment
Question by:frodem
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
jbil earned 100 total points
ID: 1452898
Something like this may work............


Private Sub Text2_Click()

Dim num As String
Dim start As Integer
Dim mystring As String
Dim first

first = 7
    For start = 1 To Len(Text1.Text) 'or your doc
        num = Mid(Text1.Text, start, 1)
        If start >= first And start <= (first + 8) Then
         mystring = mystring & num
        End If
            If num = Chr(13) Then
             first = start + 8
             Text2.SelText = mystring & vbCrLf 'your new doc
             mystring = ""
            End If
    Next

End Sub
0
 
LVL 1

Expert Comment

by:prozak
ID: 1452899
Well if the document is a text document (not a Word97 .DOC file) here how you do it:

Public Function ProcessFile(strInput As String, strOutput As String) As Boolean
  Dim intInput As Integer
  Dim intOutput As Integer
  Dim strLine As String
  Dim strOutString As String
 
  On Error GoTo LocalErrorHandler
 
  'Open the files
  intInput = FreeFile()
  Open strInput For Input As #intInput
 
  intOutput = FreeFile()
  Open strOutput For Output As #intOutput
 
  'Loop while there are lines in the file
  Do While Not EOF(intInput)
 
    'Read a line from the input file
    Line Input #intInput, strLine

    'Get the characters 6-16
    strOutString = Mid(strLine, 6, 10)
 
    'Add extra validation code here, etc.

    'Write out the string to the output file
    Print #intOutput, strOutString
  Loop
 
  'Close the files
  Close #intInput
  Close #intOutput

  On Error GoTo 0
  ProcessFile = True
 
LocalErrorHandler:
  On Error Resume Next
  Close #intInput
  Close #intOutput
  On Error GoTo 0
  ProcessFile = True
End Function

Private Sub Command1_Click()
  If Not ProcessFile("c:\temp\test.txt", "c:\temp\dude.txt") Then
    MsgBox "An error occurred!"
  End If
End Sub

Hope this helps ...
0
 

Expert Comment

by:ggilbert122998
ID: 1452900
Based on your question I have made a couple assumptions:
1. Each line of text will have 16+characters
2. Once finished you only want the 10characters on each line and the document saved.

=============================================================

dim Myobject as object
dim iCount as integer

set myObject = createobject("word.application")
Myobject.documents.open("C:\my documents\myfile.txt")

'loop through each sentence in the document
for icount = 1 to activedocument.sentences.count
'check that the sentence has at least 16 characters per line
   If ActiveDocument.Sentences(iline).Characters.Count > 15 Then

       str = Mid(ActiveDocument.Sentences(iline), 6, 11) 'extract characters 6-16
       ActiveDocument.Sentences(iline) = str & vbCrLf 'replace the line with extracted chars
   End If
next icount
'save your work
activedocument.save
activedocument.close
'close the word application
Myobject.close

======================================================
I tested it on
12345678901234567
and ended up with
67890123456

hope this is what you want ;-)
0
 

Expert Comment

by:ggilbert122998
ID: 1452901
Err eek forgot

dim str as a string ;-)

sorry
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now