Skip to main content

Folder.Add method

Table of contents
  1. Syntax
  2. Remarks
  3. Example

Adds a new Folder to a Folders collection.

Syntax

object.Add folderName

The Add method has the following parts:

Part Description
object Required. Always the name of a Folders collection.
folderName Required. The name of the new Folder being added.

Remarks

An error occurs if the folderName already exists.

Example

The following example illustrates how to use AddFolders method to add a new folder:

Sub AddNewFolder(path As String, folderName As String)
    Dim fso, f, fc, nf
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.GetFolder(path)
    Set sf = f.SubFolders
    If folderName <> "" Then
        Set nf = sf.Add(folderName)
    Else
        Set nf = sf.Add("New Folder")
    End If
End Sub

Run the following code in the Immediate Window to test this macro:

AddNewFolder "D:\TestFolder", "ArtWork"

If the folder "ArtWork" does not exist, this macro will create it, otherwise an error will appear.

Leave a comment

Your email address will not be published. Required fields are marked *

Format your code: <pre><code class="language-vba">place your code here</code></pre>