Get desktop path


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
'**********************************************************************************************************
' Name:                 getDesktopPath
' Author:               mielk | 2014-12-11
'
' Comment:              Function returns the path to the Desktop folder.
'
' Returns:
'   String              The full access path to the Desktop folder.
'
'
' Exceptions:
'   CreatingObjectException         Thrown if it was impossible to create an instance of WScript.Shell
'                                   object.
'
' --- Changes log -----------------------------------------------------------------------------------------
' 2014-12-11        mielk       Function created.
'**********************************************************************************************************
Public Function getDesktopPath() As String
    Const METHOD_NAME As String = "getDesktopPath"
    '------------------------------------------------------------------------------------------------------
    Const LIBRARY_NAME As String = "WScript.Shell"
    Const DESKTOP_FOLDER_ALIAS As String = "Desktop"
    '------------------------------------------------------------------------------------------------------
    Dim shell As Object
    '------------------------------------------------------------------------------------------------------


    'Create a new instance of WScript.Shell object ------------------------------------------------------|
    On Error GoTo CreatingObjectException                                                               '|
    Set shell = VBA.CreateObject(LIBRARY_NAME)                                                          '|
    On Error GoTo 0                                                                                     '|
    '----------------------------------------------------------------------------------------------------|


    'If shell object has been properly created, use it to obtain Desktop folder path. -------------------|
    If Not shell Is Nothing Then                                                                        '|
        getDesktopPath = shell.SpecialFolders(DESKTOP_FOLDER_ALIAS)                                     '|
    End If                                                                                              '|
    '----------------------------------------------------------------------------------------------------|



'==========================================================================================================
ExitPoint:
    Set shell = Nothing
    Exit Function


'----------------------------------------------------------------------------------------------------------
CreatingObjectException:
    'Error handler for the case if it was impossible to create an instance of Scriptlet.TypeLib object.
    GoTo ExitPoint


End Function