List All SharePoint Files In Folder

sspatriots

Well-known Member
Joined
Nov 22, 2011
Messages
585
Office Version
  1. 365
Platform
  1. Windows
I have this code below that works to a degree. That is, I can see the correct "https" file paths in the Immediate window using the "Debug.Print" command, however, I need to get these paths to be what shows up on my worksheet. The file paths on the worksheet are the ones to my local users folder. I tried using the line that is near the bottom that is commented out, but that just gives me the "Run-time error '450': Wrong number of arguments or invalid property assignment" error

VBA Code:
Sub ListAllFilesInFolder()
    Dim oFSO As Object
    Dim oFolder As Object
    Dim oFile As Object
    Dim i As Long
    Dim ws As Worksheet
    Dim userName As String
    
    Dim spacePosition As Long
    spacePosition = InStr(Application.userName, " ")
    
    Length = Len(Application.userName)
    
    userName = Left(Application.userName, 1) & Right(Application.userName, (Length - spacePosition))
    
    Set ws = ActiveSheet
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oFolder = oFSO.GetFolder("C:\Users\" & userName & "\DEM\DEM - Documents\Purchasing\PO Archive\2024") ' Replace with your desired folder path
    
    Dim oFile2 As String
    
    For Each oFile In oFolder.Files
        ws.Cells(i + 2, 1).Value = oFile.Path
        i = i + 1
        
        Dim spacePosition2 As Long
        spacePosition2 = InStr(oFile, "\2024\")
        
        Length2 = Len(oFile)
        
        oFile2 = "https://DEM.sharepoint.com/sites/DEM614/Shared Documents/Purchasing/PO Archive/2024/" & Right(oFile, (Length2 - spacePosition2 - 5))
        
        Debug.Print oFile2 'The file paths in the Immediate window are correct
        
    '    oFile = oFile2
        
    Next oFile
    
End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Forgot to mention, I don't care for the part that has "\2024\" in the string search. I couldn't figure a way to do that with wildcards. The years are from 2019 to 2024. Thanks, SS
 
Upvote 0
I've rearranged the original code a little and now it puts the correct file paths on the workbook. However, I cannot get them to become an active hyperlink unless I click in each cell and then select "Enter" on my my keyboard. I still have the issue of not knowing how to use a wildcard for that string search in my previous post as well.

VBA Code:
Sub ListAllFilesInFolder()
    Dim oFSO As Object
    Dim oFolder As Object
    Dim oFile As Object
    Dim i As Long
    Dim ws As Worksheet
    Dim userName As String
    
    Dim spacePosition As Long
    Dim spacePosition2 As Long

    spacePosition = InStr(Application.userName, " ")
    
    Length = Len(Application.userName)
    
    userName = Left(Application.userName, 1) & Right(Application.userName, (Length - spacePosition))
    
    Set ws = ActiveSheet
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oFolder = oFSO.GetFolder("C:\Users\" & userName & "\DEM\DEM - Documents\Purchasing\PO Archive\2024") ' Replace with your desired folder path
    
    Dim oFile2 As String
    
    For Each oFile In oFolder.Files
        
        spacePosition2 = InStr(oFile, "\2024\")
        
        Length2 = Len(oFile)
        
        oFile2 = "https://DEM.sharepoint.com/sites/DEM614/Shared Documents/Purchasing/PO Archive/2024/" & Right(oFile, (Length2 - spacePosition2 - 5))
        
        Debug.Print oFile2 'The file paths in the Immediate window are correct
        
        ws.Cells(i + 2, 1).Value = oFile2
        i = i + 1
        
    Next oFile
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,879
Messages
6,175,147
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top