{"id":14000,"date":"2017-08-23T21:24:39","date_gmt":"2017-08-23T21:24:39","guid":{"rendered":"http:\/\/learnexcelmacro.com\/wp\/?p=14000"},"modified":"2022-08-17T19:18:48","modified_gmt":"2022-08-17T19:18:48","slug":"vba-to-open-and-write-text-files","status":"publish","type":"post","link":"https:\/\/vmlogger.com\/excel\/2017\/08\/vba-to-open-and-write-text-files\/","title":{"rendered":"VBA Guide to Interact with Text Files – Part – 1 of 2"},"content":{"rendered":"

[et_pb_section fb_built=”1″ _builder_version=”4.16″ da_disable_devices=”off|off|off” global_colors_info=”{}” da_is_popup=”off” da_exit_intent=”off” da_has_close=”on” da_alt_close=”off” da_dark_close=”off” da_not_modal=”on” da_is_singular=”off” da_with_loader=”off” da_has_shadow=”on”][et_pb_row _builder_version=”4.16″ background_size=”initial” background_position=”top_left” background_repeat=”repeat” global_colors_info=”{}”][et_pb_column type=”4_4″ _builder_version=”4.16″ custom_padding=”|||” global_colors_info=”{}” custom_padding__hover=”|||”][et_pb_text _builder_version=”4.17.6″ background_size=”initial” background_position=”top_left” background_repeat=”repeat” global_colors_info=”{}”]T<\/span>ext files are one of the simplest, lightest file types. This is the reason it is highly used in programming to store a lot of configuration-related data, logs and so many other necessary data which is used by programs more frequently.
\nIf you browse your Program files folder in windows almost every program will have some text files for storing some configurations or logs etc. stored in it.<\/p>\n

Did you Know?<\/h1>\n

XML Files are also text files. The only difference is that XML files are more structured than Text files. This is the reason XMLs are widely used for communication.
\nIn this article I am going to teach you in detail –\u00a0How to deal with Text Files in Excel VBA<\/strong><\/em>. As explained above, Text files are very useful while developing any tool or add-in in Excel where you want to store some configuration data – in order to run your program or add-in. Using Excel file to store such data will make the program very slow.<\/p>\n

I don’t need to explain much – about why a Text file would be a better option, rather you will find it by yourself at the end of this article.<\/p>\n

So let’s\u00a0start then in a logical order… Opening a file… reading a file… writing into a file.. etc.<\/p>\n

Topics covered in this Article<\/h1>\n

In this article, the following topics are covered. Click on the below links to directly jump to that section<\/p>\n

<\/i>  1. Excel VBA to Open a Text File<\/a><\/h2>\n

<\/i>  2. Excel VBA to create a NEW Text File<\/a><\/h2>\n

<\/i>  3. VBA to write a Text file using Write <\/em>Statement<\/a><\/h2>\n

<\/i>  4. VBA to write a Text file using Print <\/em>Statement<\/a><\/h2>\n

<\/i>  5. VBA to Append <\/em>to a Text File<\/a><\/h2>\n

<\/i>  5. What is FreeFile () function ?<\/a><\/h2>\n

<\/a><\/p>\n

How to read a file, will be covered in the next article.<\/p>\n

Excel VBA Code to Open a Text File<\/h1>\n

Following is the code Syntax to open a Text file:<\/p>\n

Open [file Path]<\/em> For [mode]<\/em> As [#FileNumber]<\/em><\/strong><\/p>\n

With the Above code Syntax, as you can see, there are 3 parameters. [file Path]<\/em> [mode]<\/em> and [#FileNumber]<\/em><\/p>\n

So, lets discuss about these three parameters – who are they and what role do they play in opening the text file:<\/p>\n

What is File Name<\/em><\/h2>\n

This is the path of the Text file which is going to be opened.<\/p>\n

What is Mode<\/em> in Open Text File VBA Code<\/h2>\n

As the name suggests, its the control which you want to specify before opening it. There are mainly 3 types of controls or modes possible – Input<\/em><\/strong>, Output<\/em><\/strong> and Append<\/em><\/strong>.
\nLets discuss one by one these 3 modes<\/p>\n

Input Mode<\/h2>\n

This mode is used for READ ONLY<\/strong><\/em> control. It means, if you open a Text file in Input mode, then you can not write anything in it. All you can do is – read all the content inside the text file. Therefore you can say.. this a read only mode.<\/p>\n

Output Mode<\/h2>\n

If your text file is open in this mode, then you can write content in it. But what is important here to note is : In this mode, your existing file gets overwritten. This means, if there are content already in there in the text file then it will be replaced by the new data which you are trying to save. Therefore, be careful while choosing the mode of the file while opening it.
\nNow you must be thinking – Then how to append content in an existing text file without overwriting it. Therefore the next mode – Append Mode.<\/p>\n

Append Mode<\/h2>\n

As the name suggests, this mode allow you to append the new content at the end of the text file. It does not overwrite the existing content in the text file.
\nSo now we have an idea about all these 3 modes. It will be more clear when we use them in the below examples.<\/p>\n

Now Let’s learn about our second parameter – #FileNumber<\/em><\/strong><\/p>\n

#FileNumber<\/h2>\n

When Text files are opened then windows recognize them by a unique integer value. Valid range of Integers for this parameter is between 1 to 511<\/strong><\/em>.
\nAs I mentioned above, it should be a unique<\/strong> integer, it is challenging for you to give a fixed number here in case you are dealing with multiple text files. To overcome this challenge you can use a function called FreeFile()<\/strong><\/em>
\n
<\/a><\/p>\n

What is FreeFile() Function?<\/h1>\n

FreeFile() function returns a unique integer value which represents the file number of the file you opened. This way always a unique (or a FREE File Number – which is not used already) file-Number is assigned automatically by this function.<\/p>\n

Now you know about \u00a0– How to open a Text file<\/strong><\/em>. Let’s see some examples and learn how to use it.<\/p>\n

Sample VBA Code to Open a Text File<\/h1>\n
\nSub OpenTextFile()\nDim sFilePath As String\nDim fileNumber As Integer\n' Full path of the textFile which you want\n' to open.\nsFilePath = "C:\\Users\\Vishwa\\Desktop\\LEM.txt"\n\n' Assign a unique file numner\nfileNumber = FreeFile\n' Below statement will open the\n' above text file in output mode\nOpen sFilePath For Output As #fileNumber\nEnd Sub\n<\/code><\/pre>\n

<\/a><\/p>\n

VBA code to create a new TextFile<\/i><\/h1>\n

If the file name provided in the File Path provided while writing Open<\/strong> statement, like above, does not exists, then Open statement, automatically creates a TextFile with the same name in the same directory.
\nThus, using the same Open<\/strong> statement, you can create a new TextFile as well. You don’t have to use a different statement to create a new file.<\/p>\n

Here is a sample code<\/h2>\n

Read the commented lines written inside the code. I have tried explaining them there as well.<\/p>\n

\nSub createANewtextFile()\nDim sFilePath As String\nDim fileNumber As Integer\n\n' In the above path C:\\Users\\Vishwa\\Desktop\\ - exists\n' But on the desktop - LEM.txt file does not exist\nsFilePath = "C:\\Users\\Vishwa\\Desktop\\LEM.txt"\n\n' Assign a unique file numner\nfileNumber = FreeFile\n\n' in this case, below statement, will\n' create a new file with name LEM.txt\n' as soon as below statement gets executed\n' And also, it is open to write something\nOpen sFilePath For Output As #fileNumber\n\nEnd Sub\n<\/code><\/pre>\n

Note:<\/strong> Only File Name should not exists in order to create a new file. Directories or folders specified must exists. If not, file Not Found Error will be thrown.<\/em>[\/fusion_text]
\n
<\/a><\/p>\n

VBA Code to Write content in to Text File<\/h1>\n

[fusion_text]Basically there are two statements using which you can write content in to a text file in excel VBA : Write\u00a0<\/em><\/strong>or\u00a0Print<\/em><\/strong>[\/fusion_text][title size=”1″ content_align=”left” style_type=”none” sep_color=”” margin_top=”” margin_bottom=”” class=”” id=””]VBA to write in to Text Files using Write<\/strong> <\/em>statement[\/title][fusion_text]As I mentioned above, one can write in to text files using two statements Write<\/strong> <\/em>or Print.\u00a0<\/em><\/strong>First lets have a look on – how to write content in to a text file using Write statement.[\/fusion_text]
\nWrite #<fileNumber>, InputData 1, InputData 2, ….<\/em>
\n[title size=”2″ content_align=”left” style_type=”none” sep_color=”” margin_top=”” margin_bottom=”” class=”” id=””]Where:[\/title][fusion_text]<\/p>\n

File Number :<\/strong> This is a nemeric value which represents the File. This must be a unique number assigned to each open text files. Text files are identified by these unique numbers assigned to them while opening them. refer the Open File statement to know more about FileNumber<\/p>\n

Input Data1, 2, 3, …:<\/strong>
\nThis is the data which you want to write in the Textfile. You can write many different kind of data in each line. Each of these data will be written in textfile in a single line separated by comma.
\nImportant to note:<\/strong>
\nBased on the type of the data which you put in InputData 1, 2 etc. Write statement does some common changes while putting them in to TextFile.<\/p>\n

If the data is Date Type:<\/strong>\u00a0 Then date value is closed within hash sign. Date – 2010-10-13 will be written in TextFile as #2010-10-13#<\/p>\n

String type data:<\/strong>\u00a0 They are stored in double quotes. For example : Input Data – Vishwamitra Mishra will be stored as “Vishwamitra Mishra”.<\/p>\n

Integer, Double, Long etc :<\/strong>\u00a0 They will be written as they are. There is not formatting done before they are written in text file.<\/p>\n

Example:<\/h1>\n

Let’s take an example. Export the following table in excel to a Text file using Write statement.<\/p>\n

\"Create<\/p>\n

From the above excel cells, I will start reading one by one every column values of each row and write them in Text File.<\/p>\n

VBA Code – To Write Text File – using Write Statement<\/h1>\n
\nSub WriteTextFileUsingWriteStatement()\n\nDim sFilePath As String\nDim iRow As Integer\n\nDim OrderDate As Date\nDim OrderPriority As String\nDim OrderQuantity As Integer\nDim Discount As Double\nDim ShipMode As String\nDim CustomerName As String\nDim ShipDate As Date\n\niRow = 2\nsFilePath = "C:\\Users\\Vishwa\\Desktop\\LEM.txt"\n\n' unique file number to access the file uniquely\nfileNumber = FreeFile\n\n' to check if file name LEM.txt exists\n' if not, end the program\nIf (VBA.Len(VBA.Dir(sFilePath))) = 0 Then MsgBox "File Does not exists": End\n\n' Open the TextFile in Output mode\n' in order to write in something\nOpen sFilePath For Output As #fileNumber\n\n' Loop to read one by one every\n' non empty row and write them\n' in the text file\nDo\nWith Sheets("Orders")\nOrderDate = .Cells(iRow, 1).Value\nOrderPriority = .Cells(iRow, 2).Value\nOrderQuantity = .Cells(iRow, 3).Value\nDiscount = .Cells(iRow, 4).Value\nShipMode = .Cells(iRow, 5).Value\nCustomerName = .Cells(iRow, 6).Value\nShipDate = .Cells(iRow, 7).Value\nEnd With\n' Now write these data in text file in next line\nWrite #fileNumber, OrderDate, OrderPriority, OrderQuantity, Discount, ShipMode, ShipDate\n\n' go to the next row in Excel sheet\niRow = iRow + 1\nLoop Until IsEmpty(Sheets("Orders").Cells(iRow, 1).Value)\n\n' Close the file once all data\n' is written in text file\nClose #fileNumber\nEnd Sub\n<\/code><\/pre>\n

Result : after Running the above code<\/h1>\n

After running the above code, Your text file will look something like this:
\n\"VBA
\nNow you can see, as explained above, dates are put under Hash marks (#) and string data is put under double quotes (” “).<\/p>\n

Important points to note in above code:<\/h2>\n

1. I have used Dir$ function<\/strong> to check if TextFile already exists. Why I am thinking it is important to mention is because, if you do not put this check and by mistake your file name is not correct, then Open File statement will create a new TextFile in the same location with your provided name and write the content in that new file.
\nNote: <\/strong>Open file statement will create a new file only if directory exists. If directory itself does not exists, then Open File statement will through an error – Path Not found.
\nIt will not create a directory in that case.<\/em><\/p>\n

2. Instead of using a fixed number<\/strong> like #1, #2 etc. for FileNumber, I have used the function FreeFile() which always finds an available fileNumber which can be assigned to a textFile. It is always a good practise to use this function rather using a hardcoded File Number. This becomes very important when your program start dealing with multiple text files.<\/p>\n

3. To read every line from Excel<\/strong> and write it in textFile, I have used Do..While loop with Until keyword for condition. You can read more about do.. while loop and Until keyword here.<\/a><\/p>\n

4. Last but not the least<\/strong> – do not forget the Close the file by using the same fileNumber by using the simple statement Close #FileNumber as you can see in the above code.
\n
<\/a><\/p>\n

How to write TextFile using Print Statement<\/h1>\n

Syntax remains exactly same as Write statement. Main difference between Write and Print statement is in the formatting of the Output in TextFile. You will see in detail. Let’s start with the Syntax:<\/p>\n

Syntax of Print statement:<\/h2>\n

Print #<FileNumber>, InputData1, InputData2, ….<\/p>\n

Where:<\/h2>\n

File Number :<\/strong> This is a numeric value which represents the File. It is exactly same as explained above in Write Statement.<\/p>\n

Input Data 1, 2, 3, …:<\/strong>
\nThis is the data which you want to write in the Textfile. You can write many different kind of data in each line. Each of these data will be written in textfile in a Proper formatting with proper spacing. TextFile which you get from Print statement is well formatted for printing purposes. That means spaces between columns are adjusted based on the values in those columns. You will see in the example below.<\/p>\n

Important to note:<\/h2>\n

Unlike Write statement, this does not change any of the formatting of the data for Date or String type. It just put them as they are.
\nValues of difference columns are not separated by Comma. Rather they are separated by space(s) depending on how many spaces required to make the textfile in a printable format.<\/p>\n

Example: To write Text File using PRINT Statement<\/h1>\n

Let’s take the same example as above. Now we will export the same table from excel to a Text file using Print statement.<\/p>\n

Here is the code<\/p>\n

\nSub WriteTextFileUsingPrintStatement()\n\nDim sFilePath As String\nDim iRow As Integer\n\nDim OrderDate As Date\nDim OrderPriority As String\nDim OrderQuantity As Integer\nDim Discount As Double\nDim ShipMode As String\nDim CustomerName As String\nDim ShipDate As Date\n\niRow = 2\nsFilePath = "C:\\Users\\Vishwa\\Desktop\\LEM.txt"\n\n' unique file number to access the file uniquely\nfileNumber = FreeFile\n\n' to check if file name LEM.txt exists\n' if not, end the program\nIf (VBA.Len(VBA.Dir(sFilePath))) = 0 Then MsgBox "File Does not exists": End\n\n' Open the TextFile in Output mode\n' in order to write in something\nOpen sFilePath For Output As #fileNumber\n\n' Loop to read one by one every\n' non empty row and write them\n' in the text file\nDo\nWith Sheets("Orders")\nOrderDate = .Cells(iRow, 1).Value\nOrderPriority = .Cells(iRow, 2).Value\nOrderQuantity = .Cells(iRow, 3).Value\nDiscount = .Cells(iRow, 4).Value\nShipMode = .Cells(iRow, 5).Value\nCustomerName = .Cells(iRow, 6).Value\nShipDate = .Cells(iRow, 7).Value\nEnd With\n' Now write these data in text file in next line\nPrint #fileNumber, OrderDate, OrderPriority, OrderQuantity, Discount, ShipMode, ShipDate\n\n' go to the next row in Excel sheet\niRow = iRow + 1\nLoop Until IsEmpty(Sheets("Orders").Cells(iRow, 1).Value)\n\n' Close the file once all data\n' is written in text file\nClose #fileNumber\nEnd Sub\n<\/code><\/pre>\n

Result : after Running the above code<\/h1>\n

After running the above code, Your text file will look something like this:
\n\"Text<\/p>\n

Now you can see, as explained above:<\/strong><\/p>\n

1. <\/strong>None of the data are formatted. They are, in fact, put as they are in Excel cells.
\n2. <\/strong>Spaces between columns are adjusted based on the data in each columns.<\/p>\n

\n

<\/i> Important to know…<\/strong><\/p>\n

There is another \u2013 in fact important \u2013 difference between Write and Print statement. That you will realize while reading above two TextFiles \u2013
\n1.<\/strong> Written using Write Statement
\n2.<\/strong> Written using Print Statement. This is a hint for now. It will be explained in detail in the next article, where we will be talking all about reading a TextFile.<\/p>\n<\/div>\n

[\/fusion_text][fusion_text]In all the above examples of writing to a text box, I have used the File Open mode as Output<\/strong>. This means, every time you run the code, all the content of the text file will be replaced with the new one. Therefore, let’s take an example, of how can we append to the existing content in a text file using write or print statement.
\n
<\/a><\/p>\n

VBA Code to Append to Text File<\/h1>\n

The whole trick lies in to the mode you open your text file during your VBA program. Why I say that, because you do not need to change anything else in the Write<\/em><\/strong> or Print<\/em><\/strong> statements in order to append and not to replace. Isn’t it simple? So the complete VBA code remains same as it is there for replacing the whole content – except changing the open mode – from Output <\/strong>to Append<\/strong>. What these modes are –<\/strong> They are explained in the beginning of the article.<\/em><\/p>\n

\nSub AppendToTextFile()\n\nDim sFilePath As String\nDim iRow As Integer\n\nDim OrderDate As Date\nDim OrderPriority As String\nDim OrderQuantity As Integer\nDim Discount As Double\nDim ShipMode As String\nDim CustomerName As String\nDim ShipDate As Date\n\n    iRow = 2\n    sFilePath = "C:\\Users\\Vishwa\\Desktop\\LEM.txt"\n    \n    ' unique file number to access the file uniquely\n    fileNumber = FreeFile\n    \n    ' to check if file name LEM.txt exists\n    ' if not, end the program\n    If (VBA.Len(VBA.Dir(sFilePath))) = 0 Then MsgBox "File Does not exists": End\n    \n    ' Open the TextFile in Append mode\n    ' in order to write in something\n    Open sFilePath For Append As #fileNumber\n    \n    ' Loop to read one by one every\n    ' non empty row and write them\n    ' in the text file\n    Do\n    With Sheets("Orders")\n        OrderDate = .Cells(iRow, 1).Value\n        OrderPriority = .Cells(iRow, 2).Value\n        OrderQuantity = .Cells(iRow, 3).Value\n        Discount = .Cells(iRow, 4).Value\n        ShipMode = .Cells(iRow, 5).Value\n        CustomerName = .Cells(iRow, 6).Value\n        ShipDate = .Cells(iRow, 7).Value\n    End With\n    ' Now write these data in text file in next line\n    Write #fileNumber, OrderDate, OrderPriority, OrderQuantity, Discount, ShipMode, ShipDate\n    \n    ' go to the next row in Excel sheet\n    iRow = iRow + 1\n    Loop Until IsEmpty(Sheets("Orders").Cells(iRow, 1).Value)\n    \n    ' Close the file once all data\n    ' is written in text file\n    Close #fileNumber\nEnd Sub\n<\/code><\/pre>\n

Note: <\/strong>All the new information gets appended at the end of the text file (from the first blank line)<\/em>
\nDid you like this article? Then share it with your friends\u2026 spread knowledge…”
\nLearn All about interacting with Text Files in Excel VBA like opening, creating, writing, reading etc. from Text Files using Excel VBA code”[\/et_pb_text][et_pb_blurb title=”You may like to read this too” use_icon=”on” font_icon=”||fa||900″ _builder_version=”4.17.6″ _module_preset=”0249c68e-4de8-4f44-84ff-a9b1850785b6″ hover_enabled=”0″ global_colors_info=”{}” sticky_enabled=”0″]<\/p>\n

    \n
  1. VBA Guide to Interact with Text Files \u2013 Part \u2013 2 of 2<\/a><\/li>\n
  2. Excel Macro : Excel VBA code to Print the Sheet<\/a><\/li>\n
  3. Excel VBA Basics : All about Debugging Excel VBA code<\/a><\/li>\n
  4. Download Free File Manager in Excel Macro<\/a><\/li>\n
  5. https:\/\/vmlogger.com\/excel\/2012\/08\/download-free-file-manager-new-version\/<\/a><\/li>\n<\/ol>\n

    [\/et_pb_blurb][\/et_pb_column][\/et_pb_row][\/et_pb_section]<\/p>\n<\/span>","protected":false},"excerpt":{"rendered":"

    T<\/span>ext files are one of the simplest, lightest file types. This is the reason it is highly used in programming to store a lot of configuration-related data, logs and so many other necessary data which is used by programs more frequently. If you browse your Program files folder in windows […]<\/p>\n","protected":false},"author":45,"featured_media":14095,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"on","_et_pb_old_content":"[dropcap color=\"\" boxed=\"no\" boxed_radius=\"8px\" class=\"\" id=\"\"]T[\/dropcap]ext files are one of the simplest, lightest file types. This is the reason it is highly used in programming to store a lot of configuration-related data, logs and so many other necessary data which is used by programs more frequently.\r\nIf you browse your Program files folder in windows almost every program will have some text files for storing some configurations or logs etc. stored in it.\r\n\r\n

    Did you Know?<\/h1>\r\nXML Files are also text files. The only difference is that XML files are more structured than Text files. This is the reason XMLs are widely used for communication.\r\nIn this article I am going to teach you in detail -\u00a0How to deal with Text Files in Excel VBA<\/strong><\/em>. As explained above, Text files are very useful while developing any tool or add-in in Excel where you want to store some configuration data - in order to run your program or add-in. Using Excel file to store such data will make the program very slow.\r\n\r\nI don't need to explain much - about why a Text file would be a better option, rather you will find it by yourself at the end of this article.\r\n\r\nSo let's\u00a0start then in a logical order... Opening a file... reading a file... writing into a file.. etc.\r\n\r\n

    Topics covered in this Article<\/h1>\r\nIn this article, the following topics are covered. Click on the below links to directly jump to that section\r\n\r\n\r\n

    <\/i>\u00a0 1. Excel VBA to Open a Text File<\/a><\/h2>\r\n

    <\/i>\u00a0 2. Excel VBA to create a NEW Text File<\/a><\/h2>\r\n

    <\/i>\u00a0 3. VBA to write a Text file using Write <\/em>Statement<\/a><\/h2>\r\n

    <\/i>\u00a0 4. VBA to write a Text file using Print <\/em>Statement<\/a><\/h2>\r\n

    <\/i>\u00a0 5. VBA to Append <\/em>to a Text File<\/a><\/h2>\r\n

    <\/i>\u00a0 5. What is FreeFile () function ?<\/a><\/h2>\r\n\r\n\r\n<\/a>\r\n\r\nHow to read a file, will be covered in the next article.\r\n

    Excel VBA Code to Open a Text File<\/h1>\r\nFollowing is the code Syntax to open a Text file:\r\n\r\nOpen [file Path]<\/em> For [mode]<\/em> As [#FileNumber]<\/em><\/strong>\r\n\r\nWith the Above code Syntax, as you can see, there are 3 parameters. [file Path]<\/em> [mode]<\/em> and [#FileNumber]<\/em>\r\n\r\nSo, lets discuss about these three parameters - who are they and what role do they play in opening the text file:\r\n

    What is File Name<\/em><\/h2>\r\nThis is the path of the Text file which is going to be opened.\r\n

    What is Mode<\/em> in Open Text File VBA Code<\/h2>\r\nAs the name suggests, its the control which you want to specify before opening it. There are mainly 3 types of controls or modes possible - Input<\/em><\/strong>, Output<\/em><\/strong> and Append<\/em><\/strong>.\r\nLets discuss one by one these 3 modes\r\n

    Input Mode<\/h2>\r\nThis mode is used for READ ONLY<\/strong><\/em> control. It means, if you open a Text file in Input mode, then you can not write anything in it. All you can do is - read all the content inside the text file. Therefore you can say.. this a read only mode.\r\n

    Output Mode<\/h2>\r\nIf your text file is open in this mode, then you can write content in it. But what is important here to note is : In this mode, your existing file gets overwritten. This means, if there are content already in there in the text file then it will be replaced by the new data which you are trying to save. Therefore, be careful while choosing the mode of the file while opening it.\r\nNow you must be thinking - Then how to append content in an existing text file without overwriting it. Therefore the next mode - Append Mode.\r\n

    Append Mode<\/h2>\r\nAs the name suggests, this mode allow you to append the new content at the end of the text file. It does not overwrite the existing content in the text file.\r\nSo now we have an idea about all these 3 modes. It will be more clear when we use them in the below examples.\r\n\r\nNow Let's learn about our second parameter - #FileNumber<\/em><\/strong>\r\n

    #FileNumber<\/h2>\r\nWhen Text files are opened then windows recognize them by a unique integer value. Valid range of Integers for this parameter is between 1 to 511<\/strong><\/em>.\r\nAs I mentioned above, it should be a unique<\/strong> integer, it is challenging for you to give a fixed number here in case you are dealing with multiple text files. To overcome this challenge you can use a function called FreeFile()<\/strong><\/em>\r\n<\/a>\r\n\r\n

    What is FreeFile() Function?<\/h1>\r\nFreeFile() function returns a unique integer value which represents the file number of the file you opened. This way always a unique (or a FREE File Number - which is not used already) file-Number is assigned automatically by this function.\r\n\r\nNow you know about \u00a0- How to open a Text file<\/strong><\/em>. Let's see some examples and learn how to use it.\r\n

    Sample VBA Code to Open a Text File<\/h1>\r\n\r\n
    \r\nSub OpenTextFile()\r\nDim sFilePath As String\r\nDim fileNumber As Integer\r\n' Full path of the textFile which you want\r\n' to open.\r\nsFilePath = \"C:\\Users\\Vishwa\\Desktop\\LEM.txt\"\r\n\r\n' Assign a unique file numner\r\nfileNumber = FreeFile\r\n' Below statement will open the\r\n' above text file in output mode\r\nOpen sFilePath For Output As #fileNumber\r\nEnd Sub\r\n<\/code><\/pre>\r\n<\/a>\r\n\r\n

    VBA code to create a new TextFile<\/i><\/h1>\r\nIf the file name provided in the File Path provided while writing Open<\/strong> statement, like above, does not exists, then Open statement, automatically creates a TextFile with the same name in the same directory.\r\nThus, using the same Open<\/strong> statement, you can create a new TextFile as well. You don't have to use a different statement to create a new file.\r\n

    Here is a sample code<\/h2>\r\nRead the commented lines written inside the code. I have tried explaining them there as well.\r\n
    \r\nSub createANewtextFile()\r\nDim sFilePath As String\r\nDim fileNumber As Integer\r\n\r\n' In the above path C:\\Users\\Vishwa\\Desktop\\ - exists\r\n' But on the desktop - LEM.txt file does not exist\r\nsFilePath = \"C:\\Users\\Vishwa\\Desktop\\LEM.txt\"\r\n\r\n' Assign a unique file numner\r\nfileNumber = FreeFile\r\n\r\n' in this case, below statement, will\r\n' create a new file with name LEM.txt\r\n' as soon as below statement gets executed\r\n' And also, it is open to write something\r\nOpen sFilePath For Output As #fileNumber\r\n\r\nEnd Sub\r\n<\/code><\/pre>\r\nNote:<\/strong> Only File Name should not exists in order to create a new file. Directories or folders specified must exists. If not, file Not Found Error will be thrown.<\/em>[\/fusion_text]\r\n<\/a>\r\n

    VBA Code to Write content in to Text File<\/h1>\r\n[fusion_text]Basically there are two statements using which you can write content in to a text file in excel VBA : Write\u00a0<\/em><\/strong>or\u00a0Print<\/em><\/strong>[\/fusion_text][title size=\"1\" content_align=\"left\" style_type=\"none\" sep_color=\"\" margin_top=\"\" margin_bottom=\"\" class=\"\" id=\"\"]VBA to write in to Text Files using Write<\/strong> <\/em>statement[\/title][fusion_text]As I mentioned above, one can write in to text files using two statements Write<\/strong> <\/em>or Print.\u00a0<\/em><\/strong>First lets have a look on - how to write content in to a text file using Write statement.[\/fusion_text]\r\nWrite #, InputData 1, InputData 2, ....<\/em>\r\n[title size=\"2\" content_align=\"left\" style_type=\"none\" sep_color=\"\" margin_top=\"\" margin_bottom=\"\" class=\"\" id=\"\"]Where:[\/title][fusion_text]\r\n\r\nFile Number :<\/strong> This is a nemeric value which represents the File. This must be a unique number assigned to each open text files. Text files are identified by these unique numbers assigned to them while opening them. refer the Open File statement to know more about FileNumber\r\n\r\nInput Data1, 2, 3, ...:<\/strong>\r\nThis is the data which you want to write in the Textfile. You can write many different kind of data in each line. Each of these data will be written in textfile in a single line separated by comma.\r\nImportant to note:<\/strong>\r\nBased on the type of the data which you put in InputData 1, 2 etc. Write statement does some common changes while putting them in to TextFile.\r\n\r\nIf the data is Date Type:<\/strong>\u00a0 Then date value is closed within hash sign. Date - 2010-10-13 will be written in TextFile as #2010-10-13#\r\n\r\nString type data:<\/strong>\u00a0 They are stored in double quotes. For example : Input Data - Vishwamitra Mishra will be stored as \"Vishwamitra Mishra\".\r\n\r\nInteger, Double, Long etc :<\/strong>\u00a0 They will be written as they are. There is not formatting done before they are written in text file.\r\n

    Example:<\/h1>\r\nLet's take an example. Export the following table in excel to a Text file using Write statement.\r\n\r\n\"Create\r\n\r\nFrom the above excel cells, I will start reading one by one every column values of each row and write them in Text File.\r\n

    VBA Code - To Write Text File - using Write Statement<\/h1>\r\n\r\n
    \r\nSub WriteTextFileUsingWriteStatement()\r\n\r\nDim sFilePath As String\r\nDim iRow As Integer\r\n\r\nDim OrderDate As Date\r\nDim OrderPriority As String\r\nDim OrderQuantity As Integer\r\nDim Discount As Double\r\nDim ShipMode As String\r\nDim CustomerName As String\r\nDim ShipDate As Date\r\n\r\niRow = 2\r\nsFilePath = \"C:\\Users\\Vishwa\\Desktop\\LEM.txt\"\r\n\r\n' unique file number to access the file uniquely\r\nfileNumber = FreeFile\r\n\r\n' to check if file name LEM.txt exists\r\n' if not, end the program\r\nIf (VBA.Len(VBA.Dir(sFilePath))) = 0 Then MsgBox \"File Does not exists\": End\r\n\r\n' Open the TextFile in Output mode\r\n' in order to write in something\r\nOpen sFilePath For Output As #fileNumber\r\n\r\n' Loop to read one by one every\r\n' non empty row and write them\r\n' in the text file\r\nDo\r\nWith Sheets(\"Orders\")\r\nOrderDate = .Cells(iRow, 1).Value\r\nOrderPriority = .Cells(iRow, 2).Value\r\nOrderQuantity = .Cells(iRow, 3).Value\r\nDiscount = .Cells(iRow, 4).Value\r\nShipMode = .Cells(iRow, 5).Value\r\nCustomerName = .Cells(iRow, 6).Value\r\nShipDate = .Cells(iRow, 7).Value\r\nEnd With\r\n' Now write these data in text file in next line\r\nWrite #fileNumber, OrderDate, OrderPriority, OrderQuantity, Discount, ShipMode, ShipDate\r\n\r\n' go to the next row in Excel sheet\r\niRow = iRow + 1\r\nLoop Until IsEmpty(Sheets(\"Orders\").Cells(iRow, 1).Value)\r\n\r\n' Close the file once all data\r\n' is written in text file\r\nClose #fileNumber\r\nEnd Sub\r\n<\/code><\/pre>\r\n\r\n

    Result : after Running the above code<\/h1>\r\nAfter running the above code, Your text file will look something like this:\r\n\"VBA\r\nNow you can see, as explained above, dates are put under Hash marks (#) and string data is put under double quotes (\" \").\r\n

    Important points to note in above code:<\/h2>\r\n1. I have used Dir$ function<\/strong> to check if TextFile already exists. Why I am thinking it is important to mention is because, if you do not put this check and by mistake your file name is not correct, then Open File statement will create a new TextFile in the same location with your provided name and write the content in that new file.\r\nNote: <\/strong>Open file statement will create a new file only if directory exists. If directory itself does not exists, then Open File statement will through an error - Path Not found.\r\nIt will not create a directory in that case.<\/em>\r\n\r\n2. Instead of using a fixed number<\/strong> like #1, #2 etc. for FileNumber, I have used the function FreeFile() which always finds an available fileNumber which can be assigned to a textFile. It is always a good practise to use this function rather using a hardcoded File Number. This becomes very important when your program start dealing with multiple text files.\r\n\r\n3. To read every line from Excel<\/strong> and write it in textFile, I have used Do..While loop with Until keyword for condition. You can read more about do.. while loop and Until keyword here.<\/a>\r\n\r\n4. Last but not the least<\/strong> - do not forget the Close the file by using the same fileNumber by using the simple statement Close #FileNumber as you can see in the above code.\r\n<\/a>\r\n

    How to write TextFile using Print Statement<\/h1>\r\nSyntax remains exactly same as Write statement. Main difference between Write and Print statement is in the formatting of the Output in TextFile. You will see in detail. Let's start with the Syntax:\r\n

    Syntax of Print statement:<\/h2>\r\nPrint #, InputData1, InputData2, ....\r\n

    Where:<\/h2>\r\nFile Number :<\/strong> This is a numeric value which represents the File. It is exactly same as explained above in Write Statement.\r\n\r\nInput Data 1, 2, 3, ...:<\/strong>\r\nThis is the data which you want to write in the Textfile. You can write many different kind of data in each line. Each of these data will be written in textfile in a Proper formatting with proper spacing. TextFile which you get from Print statement is well formatted for printing purposes. That means spaces between columns are adjusted based on the values in those columns. You will see in the example below.\r\n

    Important to note:<\/h2>\r\nUnlike Write statement, this does not change any of the formatting of the data for Date or String type. It just put them as they are.\r\nValues of difference columns are not separated by Comma. Rather they are separated by space(s) depending on how many spaces required to make the textfile in a printable format.\r\n

    Example: To write Text File using PRINT Statement<\/h1>\r\nLet's take the same example as above. Now we will export the same table from excel to a Text file using Print statement.\r\n\r\nHere is the code\r\n\r\n
    \r\nSub WriteTextFileUsingPrintStatement()\r\n\r\nDim sFilePath As String\r\nDim iRow As Integer\r\n\r\nDim OrderDate As Date\r\nDim OrderPriority As String\r\nDim OrderQuantity As Integer\r\nDim Discount As Double\r\nDim ShipMode As String\r\nDim CustomerName As String\r\nDim ShipDate As Date\r\n\r\niRow = 2\r\nsFilePath = \"C:\\Users\\Vishwa\\Desktop\\LEM.txt\"\r\n\r\n' unique file number to access the file uniquely\r\nfileNumber = FreeFile\r\n\r\n' to check if file name LEM.txt exists\r\n' if not, end the program\r\nIf (VBA.Len(VBA.Dir(sFilePath))) = 0 Then MsgBox \"File Does not exists\": End\r\n\r\n' Open the TextFile in Output mode\r\n' in order to write in something\r\nOpen sFilePath For Output As #fileNumber\r\n\r\n' Loop to read one by one every\r\n' non empty row and write them\r\n' in the text file\r\nDo\r\nWith Sheets(\"Orders\")\r\nOrderDate = .Cells(iRow, 1).Value\r\nOrderPriority = .Cells(iRow, 2).Value\r\nOrderQuantity = .Cells(iRow, 3).Value\r\nDiscount = .Cells(iRow, 4).Value\r\nShipMode = .Cells(iRow, 5).Value\r\nCustomerName = .Cells(iRow, 6).Value\r\nShipDate = .Cells(iRow, 7).Value\r\nEnd With\r\n' Now write these data in text file in next line\r\nPrint #fileNumber, OrderDate, OrderPriority, OrderQuantity, Discount, ShipMode, ShipDate\r\n\r\n' go to the next row in Excel sheet\r\niRow = iRow + 1\r\nLoop Until IsEmpty(Sheets(\"Orders\").Cells(iRow, 1).Value)\r\n\r\n' Close the file once all data\r\n' is written in text file\r\nClose #fileNumber\r\nEnd Sub\r\n<\/code><\/pre>\r\n\r\n

    Result : after Running the above code<\/h1>\r\nAfter running the above code, Your text file will look something like this:\r\n\"Text\r\n\r\nNow you can see, as explained above:<\/strong>\r\n\r\n1. <\/strong>None of the data are formatted. They are, in fact, put as they are in Excel cells.\r\n2. <\/strong>Spaces between columns are adjusted based on the data in each columns.\r\n
    \r\n\r\n<\/i> Important to know...<\/strong>\r\n\r\nThere is another \u2013 in fact important \u2013 difference between Write and Print statement. That you will realize while reading above two TextFiles \u2013\r\n1.<\/strong> Written using Write Statement\r\n2.<\/strong> Written using Print Statement. This is a hint for now. It will be explained in detail in the next article, where we will be talking all about reading a TextFile.\r\n\r\n<\/div>[\/fusion_text][fusion_text]In all the above examples of writing to a text box, I have used the File Open mode as Output<\/strong>. This means, every time you run the code, all the content of the text file will be replaced with the new one. Therefore, let's take an example, of how can we append to the existing content in a text file using write or print statement.\r\n<\/a>\r\n\r\n

    VBA Code to Append to Text File<\/h1>\r\nThe whole trick lies in to the mode you open your text file during your VBA program. Why I say that, because you do not need to change anything else in the Write<\/em><\/strong> or Print<\/em><\/strong> statements in order to append and not to replace. Isn't it simple? So the complete VBA code remains same as it is there for replacing the whole content - except changing the open mode - from Output <\/strong>to Append<\/strong>. What these modes are -<\/strong> They are explained in the beginning of the article.<\/em>\r\n\r\n
    \r\nSub AppendToTextFile()\r\n\r\nDim sFilePath As String\r\nDim iRow As Integer\r\n\r\nDim OrderDate As Date\r\nDim OrderPriority As String\r\nDim OrderQuantity As Integer\r\nDim Discount As Double\r\nDim ShipMode As String\r\nDim CustomerName As String\r\nDim ShipDate As Date\r\n\r\n    iRow = 2\r\n    sFilePath = \"C:\\Users\\Vishwa\\Desktop\\LEM.txt\"\r\n    \r\n    ' unique file number to access the file uniquely\r\n    fileNumber = FreeFile\r\n    \r\n    ' to check if file name LEM.txt exists\r\n    ' if not, end the program\r\n    If (VBA.Len(VBA.Dir(sFilePath))) = 0 Then MsgBox \"File Does not exists\": End\r\n    \r\n    ' Open the TextFile in Append mode\r\n    ' in order to write in something\r\n    Open sFilePath For Append As #fileNumber\r\n    \r\n    ' Loop to read one by one every\r\n    ' non empty row and write them\r\n    ' in the text file\r\n    Do\r\n    With Sheets(\"Orders\")\r\n        OrderDate = .Cells(iRow, 1).Value\r\n        OrderPriority = .Cells(iRow, 2).Value\r\n        OrderQuantity = .Cells(iRow, 3).Value\r\n        Discount = .Cells(iRow, 4).Value\r\n        ShipMode = .Cells(iRow, 5).Value\r\n        CustomerName = .Cells(iRow, 6).Value\r\n        ShipDate = .Cells(iRow, 7).Value\r\n    End With\r\n    ' Now write these data in text file in next line\r\n    Write #fileNumber, OrderDate, OrderPriority, OrderQuantity, Discount, ShipMode, ShipDate\r\n    \r\n    ' go to the next row in Excel sheet\r\n    iRow = iRow + 1\r\n    Loop Until IsEmpty(Sheets(\"Orders\").Cells(iRow, 1).Value)\r\n    \r\n    ' Close the file once all data\r\n    ' is written in text file\r\n    Close #fileNumber\r\nEnd Sub\r\n<\/code><\/pre>\r\n\r\nNote: <\/strong>All the new information gets appended at the end of the text file (from the first blank line)<\/em>\r\nDid you like this article? Then share it with your friends\u2026 spread knowledge...\"\r\nLearn All about interacting with Text Files in Excel VBA like opening, creating, writing, reading etc. from Text Files using Excel VBA code\"","_et_gb_content_width":"","footnotes":""},"categories":[1246,1675],"tags":[],"yoast_head":"\nComplete VBA Guide to Interact with Text Files with Examples<\/title>\n<meta name=\"description\" content=\"VBA to open Text file. VBA to write content in to text file. VBA to append content to text file. VBA to read text file. Write and Print statement in VBA.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/vmlogger.com\/excel\/2017\/08\/vba-to-open-and-write-text-files\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"VBA Guide to Interact with Text Files - Part - 1 of 2\" \/>\n<meta property=\"og:description\" content=\"VBA to open Text file. VBA to write content in to text file. VBA to append content to text file. VBA to read text file. Write and Print statement in VBA.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/vmlogger.com\/excel\/2017\/08\/vba-to-open-and-write-text-files\/\" \/>\n<meta property=\"og:site_name\" content=\"Let's excel in Excel\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/www.facebook.com\/vmlogger\" \/>\n<meta property=\"article:author\" content=\"http:\/\/www.facebook.com\/vmlogger\" \/>\n<meta property=\"article:published_time\" content=\"2017-08-23T21:24:39+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-08-17T19:18:48+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2017\/08\/All-About-TextFiles-VBA-Tutorial-3.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"800\" \/>\n\t<meta property=\"og:image:height\" content=\"538\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Vishwamitra Mishra\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/www.twitter.com\/learnexcelmacro\" \/>\n<meta name=\"twitter:site\" content=\"@learnexcelmacro\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Vishwamitra Mishra\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"14 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/vmlogger.com\/excel\/2017\/08\/vba-to-open-and-write-text-files\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/2017\/08\/vba-to-open-and-write-text-files\/\"},\"author\":{\"name\":\"Vishwamitra Mishra\",\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5\"},\"headline\":\"VBA Guide to Interact with Text Files – Part – 1 of 2\",\"datePublished\":\"2017-08-23T21:24:39+00:00\",\"dateModified\":\"2022-08-17T19:18:48+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/2017\/08\/vba-to-open-and-write-text-files\/\"},\"wordCount\":2349,\"commentCount\":4,\"publisher\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5\"},\"articleSection\":[\"Excel Macro\",\"Excel Macro Tutorial\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/vmlogger.com\/excel\/2017\/08\/vba-to-open-and-write-text-files\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/vmlogger.com\/excel\/2017\/08\/vba-to-open-and-write-text-files\/\",\"url\":\"https:\/\/vmlogger.com\/excel\/2017\/08\/vba-to-open-and-write-text-files\/\",\"name\":\"Complete VBA Guide to Interact with Text Files with Examples\",\"isPartOf\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/#website\"},\"datePublished\":\"2017-08-23T21:24:39+00:00\",\"dateModified\":\"2022-08-17T19:18:48+00:00\",\"description\":\"VBA to open Text file. VBA to write content in to text file. VBA to append content to text file. VBA to read text file. Write and Print statement in VBA.\",\"breadcrumb\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/2017\/08\/vba-to-open-and-write-text-files\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/vmlogger.com\/excel\/2017\/08\/vba-to-open-and-write-text-files\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/vmlogger.com\/excel\/2017\/08\/vba-to-open-and-write-text-files\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/vmlogger.com\/excel\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Excel Macro\",\"item\":\"https:\/\/vmlogger.com\/excel\/macro\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"VBA Guide to Interact with Text Files – Part – 1 of 2\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/vmlogger.com\/excel\/#website\",\"url\":\"https:\/\/vmlogger.com\/excel\/\",\"name\":\"Let's excel in Excel\",\"description\":\"Let's share knowledge\",\"publisher\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/vmlogger.com\/excel\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5\",\"name\":\"Vishwamitra Mishra\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2022\/07\/avataaars-1.png\",\"contentUrl\":\"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2022\/07\/avataaars-1.png\",\"width\":528,\"height\":560,\"caption\":\"Vishwamitra Mishra\"},\"logo\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/image\/\"},\"description\":\"My name is Vishwamitra Mishra. Friends Call me Vishwa. I hold a Bachelor\u2019s Degree in Computer Science from D.A.V.V. Indore & currently working as a Technical Lead having over 7 years of experience.\",\"sameAs\":[\"http:\/\/www.learnexcelmacro.com\",\"http:\/\/www.facebook.com\/vmlogger\",\"https:\/\/twitter.com\/https:\/\/www.twitter.com\/learnexcelmacro\",\"https:\/\/www.youtube.com\/c\/VMLogger\"],\"url\":\"https:\/\/vmlogger.com\/excel\/author\/vishwamitra\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Complete VBA Guide to Interact with Text Files with Examples","description":"VBA to open Text file. VBA to write content in to text file. VBA to append content to text file. VBA to read text file. Write and Print statement in VBA.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/vmlogger.com\/excel\/2017\/08\/vba-to-open-and-write-text-files\/","og_locale":"en_US","og_type":"article","og_title":"VBA Guide to Interact with Text Files - Part - 1 of 2","og_description":"VBA to open Text file. VBA to write content in to text file. VBA to append content to text file. VBA to read text file. Write and Print statement in VBA.","og_url":"https:\/\/vmlogger.com\/excel\/2017\/08\/vba-to-open-and-write-text-files\/","og_site_name":"Let's excel in Excel","article_publisher":"http:\/\/www.facebook.com\/vmlogger","article_author":"http:\/\/www.facebook.com\/vmlogger","article_published_time":"2017-08-23T21:24:39+00:00","article_modified_time":"2022-08-17T19:18:48+00:00","og_image":[{"width":800,"height":538,"url":"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2017\/08\/All-About-TextFiles-VBA-Tutorial-3.jpg","type":"image\/jpeg"}],"author":"Vishwamitra Mishra","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/www.twitter.com\/learnexcelmacro","twitter_site":"@learnexcelmacro","twitter_misc":{"Written by":"Vishwamitra Mishra","Est. reading time":"14 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/vmlogger.com\/excel\/2017\/08\/vba-to-open-and-write-text-files\/#article","isPartOf":{"@id":"https:\/\/vmlogger.com\/excel\/2017\/08\/vba-to-open-and-write-text-files\/"},"author":{"name":"Vishwamitra Mishra","@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5"},"headline":"VBA Guide to Interact with Text Files – Part – 1 of 2","datePublished":"2017-08-23T21:24:39+00:00","dateModified":"2022-08-17T19:18:48+00:00","mainEntityOfPage":{"@id":"https:\/\/vmlogger.com\/excel\/2017\/08\/vba-to-open-and-write-text-files\/"},"wordCount":2349,"commentCount":4,"publisher":{"@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5"},"articleSection":["Excel Macro","Excel Macro Tutorial"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/vmlogger.com\/excel\/2017\/08\/vba-to-open-and-write-text-files\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/vmlogger.com\/excel\/2017\/08\/vba-to-open-and-write-text-files\/","url":"https:\/\/vmlogger.com\/excel\/2017\/08\/vba-to-open-and-write-text-files\/","name":"Complete VBA Guide to Interact with Text Files with Examples","isPartOf":{"@id":"https:\/\/vmlogger.com\/excel\/#website"},"datePublished":"2017-08-23T21:24:39+00:00","dateModified":"2022-08-17T19:18:48+00:00","description":"VBA to open Text file. VBA to write content in to text file. VBA to append content to text file. VBA to read text file. Write and Print statement in VBA.","breadcrumb":{"@id":"https:\/\/vmlogger.com\/excel\/2017\/08\/vba-to-open-and-write-text-files\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/vmlogger.com\/excel\/2017\/08\/vba-to-open-and-write-text-files\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/vmlogger.com\/excel\/2017\/08\/vba-to-open-and-write-text-files\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/vmlogger.com\/excel\/"},{"@type":"ListItem","position":2,"name":"Excel Macro","item":"https:\/\/vmlogger.com\/excel\/macro\/"},{"@type":"ListItem","position":3,"name":"VBA Guide to Interact with Text Files – Part – 1 of 2"}]},{"@type":"WebSite","@id":"https:\/\/vmlogger.com\/excel\/#website","url":"https:\/\/vmlogger.com\/excel\/","name":"Let's excel in Excel","description":"Let's share knowledge","publisher":{"@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/vmlogger.com\/excel\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5","name":"Vishwamitra Mishra","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/image\/","url":"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2022\/07\/avataaars-1.png","contentUrl":"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2022\/07\/avataaars-1.png","width":528,"height":560,"caption":"Vishwamitra Mishra"},"logo":{"@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/image\/"},"description":"My name is Vishwamitra Mishra. Friends Call me Vishwa. I hold a Bachelor\u2019s Degree in Computer Science from D.A.V.V. Indore & currently working as a Technical Lead having over 7 years of experience.","sameAs":["http:\/\/www.learnexcelmacro.com","http:\/\/www.facebook.com\/vmlogger","https:\/\/twitter.com\/https:\/\/www.twitter.com\/learnexcelmacro","https:\/\/www.youtube.com\/c\/VMLogger"],"url":"https:\/\/vmlogger.com\/excel\/author\/vishwamitra\/"}]}},"_links":{"self":[{"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/posts\/14000"}],"collection":[{"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/users\/45"}],"replies":[{"embeddable":true,"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/comments?post=14000"}],"version-history":[{"count":0,"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/posts\/14000\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/media\/14095"}],"wp:attachment":[{"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/media?parent=14000"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/categories?post=14000"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/tags?post=14000"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}