Yes, VBA can read a SharePoint folder, but not the way it reads a local folder. The catch is that VBA's file functions only understand paths the Windows file system can resolve, so the folder must first be connected in File Explorer. If you have synced the SharePoint library with OneDrive, or you have mapped the site as a network drive and supplied your credentials, then Dir, FileSystemObject and Workbooks.Open all work normally. If the library is not connected, VBA cannot browse it by its https address and you need to either sync it or pull the file list through the SharePoint REST API from VBA. The steps below cover the synced or mapped approach because it is the one that works in every Office version, followed by the REST fallback for people who cannot sync.

What You'll Need

  • Excel, Access or Word with the VBA editor available (Alt+F11 opens it in the desktop apps).
  • Permission to read the SharePoint library, granted through your Microsoft 365 account.
  • The library synced with OneDrive for work or school, or mapped as a network location.
  • Excel set to trust the synced location, or the file will fail to open without any clear error.
  • For the REST route: the site's web address, a library name, and the network access your organisation allows.

Step-by-Step

  1. Connect the library to your PC. In a browser, open the SharePoint site, go to the document library, and use the Sync button in the command bar. OneDrive for work or school will pick it up and place a folder under your organisation's name in File Explorer. Alternatively, if your administrator allows older methods, right-click This PC and choose Map network drive, then paste the library's address. Button and menu wording differs slightly between Windows versions and tenant configurations, so look for the sync or connect option in the library's toolbar rather than an exact label.
  2. Copy the real local path. Open the synced folder in File Explorer, click the address bar, and copy the text. It looks like C:\Users\YourName\YourCompany\TeamSite - Documents\Reports. This is the path VBA needs. Never paste the https:// address into your code.
  3. Test the path before writing code. Press Alt+F11 to open the VBA editor, press Ctrl+G for the Immediate window, and type ? Dir("C:\Users\YourName\YourCompany\TeamSite - Documents\Reports\*.xlsx") then press Enter. If a filename comes back, the folder is readable. If you get nothing, stop here and fix the path or permissions first.
  4. Write a loop that lists the files. In a standard module, add a procedure that walks the folder. A reliable pattern is:
    Dim f As String
    f = Dir(folderPath & "*.xlsx")
    Do While Len(f) > 0
        Debug.Print f
        f = Dir
    Loop

    Run it with F5 and check the Immediate window. Adjust the wildcard to match the file type you actually store, for example *.csv or *.*.
  5. Be aware that synchronisation takes time. A file that appears on the SharePoint website is not instantly on disk. If you enable the option to keep items available offline, OneDrive downloads them; otherwise files may be placeholders until opened. For a quick check, confirm the file opens by double-clicking it in File Explorer before blaming the macro.
  6. Open files read-only by default. Use Workbooks.Open Filename:=fullPath, ReadOnly:=True while you are testing. SharePoint files are frequently locked by colleagues, and a read-only open avoids a confusing prompt or a failure. Close each workbook with Close SaveChanges:=False so you never write changes back by accident.
  7. Handle the sync folder as a moving target. Store the folder path in a cell or a named constant rather than hard-coding a username. Colleagues' profiles have different paths, and OneDrive can change the folder name when the library is renamed. A cell reference is easier to update than code.
  8. Optional: use the REST API when sync is not allowed. If your organisation blocks syncing, VBA can call the SharePoint REST interface with an XML HTTP request. The pattern is to build a URL such as https://yourtenant.sharepoint.com/sites/YourSite/_api/web/GetFolderByServerRelativeUrl('/sites/YourSite/Shared Documents/Reports')/Files, send it with MSXML2.XMLHTTP or WinHttp.WinHttpRequest, and parse the returned XML or JSON. You must supply credentials, typically by using the logged-on Windows identity, and the request will fail if the tenant requires modern authentication prompts that VBA cannot display. Treat this as an advanced route and test it against a small library first.
  9. Save the workbook as a macro-enabled file. Use .xlsm, because .xlsx strips the code and .xlsb only if you need the smaller binary size. Store the macro file somewhere in the synced folder if colleagues need it, but keep a local copy as well.
  10. Add basic error handling. Wrap the file operations in On Error GoTo Cleanup, report which file failed, and always close any workbook you opened in the cleanup block. A single locked file otherwise leaves orphaned workbooks open and the next run behaves strangely.

