Is textfile writeable


Since function isTextfileWriteable 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
'**********************************************************************************************************
' Name:                 isTextfileWriteable
' Author:               mielk | 2012-12-02
'
' Comment:              Function to check if it is possible to write data into the specified textfile.
'
' Parameters:
'   filepath            The path of a file to be checked.
'
' Returns:
'   Boolean             True - if it is possible to write to a textfile with the given path.
'                       False - if the folder of the specified textfile doesn't exist or the user has
'                               not read-write access to it.
'
'
' --- Changes log -----------------------------------------------------------------------------------------
' 2012-12-02        mielk           Function created.
'**********************************************************************************************************
Public Function isTextfileWriteable(filepath As String) As Boolean
    Const METHOD_NAME As String = "isTextfileWriteable"
    'Define extensions of text files.
    Dim TEXT_EXTENSIONS As Variant:         TEXT_EXTENSIONS = Array("txt", "csv")
    '------------------------------------------------------------------------------------------------------
    Static objFSO As Object                 '(Late binding that allows to use the function, even if
                                            'Microsoft Scripting Runtime library is not loaded)
    Dim strDriveName As String
    Dim strBaseFolder As String
    Dim strParentFolder As String
    Dim strExtension 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                                                                                              '|
    '----------------------------------------------------------------------------------------------------|



    With objFSO

        'Check if the given textfile exists and if it is read-only. -------------------------------------|
        If .fileExists(filepath) Then                                                                   '|
                                                                                                        '|
            isTextfileWriteable = Not (VBA.GetAttr(filepath) And VBA.vbReadOnly)                        '|
                                                                                                        '|
        Else                                                                                            '|
                                                                                                        '|
            'Check if the drive from the given filepath exists. -------------------------------------|  '|
            strDriveName = .GetDriveName(filepath)                                                  '|  '|
            If .DriveExists(strDriveName) Then                                                      '|  '|
                                                                                                    '|  '|
                'Checks if the extension in the specified filepath is proper for a textfile. ----|  '|  '|
                strExtension = .GetExtensionName(filepath)                                      '|  '|  '|
                If isInArray(strExtension, TEXT_EXTENSIONS, False) Then                         '|  '|  '|
                                                                                                '|  '|  '|
                    'Retrieve the base folder name and check if it is allowed to add --------|  '|  '|  '|
                    'subfolders to this folder.                                             '|  '|  '|  '|
                    strParentFolder = .GetParentFolderName(filepath)                        '|  '|  '|  '|
                    strBaseFolder = .GetParentFolderName(filepath)                          '|  '|  '|  '|
                    Do Until .FolderExists(strBaseFolder)                                   '|  '|  '|  '|
                        strBaseFolder = .GetParentFolderName(strBaseFolder)                 '|  '|  '|  '|
                    Loop                                                                    '|  '|  '|  '|
                    '------------------------------------------------------------------------|  '|  '|  '|
                                                                                                '|  '|  '|
                                                                                                '|  '|  '|
                    '------------------------------------------------------------------------|  '|  '|  '|
                    If VBA.StrComp(strBaseFolder, strParentFolder, vbTextCompare) = 0 Then  '|  '|  '|  '|
                        'Destination folder already exists.                                 '|  '|  '|  '|
                        isTextfileWriteable = isFolderWriteable(strParentFolder)            '|  '|  '|  '|
                    Else                                                                    '|  '|  '|  '|
                        'Destination folder is yet to be created.                           '|  '|  '|  '|
                        isTextfileWriteable = isSubfoldersAddingAllowed(strBaseFolder)      '|  '|  '|  '|
                    End If                                                                  '|  '|  '|  '|
                    '------------------------------------------------------------------------|  '|  '|  '|
                                                                                                '|  '|  '|
                                                                                                '|  '|  '|
                Else                                                                            '|  '|  '|
                                                                                                '|  '|  '|
                    'The extension in the specified filepath is improper.                       '|  '|  '|
                    isTextfileWriteable = False                                                 '|  '|  '|
                                                                                                '|  '|  '|
                End If                                                                          '|  '|  '|
                '---- [If .FileExists(filepath) Then] -------------------------------------------|  '|  '|
                                                                                                    '|  '|
            Else                                                                                    '|  '|
                                                                                                    '|  '|
                'Drive retrieved from the given filepath doesn't exist.                             '|  '|
                isTextfileWriteable = False                                                         '|  '|
                                                                                                    '|  '|
            End If                                                                                  '|  '|
            '-------- [If .FileExists(filepath) Then] -----------------------------------------------|  '|
                                                                                                        '|
        End If                                                                                          '|
        '------------ [If .FileExists(filepath) Then] ---------------------------------------------------|

    End With


End Function