+ Reply to Thread
Results 1 to 6 of 6

(Open txt For Input As #F) vs (Workbooks.Open)

  1. #1
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    (Open txt For Input As #F) vs (Workbooks.Open)

    I am new to importing text files using excel and the examples I have got on this site use

    "Open txt For Input As #F"

    where txt is the file path+filename


    In the past I have used something more like this to open files

    "Workbooks.Open FileName:=tempdirpath + fl_nm_to_open"


    What are the major advantages of one over the other?

    Why would you do one vs the other?

  2. #2
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: (Open txt For Input As #F) vs (Workbooks.Open)

    The first method is a background method and the second is a foreground method. The first is most always faster.

  3. #3
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: (Open txt For Input As #F) vs (Workbooks.Open)

    Plus Excel will recognise that the Import Wizard needs to be displayed so the file can be imported, while you have to manually read/parse the file contents to put them in a worksheet if the File Open method is used.

    After saying, the low level file functions do have a lot of advantages if you want to manipulate files without importing into Excel first. See this post for an example. It uses a list of TXT files in Column A and replacement text in Col B. It opens each file and replaces the 5th line with the contents of col B for each file. (Blatant bit of self advertising...? Nah, it's just something that came to mind to illustrate the low level file functions.)

  4. #4
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: (Open txt For Input As #F) vs (Workbooks.Open)

    thanks for the info........I had never used this "Open txt For Input As #F" before but someone showed it to me while trying to efficiently import a text file. Seems to work great but I was not sure of the differences between it and the one I normally used.

    One diff. I did notice as well is that with the workbooks.open ..............after you use it the file will appear opened but with the "Open txt For..." you open the file but the file will not show up as being open...........

    anyway, thanks...............

    oh, can you use the "Open txt For" method only on text files? Or does it apply to all file types?

  5. #5
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: (Open txt For Input As #F) vs (Workbooks.Open)

    It will open any file but the data will be very hard to use for any but text files.
    • Please remember to mark threads Solved with Thread Tools link at top of page.
    • Please use code tags when posting code: [code]Place your code here[/code]
    • Please read Forum Rules

  6. #6
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: (Open txt For Input As #F) vs (Workbooks.Open)

    I'd suggest you read the Documentation for the low level file functions carefully, and make sure you understand them and what exactly you are doing. Experiment on test files. Reason being a statement like
    Please Login or Register  to view this content.
    will immediately destroy the file by truncating it's contents. There's no warning and you don't even have to 'save' the file.

    After saying that, low level file functions can be used to manipulate both bog standard ASCII (text) files and binary files. They do not, however, load data into an application.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Corrupt file. Fail to open using the workbooks.open method
    By Mortphi in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-27-2013, 01:48 AM
  2. Replies: 14
    Last Post: 08-17-2012, 10:54 AM
  3. How to close open workbooks from an application get open filename call ?
    By leanne2011 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-10-2011, 10:41 PM
  4. Replies: 1
    Last Post: 01-02-2006, 11:30 PM
  5. Replies: 0
    Last Post: 12-30-2005, 04:35 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1