Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Just Create the Dang File!

Status
Not open for further replies.

SpaaamCatcher

Technical User
Aug 21, 2003
19
US
Hi, there. I want to programmatically create a folder path that has been defined in a particular cell in Excel, and it works, but not all the way.

If the user enters a value in cell A1 equal to: "C:\Folder1\Folder2", I want to check if this path exists and then create it if it doesn't. This works, but only if I'm only creating the last folder...

So if A1 = "C:\Folder1\Folder2" and Folder1 exists, the code will generate Folder2. HOWEVER, if Folder1 does NOT exist, I get a path not found error and nothing gets created. Whassup???

Help?

Jamie

See code below:

Private Sub DirectoryExists()
Dim Msg, Style, Title, Response
Dim strFolderName As String

strFolderName = Worksheets("Test1").Range("A1")

Set FSO = CreateObject("Scripting.FileSystemObject")
If Not FSO.FolderExists(strFolderName) Then
Msg = "This folder does not exist. Would you like to create it?"
Style = vbYesNoCancel
Title = "Create Folder?"
Response = MsgBox(Msg, Style, Title)
If Response <> vbYes Then End
Set CreatedFolder = FSO.CreateFolder(strFolderName)
End If

End Sub
 
I was so hopeful! But no luck.

I did a basic test: MkDir &quot;C:\Folder1&quot; and it worked.
But if I say MkDir &quot;C:\Folder1\Folder2&quot;, I get the same 'path not found' error.

Any ideas? This seems like it should be basic, but clearly I'm missing something...

jem
 
Try this. First create Folder1 if it doesn't exist and then create Folder2 (then Folder3 and so on).

Private Sub DirectoryExists()

Dim Msg, Style, Title, Response
Dim strFolders() As String
Dim strFolderName As String
Dim I As Integer, N As Integer

strFolders() = Split(Worksheets(&quot;Test1&quot;).Range(&quot;A1&quot;), &quot;\&quot;)
N = UBound(strFolders)

Set FSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)

If N < 1 Then
End
Else
strFolderName = strFolders(0)
I = 0
End If

MsgBox strFolderName & N

Do While I < N

I = I + 1
strFolderName = strFolderName & &quot;\&quot; & strFolders(I)
If Not FSO.FolderExists(strFolderName) Then
Msg = &quot;This folder does not exist: &quot; &
strFolderName & vbLf
& &quot;Would you like to create it?&quot;
Style = vbYesNoCancel
Title = &quot;Create Folder?&quot;
Response = MsgBox(Msg, Style, Title)
If Response <> vbYes Then End
Set CreatedFolder = FSO.CreateFolder(strFolderName)
'or MkDir strFolderName
End If

Loop

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top