Solved

Extraxting parts of text with VB

Posted on 1998-12-29
4
163 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Err eek forgot

dim str as a string ;-)

sorry
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

743 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

10 Experts available now in Live!

Get 1:1 Help Now