Jeff,
I went to Google. You should find this post by Stephen Bullen to be very
helpful.
http://groups.google.com/groups?q=e...e=UTF-8&selm=VA.0000043d.02cd6784@mars&rnum=1
I will save you the trouble and simply paste it here.
Best regards,
Kevin
Hi Rajko,
I have a problem if I want to write a file to a folder which not exist.
How can I check if folder exists, then create it and write a file.
There are actually four outcomes to consider:
1. The folder exists - we're OK
2. The folder doesn't exist and there is no file of the same name - we
can create the folder and be OK
3. The folder doesn't exist and there IS a file of the same name - we
can't create the folder
4. A disk error ocurred during 1-3.
I might be tempted <g> to write a routine for it like:
Sub Test()
MsgBox CheckFolder("C:\Test")
End Sub
Function CheckFolder(sPath As String) As Boolean
On Error GoTo ERR_DISKERROR
'Dir checks files as well as directories
If Dir(sPath, vbDirectory) <> "" Then
'Is it a directorythat we found, or a file?
If GetAttr(sPath) And vbDirectory Then
'It's a directory, so we're OK
CheckFolder = True
Else
'What to do if you can't create the directory
End If
Else
'The directory doesn't exist, nor does a file of the same name
MkDir sPath
CheckFolder = True
End If
Exit Function
ERR_DISKERROR:
'Handle the error how you like, and terminate the function
'examples are:
' Drive doesn't exist/not accessible (e.g. A:/Test without a disk in)
' Invalid path string (e.g. contains invalid characters)
' Disk full when creating folder
' Disk not writable
' Path too long
' etc.
End Function
Regards
Stephen Bullen
Microsoft MVP - Excel
http://www.BMSLtd.co.uk