{"id":15922,"date":"2023-06-15T14:42:57","date_gmt":"2023-06-15T14:42:57","guid":{"rendered":"https:\/\/officetuts.net\/excel\/?p=15922"},"modified":"2024-03-30T12:04:53","modified_gmt":"2024-03-30T12:04:53","slug":"save-a-text-file-with-utf-8-encoding-in-vba","status":"publish","type":"post","link":"https:\/\/officetuts.net\/excel\/vba\/save-a-text-file-with-utf-8-encoding-in-vba\/","title":{"rendered":"Save a Text File with UTF-8 Encoding in VBA"},"content":{"rendered":"\n

There are a lot of saving options in Excel, and we can manipulate the saving options as well as any other. When it comes to this, different saving options are available for different file types.<\/p>\n\n\n\n

For the options that are not available directly to us, we can always use VBA to achieve what we want. Such is the case with saving text files with the UTF-8 encoding system<\/strong>. In the example below, we will show how to do this. <\/p>\n\n\n\n

Save a Text File with UTF-8 Encoding in VBA<\/h2>\n\n\n\n

UTF-8<\/strong>, in its essence, is an encoding system. It gives us the option to represent characters in the form of ASCII text<\/strong>, but it also allows international characters, such as Arabic or Chinese.<\/p>\n\n\n\n

To use VBA in order to save a text file with UTF-8 encoding<\/strong>, we first need to access the VBA, and we will do that by pressing ALT + F11 on our keyboard<\/strong>. When we do that, there will be a separate window shown, and on this one, we will go to Insert >> Module<\/strong>:<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

In the module, we are going to insert the following code:<\/p>\n\n\n\n

Sub SaveTextFileWithUTF8Encoding()\n    Dim Path As String\n    Dim Content As String\n    Dim fso As Object\n    Dim obj As Object\n    ' File path\n    Path = \"C:\\Users\\User\\Documents\\VBA Upwork\\Save a Text File with UTF-8 Encoding in VBA\\TextFile.txt\"\n    ' Content of the file\n    Content = \"This is the content that we have in our Text file\"\n    ' New FileSystemObject\n    Set fso = CreateObject(\"Scripting.FileSystemObject\")\n    ' Open a text file for writing with UTF-8 encoding\n    Set obj = fso.CreateTextFile(Path, True, True)\n    ' Write the content to the file\n    obj.Write Content\n    ' Close the file\n    obj.Close\n    ' Clean up\n    Set obj = Nothing\n    Set fso = Nothing\n    MsgBox \"Text file saved with UTF-8 encoding.\"\nEnd Sub<\/code><\/pre>\n\n\n\n

This is what this code looks like in the module:<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

For our code to work, we need to reference a specific library<\/strong> in our VBA project. A library is basically a code structure, that is pre-defined, and we can access them to get multiple options. In VBA, libraries usually give us new functions and objects. In our example, we need a Scripting library<\/strong>. You can find and add libraries by going to the Tools tab in the Module<\/strong>, and then choosing References<\/strong>: <\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

To add a certain library, you can search for them in the window that appears, and add them by clicking on the little box next to the library title<\/strong>:<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

If you do not have a particular library in the list, you can always add them. Since we do not have a Scripting library installed, in this particular case, we can use Microsoft Object Library<\/strong>, which you can see as chosen in the picture above, to make our code work. This will allow us to use the FileSystemObject<\/strong> (which is a part of the Scripting library<\/strong>) directly.  <\/p>\n\n\n\n

Now, as for an explanation of the code: <\/p>\n\n\n\n

The first part declares four variables: Path and Content as a String, and fso and obj as an Object<\/strong>. <\/p>\n\n\n\n

Then we set the values for these variables. The Path<\/strong> will be where our file will be stored (we use the link where our folder is located). The Content variable will store our text<\/strong>, which will be \u201cThis is the content that we have in our Text file\u201d<\/strong>. Our fso variable<\/strong> will be used to create an object (basically, our text file), which we call the previously described Scripting library and FileSystemObject<\/strong>. The obj<\/strong> variable<\/strong> will make the text file in our object.     <\/p>\n\n\n\n

For the final step, we will write the content in our file, and then close that file. <\/p>\n\n\n\n

Clean up part is used to delete the created object and file from the memory<\/strong> because these things can take up a lot of space.  <\/p>\n\n\n\n

Lastly, we will create a message that the file was saved with UTF-8 encoding.<\/p>\n\n\n\n

When we execute the code by pressing F5 while in the module<\/strong>, we will get that exact message:<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

And our file will be saved<\/strong> in the desired folder:<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

When we open the file, we can see our content<\/strong> is there as well: <\/p>\n\n\n\n

\"\"<\/figure>\n","protected":false},"excerpt":{"rendered":"

There are a lot of saving options in Excel, and we can manipulate the saving options as well as any other. When it…<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[190],"yoast_head":"\nSave a Text File with UTF-8 Encoding in VBA<\/title>\n<meta name=\"description\" content=\"Learn how to save a text file with UTF-8 encoding in Excel VBA using a simple code. This will allow you to use international characters in your files.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/officetuts.net\/excel\/vba\/save-a-text-file-with-utf-8-encoding-in-vba\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Save a Text File with UTF-8 Encoding in VBA\" \/>\n<meta property=\"og:description\" content=\"Learn how to save a text file with UTF-8 encoding in Excel VBA using a simple code. This will allow you to use international characters in your files.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/officetuts.net\/excel\/vba\/save-a-text-file-with-utf-8-encoding-in-vba\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-06-15T14:42:57+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-30T12:04:53+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2023\/06\/word-image-15922-1.png\" \/>\n<meta name=\"author\" content=\"Harun Spahic\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Harun Spahic\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/save-a-text-file-with-utf-8-encoding-in-vba\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/save-a-text-file-with-utf-8-encoding-in-vba\/\"},\"author\":{\"name\":\"Harun Spahic\",\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/694491c70f776081acdac4ae41f222a2\"},\"headline\":\"Save a Text File with UTF-8 Encoding in VBA\",\"datePublished\":\"2023-06-15T14:42:57+00:00\",\"dateModified\":\"2024-03-30T12:04:53+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/save-a-text-file-with-utf-8-encoding-in-vba\/\"},\"wordCount\":597,\"publisher\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\"},\"image\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/save-a-text-file-with-utf-8-encoding-in-vba\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2023\/06\/word-image-15922-1.png\",\"keywords\":[\"pinterest\"],\"articleSection\":[\"vba\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/save-a-text-file-with-utf-8-encoding-in-vba\/\",\"url\":\"https:\/\/officetuts.net\/excel\/vba\/save-a-text-file-with-utf-8-encoding-in-vba\/\",\"name\":\"Save a Text File with UTF-8 Encoding in VBA\",\"isPartOf\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/save-a-text-file-with-utf-8-encoding-in-vba\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/save-a-text-file-with-utf-8-encoding-in-vba\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2023\/06\/word-image-15922-1.png\",\"datePublished\":\"2023-06-15T14:42:57+00:00\",\"dateModified\":\"2024-03-30T12:04:53+00:00\",\"description\":\"Learn how to save a text file with UTF-8 encoding in Excel VBA using a simple code. This will allow you to use international characters in your files.\",\"breadcrumb\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/save-a-text-file-with-utf-8-encoding-in-vba\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/officetuts.net\/excel\/vba\/save-a-text-file-with-utf-8-encoding-in-vba\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/save-a-text-file-with-utf-8-encoding-in-vba\/#primaryimage\",\"url\":\"https:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2023\/06\/word-image-15922-1.png\",\"contentUrl\":\"https:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2023\/06\/word-image-15922-1.png\",\"width\":347,\"height\":206},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/save-a-text-file-with-utf-8-encoding-in-vba\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/officetuts.net\/excel\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Save a Text File with UTF-8 Encoding in VBA\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/officetuts.net\/excel\/#website\",\"url\":\"https:\/\/officetuts.net\/excel\/\",\"name\":\"\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/officetuts.net\/excel\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\",\"name\":\"Tomasz Decker\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/18cbe22837193574870ae40ba56bf712?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/18cbe22837193574870ae40ba56bf712?s=96&d=mm&r=g\",\"caption\":\"Tomasz Decker\"},\"logo\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/\"},\"description\":\"Spreadsheet and Python enthusiast.\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/694491c70f776081acdac4ae41f222a2\",\"name\":\"Harun Spahic\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/b7b887a2249679182be5550a4421a7a9?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/b7b887a2249679182be5550a4421a7a9?s=96&d=mm&r=g\",\"caption\":\"Harun Spahic\"},\"sameAs\":[\"http:\/\/human.bsru.ac.th\/\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Save a Text File with UTF-8 Encoding in VBA","description":"Learn how to save a text file with UTF-8 encoding in Excel VBA using a simple code. This will allow you to use international characters in your files.","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:\/\/officetuts.net\/excel\/vba\/save-a-text-file-with-utf-8-encoding-in-vba\/","og_locale":"en_US","og_type":"article","og_title":"Save a Text File with UTF-8 Encoding in VBA","og_description":"Learn how to save a text file with UTF-8 encoding in Excel VBA using a simple code. This will allow you to use international characters in your files.","og_url":"https:\/\/officetuts.net\/excel\/vba\/save-a-text-file-with-utf-8-encoding-in-vba\/","article_published_time":"2023-06-15T14:42:57+00:00","article_modified_time":"2024-03-30T12:04:53+00:00","og_image":[{"url":"https:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2023\/06\/word-image-15922-1.png"}],"author":"Harun Spahic","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Harun Spahic","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/officetuts.net\/excel\/vba\/save-a-text-file-with-utf-8-encoding-in-vba\/#article","isPartOf":{"@id":"https:\/\/officetuts.net\/excel\/vba\/save-a-text-file-with-utf-8-encoding-in-vba\/"},"author":{"name":"Harun Spahic","@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/694491c70f776081acdac4ae41f222a2"},"headline":"Save a Text File with UTF-8 Encoding in VBA","datePublished":"2023-06-15T14:42:57+00:00","dateModified":"2024-03-30T12:04:53+00:00","mainEntityOfPage":{"@id":"https:\/\/officetuts.net\/excel\/vba\/save-a-text-file-with-utf-8-encoding-in-vba\/"},"wordCount":597,"publisher":{"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42"},"image":{"@id":"https:\/\/officetuts.net\/excel\/vba\/save-a-text-file-with-utf-8-encoding-in-vba\/#primaryimage"},"thumbnailUrl":"https:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2023\/06\/word-image-15922-1.png","keywords":["pinterest"],"articleSection":["vba"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/officetuts.net\/excel\/vba\/save-a-text-file-with-utf-8-encoding-in-vba\/","url":"https:\/\/officetuts.net\/excel\/vba\/save-a-text-file-with-utf-8-encoding-in-vba\/","name":"Save a Text File with UTF-8 Encoding in VBA","isPartOf":{"@id":"https:\/\/officetuts.net\/excel\/#website"},"primaryImageOfPage":{"@id":"https:\/\/officetuts.net\/excel\/vba\/save-a-text-file-with-utf-8-encoding-in-vba\/#primaryimage"},"image":{"@id":"https:\/\/officetuts.net\/excel\/vba\/save-a-text-file-with-utf-8-encoding-in-vba\/#primaryimage"},"thumbnailUrl":"https:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2023\/06\/word-image-15922-1.png","datePublished":"2023-06-15T14:42:57+00:00","dateModified":"2024-03-30T12:04:53+00:00","description":"Learn how to save a text file with UTF-8 encoding in Excel VBA using a simple code. This will allow you to use international characters in your files.","breadcrumb":{"@id":"https:\/\/officetuts.net\/excel\/vba\/save-a-text-file-with-utf-8-encoding-in-vba\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/officetuts.net\/excel\/vba\/save-a-text-file-with-utf-8-encoding-in-vba\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/officetuts.net\/excel\/vba\/save-a-text-file-with-utf-8-encoding-in-vba\/#primaryimage","url":"https:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2023\/06\/word-image-15922-1.png","contentUrl":"https:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2023\/06\/word-image-15922-1.png","width":347,"height":206},{"@type":"BreadcrumbList","@id":"https:\/\/officetuts.net\/excel\/vba\/save-a-text-file-with-utf-8-encoding-in-vba\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/officetuts.net\/excel\/"},{"@type":"ListItem","position":2,"name":"Save a Text File with UTF-8 Encoding in VBA"}]},{"@type":"WebSite","@id":"https:\/\/officetuts.net\/excel\/#website","url":"https:\/\/officetuts.net\/excel\/","name":"","description":"","publisher":{"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/officetuts.net\/excel\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42","name":"Tomasz Decker","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/18cbe22837193574870ae40ba56bf712?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/18cbe22837193574870ae40ba56bf712?s=96&d=mm&r=g","caption":"Tomasz Decker"},"logo":{"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/"},"description":"Spreadsheet and Python enthusiast."},{"@type":"Person","@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/694491c70f776081acdac4ae41f222a2","name":"Harun Spahic","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/b7b887a2249679182be5550a4421a7a9?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b7b887a2249679182be5550a4421a7a9?s=96&d=mm&r=g","caption":"Harun Spahic"},"sameAs":["http:\/\/human.bsru.ac.th\/"]}]}},"_links":{"self":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts\/15922"}],"collection":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/comments?post=15922"}],"version-history":[{"count":1,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts\/15922\/revisions"}],"predecessor-version":[{"id":15930,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts\/15922\/revisions\/15930"}],"wp:attachment":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/media?parent=15922"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/categories?post=15922"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/tags?post=15922"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}