Consulting

Results 1 to 4 of 4

Thread: Reading text files without .txt extensions

  1. #1
    VBAX Regular
    Joined
    Jun 2013
    Posts
    16
    Location

    Reading text files without .txt extensions

    I'm working with output files from an in-house finite element program and the solution files are just text files with custom extensions (.bms, .loadcase, .spc, etc.). I'm trying to read these into dictionaries, but I'm having issues getting into the files.

    If I navigate to the files outside of VBA, i get the "program not found, please select a program to open with" or whatever it says. If I do this once with NotePad, it's in the system memory and I can then access the one file through VBA, but this would be inefficient to do with almost hundreds of files.

    I'd like this to run without opening NotePad, like running an FSO stream or Open...For method.

    Currently I'm looping through an output folder containing all the files until I find a specific extension, then attempting to read like this


    file = dir(dataform.file1) 'user selects a file in the root folder with getopenfilename
    
    do while file <> ""
      if instr(file, ".bms") > 0 then
         open file for Random as #1   ''''''''Kicks me out here''''''''''
         'do things with awesome code
      end if 
      file = dir
    loop
    Any input is appreciated, thanks!

    khu

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,638
    open "G:\OF\Filename.bms" for input as #1
           c00=Input(LOF(1),1)
    close

  3. #3
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,872
    If there's more than one .bms file, I don't think it'll like using #1 again unless #1 is closed first.
    Check out Freefile in excel vba help.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    If I do this once with NotePad, it's in the system memory
    I'm not sure why you need to open it with Notepad first since VBA doesn't really care


    Dim iFileNum as long
    
    file = dir(dataform.file1) 'user selects a file in the root folder with getopenfilename
     
    Do While[/COLOR] file <> "" 
         If instr(file, ".bms") > 0 Then
            iFileNum=FreeFile
             Open file ForRandom As #iFileNum 
              'do things with awesome code
            Close #Ifilename
    
    
        Else instr(file, ".load") > 0 Then
            iFileNum=FreeFile
             Open file For Random As #iFileNum 
              'do things with awesome code
            Close #Ifilename
    
    
             etc.
    
    
         End If
        file = dir 
    Loop
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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