Sharepoint library: VBA to loop through all files in library

Sharpefiction

New Member
Joined
Sep 29, 2010
Messages
33
Hi All,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
I spent hours looking through different means of looping through a SharePoint folder within Excel VBA to get details from the files there and then store them within a spreadsheet.<o:p></o:p>
What I was aiming to do was gather all of the information from a 'document library' of excel files (where files were names I didn’t know & therefore couldn't specify beforehand), of varying names & varying quantity, but with the same defined names & so on within the spreadsheet. <o:p></o:p>
<o:p> </o:p>
Finally, the other day I cracked it. I might be posting this in the wrong place, but I wanted to share in case there is someone like me out there who could use.<o:p></o:p>
<o:p> </o:p>
I need to say that the initial loop through files was copied from a forum response I found a little while ago and have used in various instances since. Unfortunately, I cannot give credit directly, as I didn’t note their username/the site where I got it from. So thank you for your code.<o:p></o:p>
<o:p> </o:p>
'Sub ListAllFile()<o:p></o:p>
'<o:p></o:p>
' Dim objFSO As Object<o:p></o:p>
' Dim objFolder As Object<o:p></o:p>
' Dim ObjFile As Object<o:p></o:p>
' Dim WBo As Workbook<o:p></o:p>
' Dim pth As String<o:p></o:p>
' Dim WBn As Workbook<o:p></o:p>
' Dim FinalRow As Long<o:p></o:p>
' Set WBo = ThisWorkbook<o:p></o:p>
' Dim ObCount As Long<o:p></o:p>
' Dim FileNme As String<o:p></o:p>
' Dim FnameMstr As String<o:p></o:p>
' Dim SnameMstr As String<o:p></o:p>
' Dim RforNomMstr As String<o:p></o:p>
' Dim NomCatMstr As String<o:p></o:p>
' Dim NamNomMstr As String<o:p></o:p>
'<o:p></o:p>
' Application.ScreenUpdating = False<o:p></o:p>
'<o:p></o:p>
' Set objFSO = CreateObject("Scripting.FileSystemObject")<o:p></o:p>
'<o:p></o:p>
' Application.StatusBar = False<o:p></o:p>
'<o:p></o:p>
' FinRow = Cells(Rows.Count, 2).End(xlUp).Row + 1<o:p></o:p>
'<o:p></o:p>
' Cells(2, 2).Resize(FinRow, 6).ClearContents<o:p></o:p>
'<o:p></o:p>
' 'Get the folder object associated with the directory<o:p></o:p>
' Set objFolder = objFSO.GetFolder("//projectspace/ColleagueResponses/")<o:p></o:p>
** You'll need to specify your path here. By removing the http: from the path, the code liked it & found the folder. It wasn’t working previously ***<o:p></o:p>
' Data.Activate<o:p></o:p>
'<o:p></o:p>
' pth = http://projectspace/colleagueResponses/<o:p></o:p>
** You'll need to specify your path here. The reason I’ve done this separately is because the path is not recognised otherwise when trying to specify it with workbook.open & using the value set for objFolder **<o:p></o:p>
'<o:p></o:p>
' ObCount = objFolder.Files.Count<o:p></o:p>
** counts the number of files in the folder<o:p></o:p>
'<o:p></o:p>
' 'Loop through the Files collection<o:p></o:p>
' For Each ObjFile In objFolder.Files<o:p></o:p>
' Nm1 = Len("\\projectspace\colleagueResponses\")
<o:p>** You'll need to specify your path here **</o:p>
' Nm2 = Len(ObjFile) - Nm1<o:p></o:p>
' FileNme = Right(ObjFile, Nm2)<o:p></o:p>
** I’ve done this part to find out/set the file name**<o:p></o:p>
<o:p> </o:p>
' WBo.Activate<o:p></o:p>
' FinRow = Cells(Rows.Count, 2).End(xlUp).Row + 1<o:p></o:p>
'<o:p></o:p>
' Set WBn = Workbooks.Open(pth & FileNme, , , , Password:="Password1")<o:p></o:p>
'<o:p></o:p>
' Application.ScreenUpdating = False<o:p></o:p>
'<o:p></o:p>
' FnameMstr = Range("FName")<o:p></o:p>
' SnameMstr = Range("SName")<o:p></o:p>
' RforNomMstr = Range("RforNom")<o:p></o:p>
' NomCatMstr = Range("NomCat")<o:p></o:p>
' NamNomMstr = Range("NamNom")<o:p></o:p>
' WBo.Activate<o:p></o:p>
' Cells(FinRow, 2).Value = FnameMstr<o:p></o:p>
' Cells(FinRow, 3).Value = SnameMstr<o:p></o:p>
' Cells(FinRow, 4).Value = RforNomMstr<o:p></o:p>
' Cells(FinRow, 5).Value = NomCatMstr<o:p></o:p>
' Cells(FinRow, 6).Value = NamNomMstr<o:p></o:p>
'<o:p></o:p>
' WBn.Close savechanges:=False<o:p></o:p>
<o:p> </o:p>
'<o:p></o:p>
' Next ' loops through each file<o:p></o:p>
'<o:p></o:p>
' '<o:p></o:p>
' Range(Cells(2, 2), Cells(FinRow, 6)).Select<o:p></o:p>
' Selection.Sort Key1:=Cells(2, 3), Order1:=xlAscending, Header:=xlGuess, _<o:p></o:p>
' OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _<o:p></o:p>
' DataOption1:=xlSortNormal<o:p></o:p>
'<o:p></o:p>
' Cells(1, 2).Select<o:p></o:p>
'<o:p></o:p>
' WBo.Save<o:p></o:p>
'<o:p></o:p>
'Application.StatusBar = False<o:p></o:p>
'<o:p></o:p>
'Application.ScreenUpdating = True<o:p></o:p>
'<o:p></o:p>
'End Sub<o:p></o:p>
<o:p> </o:p>
<o:p></o:p>
I hope this is of some use; I just wanted to give something back really.

Some of the coding could probably be made more efficient, but it does work.
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
In your code, the line:
Code:
Set objFolder = objFSO.GetFolder(PthNoHttp)
returns error 76, path not found.

I strip out the Https: and any spaces I leave as spaces and not as %20. I also turn the / to \. Suggestions?
 
Upvote 0
After mapping my SharePoint directory to a drive letter, I noticed that the name had some interesting changes:

Directory mapped: "https://mycompany.sharepoint.microsoftonline.com/folder1/folder2/Document Library/"
Displayed name in mapped drive: "\\mycompany.sharepoint.microsoftonline.com@SSL\DavWWWRoot\folder1\folder2\Document Library"

When I tried using the mapped name - including the "@SSL\DavWWWRoot\" - in the GetFolder method of the code, it seems to work fine. Well, very slow, but still fine. I'm not sure exactly what the modified string does (though I assume the SSL is for the Secured Socket Layer used in the https connection), but it appears that it is what Windows needs to use the SharePoint Document Library like a Windows folder.

BTW, I'm using Windows 7, Office 2007, SharePoint 2010.
 
Upvote 0
Hi

My sharepoint folder address is << https://team.extsp.ford.com/sites/DIESELDIAGNOSTICS/Global Documents/Test Folder (Tool Experiment)>>

I notice couple of spaces there. Your code is not working. It showing error in GetFolder(" ") command. I gave the command like this:
Set objFolder = objFSO.GetFolder("//team.extsp.ford.com/sites/DIESELDIAGNOSTICS/Global Documents/Test Folder (Tool Experiment)/")

I need your help to fix this.

ANDREW
 
Upvote 0

Forum statistics

Threads
1,223,264
Messages
6,171,081
Members
452,377
Latest member
bradfordsam

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