View Single Post
  #10   Report Post  
Posted to microsoft.public.excel.programming
Patrick Molloy Patrick Molloy is offline
external usenet poster
 
Posts: 1,049
Default Making a directory if one doesn't exist

ine could use the FileSystemObject and a UDF to show on the worksheet
whether its a good folder or not.

In the Developer environment, under the Tools menu, select References
scroll down until you see Microsoft Scripting Runtime....check the box and
click OK

then in a code module, add the UDF:

Function FolderExists(sText As String) As Boolean
With New FileSystemObject
FolderExists = .FolderExists(sText)
End WithEnd Function
End Function


in a scpreadsheet, type some file path in any cell, say B5, then in the
adjascent cell
=FolderExists(B5)
it will show TRUE or False appropriately



"bigjim" wrote in message
...
I am using excel 2003. The user enters a path name where he wants to save
a
file. I need this code to check to see if the directory they enter exists
and if not I want to make the directory for them. This is the code I'm
using, but I get the error path not found when the program tries to save
it.

Worksheets("ticket").Activate

strappend = ActiveSheet.Range("j8").Value
strpath = ActiveSheet.Range("b200").Value
str3 = ActiveSheet.Range("c8").Value



MsgBox strpath

Rem making directory if it doesn't exist
If Dir(strpath, vbDirectory) = "" Then MkDir strpath

fsavename = strpath & strappend & str3 & ".xls"

ThisWorkbook.SaveAs Filename:=fsavename

I am using the msgbox to make sure the path entered is getting assigned to
strpath and it is. J8 and C8 are the file name. In his case strpath =
c:\2008\Jun\ J8 = C005482 and C8 = Encana. I checked and the folder
c:\2008\jun\ was not created.

Any help would be appreciated.

Jim