Solved

Extraxting parts of text with VB

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…

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