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 Guru
    Joined
    Apr 2012
    Posts
    4,498
    open "G:\OF\Filename.bms" for input as #1
           c00=Input(LOF(1),1)
    close

  3. #3
    Knowledge Base Approver VBAX Guru
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    4,542
    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 - - - - -This is my signature, it appears after all my posts. Below is not directed at anyone in particular - so don't take offence! - (You might guess why it's there though)
    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 Wizard
    Joined
    Apr 2007
    Posts
    6,601
    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

    ------------------------------------------------------------------------------------------------------------------------
    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)
    (multiple files can be selected while holding Ctrl key) / 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
  •