Create Folder with VBA

  • Thread starter Thread starter Tommy
  • Start date Start date
T

Tommy

Can anybody advise me how to determine whether a folder is exist and create
that folder if it is not available.

Thank you so much!!
 
If it's just a single level, you could just ignore any error when you try to
create the folder:

On error resume next
MkDir "C:\myfolder"
 
Sub CornFlakes()
Dim FSOobj As Object
Set FSOobj = CreateObject("Scripting.FilesystemObject")

If FSOobj.FolderExists("C:\DsoFile\new folder") = False Then
FSOobj.CreateFolder "C:\DsoFile\new folder"
Else
MsgBox "Folder Exists"
End If

Set FSOobj = Nothing
End Sub
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware
(Excel Add-ins / Excel Programming)



"Tommy"
wrote in message
Can anybody advise me how to determine whether a folder is exist and create
that folder if it is not available.
Thank you so much!!
 
Here is a generic routine to create top-down

Function CreateDirs(ByVal Path As String)
Dim mpDirs As Variant
Dim mpPart As String
Dim i As Long
mpDirs = Split(Path, Application.PathSeparator)

mpPart = mpDirs(LBound(mpDirs))
For i = LBound(mpDirs) + 1 To UBound(mpDirs)

mpPart = mpPart & Application.PathSeparator & mpDirs(i)
On Error Resume Next
MkDir mpPart
On Error GoTo 0
Next i
End Function


--
---
HTH

Bob


(there's no email, no snail mail, but somewhere should be gmail in my addy)
 
Here's something that Jim Rech Posted:

Option Explicit
Declare Function MakePath Lib "imagehlp.dll" Alias _
"MakeSureDirectoryPathExists" (ByVal lpPath As String) As Long

Sub Test()
MakeDir "c:\aaa\bbb"
End Sub

Sub MakeDir(DirPath As String)
If Right(DirPath, 1) <> "\" Then DirPath = DirPath & "\"
MakePath DirPath
End Sub
 
Back
Top