{"id":54089,"date":"2023-08-12T15:47:56","date_gmt":"2023-08-12T14:47:56","guid":{"rendered":"https:\/\/ekiwi-blog.de\/54089\/vba-check-whether-a-folderdirectory-exists-folderexists\/"},"modified":"2023-08-12T15:58:15","modified_gmt":"2023-08-12T14:58:15","slug":"vba-check-whether-a-folderdirectory-exists-folderexists","status":"publish","type":"post","link":"https:\/\/ekiwi-blog.de\/en\/54089\/vba-check-whether-a-folderdirectory-exists-folderexists\/","title":{"rendered":"VBA Check whether a folder\/directory exists (FolderExists)"},"content":{"rendered":"<p><abbr title=\"Visual Basic for Application\">VBA<\/abbr> is very well suited for getting rid of certain annoying tasks under Microsoft Office and limiting one&#8217;s productivity to the essentials.<\/p>\n<p>If you write a lot of VBA code, you will certainly need a function at some point to <strong>check whether a folder or directory path exists<\/strong>. It is actually part of good programming practice to carry out such a check. Because if you want to save something using VBA code, for example an e-mail or its <a title=\"VBA save email attachment\" href=\"https:\/\/ekiwi-blog.de\/en\/51740\/vba-save-e-mail-attachment\/\">attachments<\/a>, you will get an exception message if the selected directory does not exist.<\/p>\n<p>Testing if the folder exists is easily done with this function:<\/p>\n<pre><code>CreateObject(\"Scripting.FileSystemObject\").FolderExists(strPath)<\/code><\/pre>\n<p>The <em><strong>CreateObject<\/strong> <\/em> function returns an <em>ActiveX<\/em> object. The <em>ActiceX<\/em> object, in general terms, makes other operating system level functions available on Windows. Since the parameter <em>&#8220;Scripting.FileSystemObject&#8221;<\/em> is passed here, <u>file <a href=\"https:\/\/ekiwi-blog.de\/en\/49271\/vba-read-out-folder-and-directory-names-and-list-in-excel\/\" title=\"VBA: Read out folder and directory names and list in Excel\">and directory<\/a> functions<\/u> are made available in this case. In our use case we need the function <em><strong>FolderExists<\/strong><\/em>. As a transfer parameter, one specifies a string (<em>strPath<\/em>) with the complete directory path. The function then returns <em>True<\/em> or <em>False<\/em>, depending on whether the folder exists.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of content<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/ekiwi-blog.de\/en\/54089\/vba-check-whether-a-folderdirectory-exists-folderexists\/#Code_example_FolderExists\" >Code example FolderExists<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/ekiwi-blog.de\/en\/54089\/vba-check-whether-a-folderdirectory-exists-folderexists\/#Code_example_Dir\" >Code example Dir<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/ekiwi-blog.de\/en\/54089\/vba-check-whether-a-folderdirectory-exists-folderexists\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Code_example_FolderExists\"><\/span>Code example <em>FolderExists<\/em><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In this code example we work with variables for the folder path (strPath) and the boolean return value of the function (isDir), as the <a href=\"https:\/\/ekiwi-blog.de\/en\/26678\/where-is-itunes-exe-found-location-directory-path\/\" title=\"Where is iTunes.exe found (location directory path)?\">directory path<\/a> will often be present as a string variable, for example as a passed parameter of a function. The function <em>Debug.Print<\/em> then writes the output to the direct window.<\/p>\n<pre><code><span style=\"color: #3366ff;\">Dim<\/span> strPath <span style=\"color: #3366ff;\">As String<\/span>.\r\n<span style=\"color: #3366ff;\">Dim<\/span> isDir <span style=\"color: #3366ff;\">As Boolean<\/span>\r\nstrPath = \"c:\\Users\\MyUserName\\Desktop\\\"\r\nisDir = CreateObject(\"Scripting.FileSystemObject\").FolderExists(strPath)\r\n\r\n<span style=\"color: #3366ff;\">Debug.Print<\/span> isDir<\/code><\/pre>\n<p>Instead of <em>Debug.Print<\/em>, one will of course process the result <em>isDir<\/em> and check with an <em>If<\/em> statement whether the directory check is <em>True<\/em> or <em>False<\/em>. If the directory does not exist yet, you could create the directory with the <em>MkDir(&#8230;)<\/em> command in another statement.<\/p>\n<pre><code><span style=\"color: #3366ff;\">Dim<\/span> strPath <span style=\"color: #3366ff;\">As String<\/span>.\r\n<span style=\"color: #3366ff;\">Dim<\/span> isDir <span style=\"color: #3366ff;\">As Boolean<\/span>\r\nstrPath = \"c:\\Users\\MyUserName\\Desktop\\\"\r\nisDir = CreateObject(\"Scripting.FileSystemObject\").FolderExists(strPath)\r\n\r\n<span style=\"color: #3366ff;\">If<\/span> isDir = <span style=\"color: #3366ff;\">True Then<\/span>.\r\n  <span style=\"color: #339966;\">'Do something, for example save a file under the directory<\/span>.\r\n  <span style=\"color: #3366ff;\">Debug.Print<\/span> \"Directory exists\".\r\n<span style=\"colour: #3366ff;\">Else<\/span>\r\n  <span style=\"color: #339966;\">'Do something if folder does not exist<\/span>.\r\n  <span style=\"color: #3366ff;\">Debug.Print<\/span> \"Directory does not exist\"\r\n<span style=\"colour: #3366ff;\">End If<\/span><\/code><\/pre>\n<p>For those lazy about writing, here&#8217;s another short variant:<\/p>\n<pre><code><span style=\"color: #3366ff;\">If<\/span> CreateObject(\"Scripting.FileSystemObject\").FolderExists(\"c:\\Users\\MyUserName\\Desktop\\\") Then.\r\n  <span style=\"color: #339966;\">'Do something, for example save a file under the directory<\/span>.\r\n  <span style=\"color: #3366ff;\">Debug.Print<\/span> \"Directory exists\".\r\n<span style=\"colour: #3366ff;\">Else<\/span>\r\n  <span style=\"color: #339966;\">'Do something if folder does not exist<\/span>.\r\n  <span style=\"color: #3366ff;\">Debug.Print<\/span> \"Folder does not exist\"\r\n<span style=\"colour: #3366ff;\">End If<\/span><\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Code_example_Dir\"><\/span>Code example <em>Dir<\/em><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Another method, which I don&#8217;t find as elegant, but which works just as well, is shown in the following code example. Here, the function <em>Dir(&#8230;)<\/em> is used, which returns an empty string if the folder does not exist.<\/p>\n<pre><code><span style=\"color: #339966;\"> 'Define string for folder path<\/span>.\r\n  <span style=\"colour: #3366ff;\">Dim<\/span> strFolderPath <span style=\"colour: #3366ff;\">As String<\/span>.\r\n  strFolderPath = \"c:\\Users\\MyUserName\\Desktop\\1\\\"\r\n  <span style=\"color: #339966;\">' Check if folder already exists<\/span>.\r\n  <span style=\"color: #3366ff;\">If<\/span> Dir(strFolderPath, vbDirectory) = \"\" <span style=\"color: #3366ff;\">Then<\/span>.\r\n    <span style=\"color: #339966;\">' Folder does not exist yet<\/span>.\r\n    <span style=\"color: #3366ff;\">Debug.Print<\/span> \"Folder does not exist\".\r\n  <span style=\"color: #3366ff;\">Else<\/span>\r\n    <span style=\"color: #339966;\">' Folder exists<\/span>.\r\n    <span style=\"color: #3366ff;\">Debug.Print<\/span> \"Folder exists!\"\r\n  <span style=\"color: #3366ff;\">End If<\/span><\/code><\/pre>\n<p>As parameters, you have to pass the directory path (<em>strFolderPath<\/em>) to the <em>Dir<\/em> function as well as the attribute <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/microsoft.visualbasic.constants.vbdirectory?view=net-5.0\" target=\"_blank\" rel=\"noopener noreferrer\"><em>vbDirectory<\/em><\/a>.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>So you see, it&#8217;s relatively easy in VBA to check if a folder exists. And you should make frequent use of it, especially if it is uncertain whether the directory has already been created or the folder path has been specified by user input.<br \/>\n\n<div class=\"aawp\">\n\n            \n            \n<div class=\"aawp-product aawp-product--horizontal aawp-product--bestseller aawp-product--ribbon\"  data-aawp-product-asin=\"186742035X\" data-aawp-product-id=\"110751\" data-aawp-tracking-id=\"ekiwide0b-21\" data-aawp-product-title=\"Visual Basic For Applications A Complete Guide\">\n\n    <span class=\"aawp-product__ribbon aawp-product__ribbon--bestseller\">Bestseller No. 1<\/span>\n    <div class=\"aawp-product__thumb\">\n        <a class=\"aawp-product__image-link\"\n           href=\"https:\/\/www.amazon.de\/dp\/186742035X?tag=ekiwide0b-21&linkCode=ogi&th=1&psc=1&keywords=visual%20basic%20for%20application\" title=\"Visual Basic For Applications A Complete Guide\" rel=\"nofollow noopener sponsored\" target=\"_blank\">\n            <img decoding=\"async\" class=\"aawp-product__image\" src=\"https:\/\/m.media-amazon.com\/images\/I\/21qW8joYt2L._SL160_.jpg\" alt=\"Visual Basic For Applications A Complete Guide\"  \/>\n        <\/a>\n\n            <\/div>\n\n    <div class=\"aawp-product__content\">\n        <a class=\"aawp-product__title\" href=\"https:\/\/www.amazon.de\/dp\/186742035X?tag=ekiwide0b-21&linkCode=ogi&th=1&psc=1&keywords=visual%20basic%20for%20application\" title=\"Visual Basic For Applications A Complete Guide\" rel=\"nofollow noopener sponsored\" target=\"_blank\">\n            Visual Basic For Applications A Complete Guide        <\/a>\n        <div class=\"aawp-product__description\">\n                    <\/div>\n    <\/div>\n\n    <div class=\"aawp-product__footer\">\n\n        <div class=\"aawp-product__pricing\">\n            \n                            <span class=\"aawp-product__price aawp-product__price--current\">93,06 EUR<\/span>\n            \n            <a href=\"https:\/\/www.amazon.de\/gp\/prime\/?tag=ekiwide0b-21\" title=\"Amazon Prime\" rel=\"nofollow noopener sponsored\" target=\"_blank\" class=\"aawp-check-prime\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/ekiwi-blog.de\/wp-content\/plugins\/aawp\/assets\/img\/icon-check-prime.svg\" height=\"16\" width=\"55\" alt=\"Amazon Prime\" \/><\/a>        <\/div>\n\n                <a class=\"aawp-button aawp-button--buy aawp-button--icon aawp-button--icon-black\" href=\"https:\/\/www.amazon.de\/dp\/186742035X?tag=ekiwide0b-21&#038;linkCode=ogi&#038;th=1&#038;psc=1&#038;keywords=visual%20basic%20for%20application\" title=\"Buy on Amazon\" target=\"_blank\" rel=\"nofollow noopener sponsored\">Buy on Amazon<\/a>\n            <\/div>\n\n<\/div>\n\n            \n            \n<div class=\"aawp-product aawp-product--horizontal aawp-product--bestseller aawp-product--ribbon\"  data-aawp-product-asin=\"B0CWLL23VC\" data-aawp-product-id=\"110752\" data-aawp-tracking-id=\"ekiwide0b-21\" data-aawp-product-title=\"Visual Basic Programming Real World Code &amp; Explanations For Beginners Visual Basic Reference Visual Basic for Application  2 Books In 1 Visual Basic Book Learn Visual Basic.Net VB Compiler\">\n\n    <span class=\"aawp-product__ribbon aawp-product__ribbon--bestseller\">Bestseller No. 2<\/span>\n    <div class=\"aawp-product__thumb\">\n        <a class=\"aawp-product__image-link\"\n           href=\"https:\/\/www.amazon.de\/dp\/B0CWLL23VC?tag=ekiwide0b-21&linkCode=ogi&th=1&psc=1&keywords=visual%20basic%20for%20application\" title=\"Visual Basic Programming, Real World Code...\" rel=\"nofollow noopener sponsored\" target=\"_blank\">\n            <img decoding=\"async\" class=\"aawp-product__image\" src=\"https:\/\/m.media-amazon.com\/images\/I\/41g5n+-AwiL._SL160_.jpg\" alt=\"Visual Basic Programming, Real World Code...\"  \/>\n        <\/a>\n\n            <\/div>\n\n    <div class=\"aawp-product__content\">\n        <a class=\"aawp-product__title\" href=\"https:\/\/www.amazon.de\/dp\/B0CWLL23VC?tag=ekiwide0b-21&linkCode=ogi&th=1&psc=1&keywords=visual%20basic%20for%20application\" title=\"Visual Basic Programming, Real World Code...\" rel=\"nofollow noopener sponsored\" target=\"_blank\">\n            Visual Basic Programming, Real World Code...        <\/a>\n        <div class=\"aawp-product__description\">\n                    <\/div>\n    <\/div>\n\n    <div class=\"aawp-product__footer\">\n\n        <div class=\"aawp-product__pricing\">\n            \n                            <span class=\"aawp-product__price aawp-product__price--current\">18,26 EUR<\/span>\n            \n            <a href=\"https:\/\/www.amazon.de\/gp\/prime\/?tag=ekiwide0b-21\" title=\"Amazon Prime\" rel=\"nofollow noopener sponsored\" target=\"_blank\" class=\"aawp-check-prime\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/ekiwi-blog.de\/wp-content\/plugins\/aawp\/assets\/img\/icon-check-prime.svg\" height=\"16\" width=\"55\" alt=\"Amazon Prime\" \/><\/a>        <\/div>\n\n                <a class=\"aawp-button aawp-button--buy aawp-button--icon aawp-button--icon-black\" href=\"https:\/\/www.amazon.de\/dp\/B0CWLL23VC?tag=ekiwide0b-21&#038;linkCode=ogi&#038;th=1&#038;psc=1&#038;keywords=visual%20basic%20for%20application\" title=\"Buy on Amazon\" target=\"_blank\" rel=\"nofollow noopener sponsored\">Buy on Amazon<\/a>\n            <\/div>\n\n<\/div>\n\n    \n<\/div>\n<\/p>","protected":false},"excerpt":{"rendered":"<p>VBA is very well suited for getting rid of certain annoying tasks under Microsoft Office and limiting one&#8217;s productivity to<\/p>\n","protected":false},"author":2,"featured_media":6532,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1555],"tags":[1701,1855,1858,1569,1700,1861,1862,1710],"class_list":["post-54089","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software-en","tag-access-en","tag-excel-en-2","tag-microsoft-office-en-2","tag-outlook-en","tag-programming","tag-vba-en-2","tag-visual-basic-for-application-en-2","tag-word-en"],"_links":{"self":[{"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/posts\/54089","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/comments?post=54089"}],"version-history":[{"count":0,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/posts\/54089\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/media\/6532"}],"wp:attachment":[{"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/media?parent=54089"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/categories?post=54089"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/tags?post=54089"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}