Check for directory then create

R

Rex Mundi

I have figured out how to have Excel create a directory and save a CSV
of the current workbook.

Now the macro hangs if the directory already exists. How do I check
for the directory and create only if needed.

TIA
 
D

Dan R.

Try this function:

Function DirExists(PathName As String) As Boolean
Dim temp As Integer
On Error Resume Next
temp = GetAttr(PathName)
If Err.Number = 0 Then
DirExists = True
Else
DirExists = False
End If
On Error GoTo 0
End Function
 
J

Jim Rech

If Dir("c:\aa\nul") = "" Then MkDir "c:\aa"

--
Jim
|I have figured out how to have Excel create a directory and save a CSV
| of the current workbook.
|
| Now the macro hangs if the directory already exists. How do I check
| for the directory and create only if needed.
|
| TIA
 
S

sebastienm

Hi,
- Add a reference to the Microsoft Scripting Runtime library (menu Tools >
Reference).
- use someth8ing like:

Sub test()
Dim fso As Scripting.FileSystemObject
Dim path As String

path = "c:\test"
Set fso = New Scripting.FileSystemObject

If Not fso.FolderExists(path) Then 'does NOT exist, then create
''' create directory here
End If

''' now your FIleSave code
End Sub
 
B

Bob Phillips

Just create it, don't bother checking

On Error Resume Next
MkDir "C:\test\subdir"
On Error Goto 0

--
---
HTH

Bob


(there's no email, no snail mail, but somewhere should be gmail in my addy)
 

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

Top