> ## Content Index
> Fetch the complete content index at: https://nolongerset.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# MakeSurePathExists: Using the Windows API to Create Missing Subfolders in VBA
- URL: https://nolongerset.com/makesurepathexists/
- Published: 2022-08-13T03:59:00.000Z
- Updated: 2026-05-08T12:55:33.000Z
- Description: A Windows API function makes verifying (and creating, if necessary) a full folder hierarchy dead simple.
- Author: Mike Wolfe
- Tags: Code Library, #FileFunctions, #Import 2026-05-20 02:59

*This is part 2 of 3 of a [series of articles](https://nolongerset.com/ways-to-create-subfolders/) on creating folder trees in VBA.*

---

I often find the need to verify that an entire subfolder structure exists:

> Oftentimes, I'm creating multiple levels of subfolders at a time. For example, I have a folder mapped as `G:\Photos` and I need to create the following folder hierarchy, `G:\Photos\312\564312\`.

The above quote is from my [VerifyFolder() article](https://nolongerset.com/recursion-demystified-creating-subfolders/). With that function, I used recursion and the FileSystemObject to manually create an arbitrary number of subfolders.

As it turns out, I was reinventing the wheel.

## MakeSureDirectoryPathExists API Function

The Windows API includes a function named **[MakeSureDirectoryPathExists](https://docs.microsoft.com/en-us/windows/win32/api/dbghelp/nf-dbghelp-makesuredirectorypathexists)**.

> Creates all the directories in the specified path, beginning with the root.  
>  
> Each directory specified is created, if it does not already exist. If only some of the directories are created, the function will return **FALSE**.

Implementing this API is dead simple. In fact, I don't think we even need to bother with a wrapper function. Let's just alias the function name to something a bit shorter, make sure we include `PtrSafe` for 32/64-bit compatibility, and declare it as Public so we only have to declare it once within our project:

```vba
'Source: https://nolongerset.com/makesurepathexists/
Public Declare PtrSafe Function MakeSurePathExists _
    Lib "Imagehlp" Alias "MakeSureDirectoryPathExists" _
    (ByVal DirPath As String) As Boolean
```

## Usage

Just pass it the folder you want it to verify exists. To be on the safe side, you should check the return value to make sure it worked. In practice, I often use this as part of a guard clause:

```vba
Sub MyRoutine(MyFolder As String)
    If Not MakeSurePathExists(MyFolder) Then Exit Sub
    
    'Do stuff with MyFolder
    '...
    '...
End Sub
```

Here's what it looks like running in the Immediate Window:

![](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2022/08/image-19.png)

## Caveats

### Trailing Backslash Required

The function will only create a subfolder if there is a trailing backslash. For instance, in the first example below, the subfolder `\ABC\` would be created but not subfolder `\123\`:

```vba
MakeSurePathExists "C:\ABC\123"   'Creates folder: C:\ABC\

MakeSurePathExists "C:\ABC\123\"  'Creates folders: C:\ABC\123\
```

### Lacks Unicode Support

This function does not support Unicode characters in the path name. 

> This function does not support Unicode strings. To specify a Unicode path, use the [SHCreateDirectoryEx](https://docs.microsoft.com/en-us/windows/desktop/api/shlobj%5Fcore/nf-shlobj%5Fcore-shcreatedirectoryexa) function.

I will implement a Unicode-safe alternative function based on the SHCreateDirectoryEx function in a future article.

*Special thanks to Domenico Cozzolino for introducing me to this API function in the [comments section](https://nolongerset.com/gettemppath/#commento-comment-card-c04e0e32908d502d3b8274520c9855b24522ddacedf047cea967760d2d975a0c) of a different article.*

---

### External references

[MakeSureDirectoryPathExists function (dbghelp.h) - Win32 appsCreates all the directories in the specified path, beginning with the root.Microsoft Docskarl-bridge-microsoft![](https://docs.microsoft.com/en-us/media/logos/logo-ms-social.png)](https://docs.microsoft.com/en-us/windows/win32/api/dbghelp/nf-dbghelp-makesuredirectorypathexists)

### Referenced articles

[Recursion Demystified: Creating SubfoldersRecursion: it’s not just for calculating factorials any more! A practical example of using recursion to create multiple missing subfolders.![](https://nolongerset.com/favicon.png)No Longer SetMike Wolfe![](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2020/11/mirror-510976_1920.jpg)](https://nolongerset.com/recursion-demystified-creating-subfolders/)

*Image by [Joe](https://pixabay.com/users/jplenio-7645255/?utm%5Fsource=link-attribution&utm%5Fmedium=referral&utm%5Fcampaign=image&utm%5Fcontent=3224754) from [Pixabay](https://pixabay.com//?utm%5Fsource=link-attribution&utm%5Fmedium=referral&utm%5Fcampaign=image&utm%5Fcontent=3224754)*