I have copied the below from another list, but have been unable to modify it so that it works.
What I am doing is from a form have a command button that opens a folder and then subfolder, based on name, etc. and if those folders don't exist to create them automatically and then open them.
In a module (MakeFolder)I put the following:
-----------------------------------------------------
Public Function MakeFolder(FolderPath As String)
Dim astrFolder As Variant
Dim intFolder As Integer
'make sure Folderpath does not end with \
If Right$(FolderPath, 1) = "\" Then
FolderPath = Left$(FolderPath, Len(FolderPath) - 1)
End If
'make sure folder path exists
If Len(Dir(FolderPath, vbDirectory)) = 0 Then
astrFolder = Split(FolderPath, "\")
FolderPath = astrFolder(0)
For intFolder = 1 To UBound(astrFolder)
FolderPath = FolderPath & "\" & astrFolder(intFolder)
If Len(Dir(FolderPath, vbDirectory)) = 0 Then
MkDir FolderPath
End If
Next
FolderPath = FolderPath & "\"
End If
End Function
----------------------------------------------------
Then on a command button I have:
Dim astrFolder As Variant
Dim intFolder As Integer
Dim cSource As String
cSource = "C:\PetFolder" & Me.LastName & ", " & Me.FirstName & " " & Me.PetID & " " & Me.DOB
'see if pets folder exists
If Len(Dir(cSource, vbDirectory)) = 0 Then
if not then build complete folder path as necessary
Call MakeFolder(cSource)
'if it did not exist then we need to make their subfolders also
Call MakeFolder(cSource & "\XRay")
Call MakeFolder(cSource & "\Lab")
Call MakeFolder(cSource & "\Food")
End If
Shell "explorer.exe" & cSource, vbMaximizedFocus
Exit Sub
End Sub
----------------------------------------------
But when I try to run this, it brings up an error message "expected variable or proceedure, not module"
I am not sure how to get this to work. I have not done a lot with modules.
Appreciate input.
thanks
What I am doing is from a form have a command button that opens a folder and then subfolder, based on name, etc. and if those folders don't exist to create them automatically and then open them.
In a module (MakeFolder)I put the following:
-----------------------------------------------------
Public Function MakeFolder(FolderPath As String)
Dim astrFolder As Variant
Dim intFolder As Integer
'make sure Folderpath does not end with \
If Right$(FolderPath, 1) = "\" Then
FolderPath = Left$(FolderPath, Len(FolderPath) - 1)
End If
'make sure folder path exists
If Len(Dir(FolderPath, vbDirectory)) = 0 Then
astrFolder = Split(FolderPath, "\")
FolderPath = astrFolder(0)
For intFolder = 1 To UBound(astrFolder)
FolderPath = FolderPath & "\" & astrFolder(intFolder)
If Len(Dir(FolderPath, vbDirectory)) = 0 Then
MkDir FolderPath
End If
Next
FolderPath = FolderPath & "\"
End If
End Function
----------------------------------------------------
Then on a command button I have:
Dim astrFolder As Variant
Dim intFolder As Integer
Dim cSource As String
cSource = "C:\PetFolder" & Me.LastName & ", " & Me.FirstName & " " & Me.PetID & " " & Me.DOB
'see if pets folder exists
If Len(Dir(cSource, vbDirectory)) = 0 Then
if not then build complete folder path as necessary
Call MakeFolder(cSource)
'if it did not exist then we need to make their subfolders also
Call MakeFolder(cSource & "\XRay")
Call MakeFolder(cSource & "\Lab")
Call MakeFolder(cSource & "\Food")
End If
Shell "explorer.exe" & cSource, vbMaximizedFocus
Exit Sub
End Sub
----------------------------------------------
But when I try to run this, it brings up an error message "expected variable or proceedure, not module"
I am not sure how to get this to work. I have not done a lot with modules.
Appreciate input.
thanks