Common Mistakes

  • Using the browser URL as a path. Dir and FileSystemObject reject https addresses outright. Only mapped or synced local paths work.
  • Assuming the sync is instant. Code that runs immediately after someone uploads a file may see an empty folder. Add a short wait or trigger the macro manually.
  • Forgetting that spaces break nothing but quotes do. Folder names with spaces, such as "Shared Documents", are fine as long as the whole path sits inside quotes or a string variable.
  • Opening files with write access during testing. This creates check-out locks that block other people and leaves files modified.
  • Relying on a mapped drive letter. Colleagues may have the same library on a different letter, so the macro fails on their machine. The synced OneDrive path is more consistent, and a configurable path is better still.
  • Ignoring Trust Center settings. Files in an untrusted location open with protection that can block macros and links, often with a vague error.
  • Assuming the folder structure is fixed. Subfolders are not returned by a simple Dir loop; you need a recursive routine or FileSystemObject to walk them.

Tips

  • Use late binding for FileSystemObject (CreateObject("Scripting.FileSystemObject")) so the macro does not need a reference set on every PC.
  • Filter by file extension and by a naming convention such as a date prefix, so the loop ignores temp files starting with ~$.
  • Log the filenames and timestamps to a sheet instead of the Immediate window when you need to show the result to someone else.
  • For large libraries, collect paths into an array first and process them afterwards; that keeps the sync folder from being hammered while the loop runs.
  • If you need to run this unattended, the OneDrive sync client must be running and signed in on that machine. A server without the sync app cannot see the folder at all.

Troubleshooting

If the Immediate window returns nothing, first confirm the folder exists in File Explorer and that you copied the path from the address bar, not from a shortcut's target. Then check whether the file type matches your wildcard. Next, confirm your account still has access on the SharePoint site, because revoking permission removes the synced folder without a warning you would notice.

If the file appears in the list but Workbooks.Open fails, the file is probably checked out or locked. Ask the colleague to check it in, or open it read-only. If the error only happens on one PC, suspect the sync client: restart OneDrive from the taskbar, sign out and back in, and let it finish syncing before rerunning the macro.

If the REST request returns an authentication error, the tenant is likely requiring a sign-in method VBA cannot complete. In that case syncing is the practical answer, or ask IT whether an approved integration is available. If the request returns a permissions error, the account needs read access to that library on the site.

If macros are blocked, check Excel's Trust Center settings and add the synced folder as a trusted location, or open the file from a trusted path. A protected view banner means the file came from an external location; use Enable Editing rather than lowering security settings globally.

Frequently Asked Questions

Can VBA open a SharePoint file straight from its web address?

No. The built-in file functions need a local path. Sync the library or map it as a network location, then use the local path. The REST API is the only way to read directly by web address, and it needs credentials and extra code.

Does the OneDrive sync client have to be running?

Yes, for the synced-folder method. If it is closed or signed out, the folder disappears from disk and the macro sees nothing. On a shared or server machine, make sure the client starts automatically.

Will this work on Mac?

Partly. OneDrive for Mac syncs SharePoint libraries to a local folder, so the path approach can work, but the path format differs and older Mac versions of Office have limited VBA file handling. Check that the library syncs and test the path before relying on it.

How do I list files in subfolders too?

A plain Dir loop only covers one folder. Use a recursive procedure that calls itself for each subfolder, or use FileSystemObject and its folder collection, which walks subfolders more cleanly.

Why does a newly added file not appear in the list?

The sync has not finished, or the file is an online-only placeholder. Open it once in File Explorer, or wait for OneDrive to report that it is up to date, then rerun the macro.

Can one macro affect files other people are editing?

Only if you open them for writing. Keep testing with read-only opens and SaveChanges:=False. Any macro that writes to shared files should be reviewed before it runs on a schedule.