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:eek:ffice:eek:ffice" /><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

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Mate i tried but it does not work.
Can tell me what range defintion ie what columns and rows?

FnameMstr = Range("FName")
SnameMstr = Range("SName")
RforNomMstr = Range("RforNom")
NomCatMstr = Range("NomCat")
NamNomMstr = Range("NamNom")


Biz
 
Upvote 0
[FONT=&quot]Sorry, I think I’ve confused things by leaving my coding in. I’ll remove everything but that needed to make the loop work, you just need to add in your bits specific for your spreadsheets[/FONT]

[FONT=&quot]Sub ListAllFile()[/FONT]
[FONT=&quot]'[/FONT]
[FONT=&quot]' Dim objFSO As Object[/FONT]
[FONT=&quot]' Dim objFolder As Object[/FONT]
[FONT=&quot]' Dim ObjFile As Object[/FONT]
[FONT=&quot]' Dim pth As String[/FONT]
[FONT=&quot]' Dim WBn As Workbook[/FONT]
[FONT=&quot]' Dim ObCount As Long[/FONT]
[FONT=&quot]' Dim FileNme As String[/FONT]
[FONT=&quot]'[/FONT]
[FONT=&quot]' Application.ScreenUpdating = False[/FONT]
[FONT=&quot]'[/FONT]
[FONT=&quot]' Set objFSO = CreateObject("Scripting.FileSystemObject")[/FONT]
[FONT=&quot]'[/FONT]
[FONT=&quot]' 'Get the folder object associated with the directory[/FONT]
[FONT=&quot]' Set objFolder = objFSO.GetFolder("//projectspace/ColleagueResponses/")[/FONT]
[FONT=&quot]** 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 ***[/FONT]
[FONT=&quot]'[/FONT]
[FONT=&quot]' pth = http://projectspace/colleagueResponses/[/FONT]
[FONT=&quot]** 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 **[/FONT]
[FONT=&quot]'[/FONT]
[FONT=&quot]' ObCount = objFolder.Files.Count[/FONT]
[FONT=&quot]** counts the number of files in the folder[/FONT]
[FONT=&quot]'[/FONT]
[FONT=&quot]' 'Loop through the Files collection[/FONT]
[FONT=&quot]' For Each ObjFile In objFolder.Files[/FONT]
[FONT=&quot]' Nm1 = Len("\\projectspace\colleagueResponses\")[/FONT]
[FONT=&quot]** You'll need to specify your path here **[/FONT]
[FONT=&quot]' Nm2 = Len(ObjFile) - Nm1[/FONT]
[FONT=&quot]' FileNme = Right(ObjFile, Nm2)[/FONT]
[FONT=&quot]** I’ve done this part to find out/set the file name**[/FONT]
[FONT=&quot]'[/FONT]
[FONT=&quot]' Set WBn = Workbooks.Open(pth & FileNme, , , , Password:="Password1")[/FONT]
[FONT=&quot]** opens the first file in the library – if there is no password, the remove everything from - , , , , Password:="Password1" – leaving the close bracket ‘)’[/FONT]

[FONT=&quot]' Application.ScreenUpdating = False[/FONT]
[FONT=&quot]** optional – you can leave the screen updating on[/FONT]
[FONT=&quot]'[/FONT]
[FONT=&quot]<< Your coding here>>[/FONT]
[FONT=&quot]** The file is now open. Enter whatever code is specific to your spreadsheets.[/FONT]
[FONT=&quot]'[/FONT]
[FONT=&quot]' Next[/FONT]
[FONT=&quot]** goes to next file within your sharepoint folder[/FONT]

[FONT=&quot]'End Sub[/FONT]

[FONT=&quot]Hope this helps[/FONT]
 
Upvote 0
The macros belong get list of files in Sharepoint under location

Code:
Sub Create()
    EnableReference
    CreateList
End Sub
Private Sub EnableReference()
 
On Error Resume Next
ThisWorkbook.VBProject.References.AddFromFile "C:\Windows\system32\scrrun.dll"
On Error GoTo 0
End Sub
Private Sub CreateList()
    Dim folder As folder
    Dim f As File
    Dim fs As New FileSystemObject
    Dim RowCtr As Integer
    RowCtr = 1
    Set folder = fs.GetFolder("[COLOR=#000080]\\projectspace\colleagueResponses\[/COLOR]") '<=Variable Location
    For Each f In folder.Files
       Cells(RowCtr, 1).Value = f.Name
       RowCtr = RowCtr + 1
    Next f
End Sub

May be helpful for somebody.

Biz
 
Upvote 0
Thanks Biz. My coding was aimed at getting information from within the spreadsheets, whereas your coding creates a list of the names within the spreadsheet you're running the macro from.

I suppose it will depend on what you're after from the loop & the information it can provide. I wrote mine that way as I was having two issues - one was creating a loop for a sharepoint library directory; two was opening the file because of the variance of the file name. The code I posted does that job perfectly & I posted as I couldn't find a previously posted solution to my problem.

Thanks for your coding though. As I said at the bottom of my original post, there is probably more efficient coding that can be used & your loop looks like it could be! (I'll have to test first before I commit :) ) Hopefully one or both can be useful.
 
Last edited:
Upvote 0
Hi Sharpefiction,

It is cool as we all are learning through forums everyday.

Biz
 
Upvote 0
Hi there

I know this is an old thread, however wondering if someone could help me. I have used (and tweaked) the code provided by Sharpefiction, however it bugs out at the following line:

Set objFolder = objFSO.GetFolder("//projectspace/ColleagueResponses/")

It says it can't find the path, however I know the path exists (I've tried removing http: but still doesnt work) *note i have used my own path not the above.

I think the issue is due to username and password restrictions. Is there anyway to adapt the code to allow the username and password to be entered?

I have also tried adding code before this to open up the directory in Internet Explorer and enter username / password. This works and gets me into the sharepoint directory, however I don't know how to then run the code to list out the files within the folder.

Thanks in advance.
 
Upvote 0
It's an interesting one.. I've used my code on lots of different occasions & it's always worked. You need to have access to the SharePoint library - read access might be acceptable, though worth getting at least the next one up - & then make sure the file path is correct.

Have you tried stepping through the code, bit by bit to see which part of the code is causing the issue? - sorry if you've already done that, but it's always my first step.

Can I also check that you're using the forward slashes & back slashes on the right bits? That caught me when first trying the code.

If you can step through & then let me know which part comes up with invalid path/cannot find path etc, I'll try to help more.
 
Upvote 0
I can't get this to work. Has anyone actually been able to get a listing of SharePoint files in a folder?
 
Upvote 0
I can't get this to work. Has anyone actually been able to get a listing of SharePoint files in a folder?

Hi Mike,

Which code are you trying? The code I posted works for going through each file within a SharePoint library/folder & getting information from within those files to then populate the spreadsheet the macro is being run from; it doesn't provide a list of files within the library/folder. The code posted by Biz on the other hand, seems like it's aimed at providing a list of the file names within the library/folder.
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,028
Members
448,940
Latest member
mdusw

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