Results 1 to 16 of 16

Thread: Extract text file and import to excel in VB6

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    10

    Extract text file and import to excel in VB6

    Hi All,

    I have a text file with name, address, phone and some of unuseful line
    of text. I would like to extract only name, address, and phone and put them into 3 column in excel.

    My general approach is
    1-Open text file and read start a loop to read in one line at a time.
    [syntax??]
    2-Search for key words(Name, Address, Phone) the imply the desired
    string to extract [how do I "if line contains "Name" then...??
    Put them into a variable string or array [syntax??] Leading blank space
    may cause the error. [syntax?]
    3-When all desired data is collected, write them in an excel column.

    As you can tell I'm new to VB. Any and all help appreciated.

  2. #2

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    10

    Re: Extract text file and import to excel in VB6

    Here is a sample text file:

    Teststring
    Teststing dfdsfdsfdsafdsfdsfdsfsdfsdfI
    Teststimg
    Resrerere
    Erewrewrwerwer
    Rewrewrewr
    Rwerewr
    Fdsfsdf
    Ewrewrwer


    printer-friendly page


    Meetings


    rewrewrewrewrewr


    NameTonyLee
    Address12345maintstree
    Phone123456789


    NameTonyLee
    Address12345maintstree
    Phone123456789

  3. #3
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: Extract text file and import to excel in VB6


    can you upload a sample text file?
    CS

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    10

    Re: Extract text file and import to excel in VB6

    Pls see attached sample text file. Thk!
    Attached Files Attached Files

  5. #5

  6. #6

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    10

    Re: Extract text file and import to excel in VB6

    I'm able to search for Name. Pls point me to direction to grab string after Name which is the string that I'm intersted in import to Excel. Ex: NameTony Lee, capture Tony Lee and import it later into excel. Thk!

  7. #7
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Extract text file and import to excel in VB6

    when you find Name using InStr it will return a value of the position.

    Assuming you're reading line by line, you can then do something like this:
    VB Code:
    1. Line Input #1, strLine
    2. lngPos = InStr(strLine, "Name")
    3. ' If "Name" is found then lngPos will be > 0 hence
    4. If lngPos Then strName = Mid$(strLine, lngPos + 4)
    5. ' Then do what you want with strName

  8. #8

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    10

    Re: Extract text file and import to excel in VB6

    Hi,

    myFoundString = InStr(1, StringLine, SearchStringName, 1)
    If myFoundString Then strName = Mid$(StringLine, myFoundString + 6)
    TextBox2.Text = myFoundString

  9. #9
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Extract text file and import to excel in VB6

    is that question?

    you want TextBox2.Text = strName

  10. #10

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    10

    Re: Extract text file and import to excel in VB6

    Hi,

    I tried your code with different variable name, it returns 0 means nothing found. Wonder what I miss?

    Line Input #fileno, StringLine
    myFoundString = InStr(1, StringLine, SearchStringName, 1)
    If myFoundString Then strName = Mid$(StringLine, myFoundString + 6)
    TextBox2.Text = myFoundString

    On the other hand my approach slightly different encounter bug at SearchStringName +4. I'm stuck? Pls help!


    SearchStringName = "Name"
    fileno = FreeFile

    'open the file for reading
    Open myFileName For Input As #fileno
    While Not EOF(fileno)
    Line Input #fileno, StringLine
    If InStr(1, StringLine, SearchStringName, 1) Then
    myFoundString = Mid(StringLine, SearchStringName +4)
    TextBox2.Text = myFoundString
    End If
    Wend

  11. #11
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: Extract text file and import to excel in VB6

    do you have any question on post #8?

    myfoundstring will return integer value. are you trying to display it in textbox2??????
    CS

  12. #12

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    10

    Re: Extract text file and import to excel in VB6

    Pls ignore Post#8 just accidently hit enter. I meant to submit post#10

  13. #13
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: Extract text file and import to excel in VB6

    what type of error you are getting?
    CS

  14. #14
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Extract text file and import to excel in VB6

    well your trying to add numbers on to string values.

    you might be better off using Trim instead:
    VB Code:
    1. Open myFileName For Input As #fileno
    2.     Do Until EOF(fileno)
    3.         Line Input #fileno, StringLine
    4.         If InStr(1, StringLine, SearchStringName, vbTextCompare) Then
    5.             myFoundString = Mid$(Trim$(StringLine), Len(SearchStringName) + 1)
    6.         End If
    7.     Loop
    8. Close #fileno

  15. #15

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    10

    Re: Extract text file and import to excel in VB6

    Received " Run time error 13, Type Mismatch"

  16. #16

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    10

    Re: Extract text file and import to excel in VB6

    I understand the general ideas of your code and the error I got. Thanks for the input.

    Well, the text box displays the following "FoundString = Mid(StringLine, SearchStringName +4)". I quite not sure the use of Mid, Trim and Len all in one.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width