+ Reply to Thread
Results 1 to 8 of 8

Added text to first line of text file using notepad

  1. #1
    Forum Contributor
    Join Date
    03-29-2012
    Location
    Ottawa, Ontario
    MS-Off Ver
    Excel 2013
    Posts
    104

    Added text to first line of text file using notepad

    I found the following VBA code below which append text to an existing text file using notepad at the end of the file.

    Please Login or Register  to view this content.
    I have been trying to add text to a specific line but I have not been successful. The solution that I want would be to open an existing text file using notepad, add a blank line at the top of the text file. So original line #1 would move to line #2 and line #1 would be blank. See examples below. Thank you in advance for your time.


    Original file:

    Line 1 | This is line #1
    Line 2 | This is line #2


    New file after macro:

    Line 1 |
    Line 2 | This is line #1
    Line 3 | This is line #2

  2. #2
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,887

    Re: Added text to first line of text file using notepad

    I have been trying to add text to a specific line but I have not been successful.
    That's because FreeFile() method can only add new line to end of text file.

    If you want to add to start of text. You'll first need to read entire content of Text file. Add to the string your value. Then write back to text file.

    Ex:
    Please Login or Register  to view this content.
    Edit: If you want to insert text at specific position. You'll either need to use line by line read of text file, or use text manipulation to insert new string to desired position.
    ?Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something.?
    ― Robert A. Heinlein

  3. #3
    Valued Forum Contributor
    Join Date
    09-30-2018
    Location
    Vlaams Brabant Belgium
    MS-Off Ver
    365
    Posts
    456

    Re: Added text to first line of text file using notepad

    maybe this gets you going

    https://devblogs.microsoft.com/scrip...f-a-text-file/
    Please be as complete as possible in your asking so it may save use all the time to rework the solution because you didn't give all the requirements. If you have a layout in mind please work it out first so we can adapt our solution to it. Thanks.
    If you have been helped, maybe you could click the *

  4. #4
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,416

    Re: Added text to first line of text file using notepad

    Quote Originally Posted by CK76 View Post
    Please Login or Register  to view this content.
    I would like to suggest you use vbNewLine instead of vbCrLf when introducing a new line in text that will be written to a disk. On a PC, vbNewLine equates to vbCrLf whereas on a Mac, which does not use vbCrLf for its new lines as far as I know, it will automatically use whatever Macs use for new lines (Macs used to use vbCr before switching to a Linux underpinning... true Linux uses vbLf but I don't know if that was adopted by Macs or not). Anyway, the point is vbNewLine automatically equates to whatever new line the system running the code uses when writing to a disk.

  5. #5
    Forum Contributor
    Join Date
    03-29-2012
    Location
    Ottawa, Ontario
    MS-Off Ver
    Excel 2013
    Posts
    104

    Re: Added text to first line of text file using notepad

    Thank you all for your response. CK76 solution work on my test file which is just 2 lines long. Our longest file we need to work with is 2,800,000 lines long so I suspect it is a memory issue? I got the following VBA error message:

    Run-time error 62.
    Input past end file


    Any other code that would be friendly to very large text files or not have to store text file content to memory before writing new blank 1st line? Could use keyboard short-cut from vba that would be recognize by notepad like:
    1) CTRL+Home to get to top of file
    2) Press enter to have 1st blank line
    3) Control+S to save file and
    4) Alt+F4 to close file

    No the best solution but may get around the memory issue? Thank you!

  6. #6
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,887

    Re: Added text to first line of text file using notepad

    VBA is single threaded and pretty much has to read entire text file content. Your issue is that, it may have gone beyond the limit.
    Though I don't have anything beyond 800k lines to test on at the moment.

    Alternatives methods...
    1. Split text file into smaller chunks.
    2. Use dedicated large text file editor (such as... CSVed, NotePad++)

    Other alternatives.
    1. Have relational DB (such as MS SQL) consume text file. Making sure to add Index column (start @ 1). Then create temp table via VBA or other means, give it 0 for index column value. Union records and order by index column.
    Then write back to text file from SQL.

    2. Use Python to read and write to text file. using Pandas dataframe to manipulate ordering etc.

  7. #7
    Forum Contributor
    Join Date
    03-29-2012
    Location
    Ottawa, Ontario
    MS-Off Ver
    Excel 2013
    Posts
    104

    Re: Added text to first line of text file using notepad

    Thank you CK76 for all the other possible solution. I was able to make it work with send key but again not on my big file. I'll mark as solved as I have a few solutions I can look at and it may just be my file a just too big. Thanks again!

  8. #8
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,416

    Re: Added text to first line of text file using notepad

    See if this code works for your large file. First though, create a new text file which contains a single new line (press the Enter Key once, then save) and give it the name NewLine.txt. Once you have done that, here is the code (make sure you use the correct directory path for each of the three specified files... their names should tell you what they are)...
    Please Login or Register  to view this content.
    NOTE: I changed the last line of code above from what I posted originally so that it will work with file names containing spaces should your file names include them.
    Last edited by Rick Rothstein; 11-09-2020 at 05:35 PM.

+ 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. [SOLVED] Opening a text file (like actually seeing the notepad)
    By mintymike in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-15-2016, 10:25 AM
  2. [SOLVED] Loading the value of text file (notepad) to a textbox
    By nathan0519 in forum Excel General
    Replies: 4
    Last Post: 07-17-2014, 10:48 PM
  3. Trying to copy columns to a notepad text file
    By Gamerxx13 in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 04-18-2013, 05:19 PM
  4. How to save the opened word document as a Notepad/text file?
    By Rahul888 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-10-2012, 05:38 PM
  5. Replies: 1
    Last Post: 08-30-2009, 11:26 AM
  6. Reading a text file line by line-How do I open the text file with VBA
    By stressman in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-16-2005, 12:29 AM
  7. Print text file with notepad
    By side_ in forum Excel General
    Replies: 0
    Last Post: 02-04-2005, 02:01 PM

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