Since function deleteFolder
uses some non-built VBA functions, they also must be included in your code for the function to work properly.
Otherwise the following error will occur: Compile error: Sub or Function not defined.
Required functions are listed below. You can get to each function's source code by clicking its name:
When adding the functions above to your VBA project, make sure you haven't done it before. If there are two different public functions with the same name in a single VBA project, the following compilation error is thrown: Compile error: Ambiguous name detected: function_name.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
'**********************************************************************************************************
' Name: deleteFolder
' Author: mielk | 2014-08-16
'
' Comment: Function to delete the specified folder from the file system.
'
' Parameters:
' filepath The path of a folder to be deleted.
'
' Returns:
' Boolean True - if the given folder has been deleted or the given folder doesn't exist.
' False - if it was impossible to delete the given folder (i.e. it is being used
' at this moment).
'
'
' --- Changes log -----------------------------------------------------------------------------------------
' 2014-08-16 mielk Function created.
'**********************************************************************************************************
Public Function deleteFolder(folderPath As String) As Boolean
Const METHOD_NAME As String = "deleteFolder"
Const ERR_NUM_PATH_NOT_FOUND As Long = 76
'------------------------------------------------------------------------------------------------------
Static objFSO As Object '(Late binding that allows to use the function, even if
'Microsoft Scripting Runtime library is not loaded)
Dim path As String
'------------------------------------------------------------------------------------------------------
'Create FileSystemObject instance if it hasn't been created yet. ------------------------------------|
If objFSO Is Nothing Then '|
Set objFSO = VBA.CreateObject("Scripting.FileSystemObject") '|
End If '|
'----------------------------------------------------------------------------------------------------|
'Convert the given folder path to format without the last slash.
path = VBA.IIf(VBA.right$(folderPath, 1) = "\", _
cutChars(folderPath, 1), folderPath)
'Try to remove the given file. If it is not possible code moves to the DeleteFileException label. ---|
On Error GoTo DeleteFolderException '|
Call objFSO.deleteFolder(path) '|
deleteFolder = True '|
'----------------------------------------------------------------------------------------------------|
'==========================================================================================================
ExitPoint:
Exit Function
'----------------------------------------------------------------------------------------------------------
DeleteFolderException:
If VBA.Err.number = ERR_NUM_PATH_NOT_FOUND Then
'Foldercannot be deleted because it doesn't exist. 'In this case True should be returned.
deleteFolder = True
Else
'Folder cannot be deleted for any other reason.
deleteFolder = False
End If
End Function