check and create folder for saveas

  • Thread starter Thread starter Sunil
  • Start date Start date
S

Sunil

how do you chech if a folder exists and create one on A: drive is it is not
there ?
Please help a newbie- Thanks :-}
 
Hi Sunil,

One way

Sub Test()
Dim sFolder As String

sFolder = "C:\Mytest\New Dir"
If Not FolderExists(sFolder) Then
MkDir sFolder
End If

End Sub

'-----------------------------------------------------------------
Function FolderExists(Folder) As Boolean
'-----------------------------------------------------------------
Dim sFolder As String
On Error Resume Next
sFolder = Dir(Folder, vbDirectory)
If sFolder <> "" Then
If (GetAttr(sFolder) And vbDirectory) = vbDirectory Then
FolderExists = True
End If
End If
End Function
 
Another way is to just ignore the error if there's a folder already there.

on error resume next
mkdir "a:\myfolder"
on error goto 0

This assumes that you wouldn't have a filename that has that name.
 
If c:\mytest didn't exist, then I get an error when I try to make:
c:\mytest\new dir in one statement.
 
Just another idea...

s = "C:\Demo"
With CreateObject("Scripting.FileSystemObject")
If Not .FolderExists(s) Then .CreateFolder (s)
End With
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top