Check for directory then create

  • Thread starter Thread starter Rex Mundi
  • Start date Start date
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
 
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
 
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
 
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
 
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)
 
Back
Top