?
Solved

Extraxting parts of text with VB

Posted on 1998-12-29
4
Medium Priority
?
169 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
jbil earned 300 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month10 days, 19 hours left to enroll

770 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