Solved

Extraxting parts of text with VB

Posted on 1998-12-29
4
166 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 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…

726 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