VBA Code not working on a shared excel file

59 Views Asked by At

I have a code shown below. Code works fine however when a share the file getting a appliaction-defined or object-defined error. Can somebody please help me with this?

Function SearchForFile(ByVal searchText As String, ByVal directory As String) As String
    Dim fs As Object
    Dim folder As Object
    Dim subFolder As Object
    Dim file As Object
    
    ' Replace the local file system object with a network mapping or cloud storage API
    Set fs = CreateObject("Scripting.FileSystemObject")
    ' Replace the local directory path with the network share or cloud storage path
    Set folder = fs.GetFolder(directory)
    
    ' Check files in the current directory
    For Each file In folder.Files
        If InStr(1, file.Name, searchText, vbTextCompare) > 0 Then
            ' Return the path of the first matching file
            SearchForFile = file.Path
            Exit Function
        End If
    Next file
    
    ' Recursively search subdirectories
    For Each subFolder In folder.Subfolders
        SearchForFile = SearchForFile(searchText, subFolder.Path)
        If SearchForFile <> "" Then Exit Function
    Next subFolder
End Function

Thank you all in advance.

0

There are 0 best solutions below