Robert F. Lamping
Robert F. Lamping

Reputation: 39

DirExist for OneDrive synchronized Sharepoint directories

Nowadays you can work on synchronized sharepoint content via OneDrive even when you are not linked to your company network via e.g. VPN, but logged in to Onedrive via your company account. Strange thing is that the DIR function in that disconnected situation returns an empty value for a directory that certainly exists. I see solutions trying to create mapped drives, but it can be done simpler. I'll answer this question myself.

Upvotes: 0

Views: 716

Answers (2)

Kostas K.
Kostas K.

Reputation: 8518

Robert, this is exactly your code but a bit more dense.

The With statement takes care of the object reference and ensures its destroyed when it reaches the End With block.

Public Function DirExists(ByVal path As String) As Boolean
    On Error Resume Next
    
    With CreateObject("Scripting.FileSystemObject")
        DirExists = Not .GetFolder(path) Is Nothing
    End With
    
    On Error GoTo 0
End Function

This is nothing more than a different approach on how to write the particular function. There's absolutely nothing wrong with your example.

Upvotes: 3

Robert F. Lamping
Robert F. Lamping

Reputation: 39

This is my solution:

Function MyDirExists(ByVal myPath As String) As Boolean

    'Dir() doesn't work on directories in synchronized sharepoint 
    'that appear in your OneDrive folders
    'Let's use the FileSystem instead. 
    'Use Late binding as not everyone has the library FileSystemObject included

    Dim objFSO As Object    'Late binding
    Dim objfolder As Object 'Late binding

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    On Error Resume Next
    'if directory does not exist GetFolder raises an error
    'and we happily use that fact
    
        Set objfolder = objFSO.GetFolder(myPath)
        
        If Not objfolder Is Nothing Then
            MyDirExists = True  'Default return value is False
        End If
        
    On Error GoTo 0
    
    'Clean up objects
    Set objFSO = Nothing
    Set objfolder = Nothing
    
End Function

Upvotes: 1

Related Questions