Sharpefiction
New Member
- Joined
- Sep 29, 2010
- Messages
- 33
Hi All,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-comfficeffice" /><o></o>
<o></o>
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></o>
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></o>
<o> </o>
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></o>
<o> </o>
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></o>
<o> </o>
'Sub ListAllFile()<o></o>
'<o></o>
' Dim objFSO As Object<o></o>
' Dim objFolder As Object<o></o>
' Dim ObjFile As Object<o></o>
' Dim WBo As Workbook<o></o>
' Dim pth As String<o></o>
' Dim WBn As Workbook<o></o>
' Dim FinalRow As Long<o></o>
' Set WBo = ThisWorkbook<o></o>
' Dim ObCount As Long<o></o>
' Dim FileNme As String<o></o>
' Dim FnameMstr As String<o></o>
' Dim SnameMstr As String<o></o>
' Dim RforNomMstr As String<o></o>
' Dim NomCatMstr As String<o></o>
' Dim NamNomMstr As String<o></o>
'<o></o>
' Application.ScreenUpdating = False<o></o>
'<o></o>
' Set objFSO = CreateObject("Scripting.FileSystemObject")<o></o>
'<o></o>
' Application.StatusBar = False<o></o>
'<o></o>
' FinRow = Cells(Rows.Count, 2).End(xlUp).Row + 1<o></o>
'<o></o>
' Cells(2, 2).Resize(FinRow, 6).ClearContents<o></o>
'<o></o>
' 'Get the folder object associated with the directory<o></o>
' Set objFolder = objFSO.GetFolder("//projectspace/ColleagueResponses/")<o></o>
** 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></o>
' Data.Activate<o></o>
'<o></o>
' pth = http://projectspace/colleagueResponses/<o></o>
** 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></o>
'<o></o>
' ObCount = objFolder.Files.Count<o></o>
** counts the number of files in the folder<o></o>
'<o></o>
' 'Loop through the Files collection<o></o>
' For Each ObjFile In objFolder.Files<o></o>
' Nm1 = Len("\\projectspace\colleagueResponses\")
<o>** You'll need to specify your path here **</o>
' Nm2 = Len(ObjFile) - Nm1<o></o>
' FileNme = Right(ObjFile, Nm2)<o></o>
** I’ve done this part to find out/set the file name**<o></o>
<o> </o>
' WBo.Activate<o></o>
' FinRow = Cells(Rows.Count, 2).End(xlUp).Row + 1<o></o>
'<o></o>
' Set WBn = Workbooks.Open(pth & FileNme, , , , Password:="Password1")<o></o>
'<o></o>
' Application.ScreenUpdating = False<o></o>
'<o></o>
' FnameMstr = Range("FName")<o></o>
' SnameMstr = Range("SName")<o></o>
' RforNomMstr = Range("RforNom")<o></o>
' NomCatMstr = Range("NomCat")<o></o>
' NamNomMstr = Range("NamNom")<o></o>
' WBo.Activate<o></o>
' Cells(FinRow, 2).Value = FnameMstr<o></o>
' Cells(FinRow, 3).Value = SnameMstr<o></o>
' Cells(FinRow, 4).Value = RforNomMstr<o></o>
' Cells(FinRow, 5).Value = NomCatMstr<o></o>
' Cells(FinRow, 6).Value = NamNomMstr<o></o>
'<o></o>
' WBn.Close savechanges:=False<o></o>
<o> </o>
'<o></o>
' Next ' loops through each file<o></o>
'<o></o>
' '<o></o>
' Range(Cells(2, 2), Cells(FinRow, 6)).Select<o></o>
' Selection.Sort Key1:=Cells(2, 3), Order1:=xlAscending, Header:=xlGuess, _<o></o>
' OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _<o></o>
' DataOption1:=xlSortNormal<o></o>
'<o></o>
' Cells(1, 2).Select<o></o>
'<o></o>
' WBo.Save<o></o>
'<o></o>
'Application.StatusBar = False<o></o>
'<o></o>
'Application.ScreenUpdating = True<o></o>
'<o></o>
'End Sub<o></o>
<o> </o>
<o></o>
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.
<o></o>
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></o>
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></o>
<o> </o>
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></o>
<o> </o>
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></o>
<o> </o>
'Sub ListAllFile()<o></o>
'<o></o>
' Dim objFSO As Object<o></o>
' Dim objFolder As Object<o></o>
' Dim ObjFile As Object<o></o>
' Dim WBo As Workbook<o></o>
' Dim pth As String<o></o>
' Dim WBn As Workbook<o></o>
' Dim FinalRow As Long<o></o>
' Set WBo = ThisWorkbook<o></o>
' Dim ObCount As Long<o></o>
' Dim FileNme As String<o></o>
' Dim FnameMstr As String<o></o>
' Dim SnameMstr As String<o></o>
' Dim RforNomMstr As String<o></o>
' Dim NomCatMstr As String<o></o>
' Dim NamNomMstr As String<o></o>
'<o></o>
' Application.ScreenUpdating = False<o></o>
'<o></o>
' Set objFSO = CreateObject("Scripting.FileSystemObject")<o></o>
'<o></o>
' Application.StatusBar = False<o></o>
'<o></o>
' FinRow = Cells(Rows.Count, 2).End(xlUp).Row + 1<o></o>
'<o></o>
' Cells(2, 2).Resize(FinRow, 6).ClearContents<o></o>
'<o></o>
' 'Get the folder object associated with the directory<o></o>
' Set objFolder = objFSO.GetFolder("//projectspace/ColleagueResponses/")<o></o>
** 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></o>
' Data.Activate<o></o>
'<o></o>
' pth = http://projectspace/colleagueResponses/<o></o>
** 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></o>
'<o></o>
' ObCount = objFolder.Files.Count<o></o>
** counts the number of files in the folder<o></o>
'<o></o>
' 'Loop through the Files collection<o></o>
' For Each ObjFile In objFolder.Files<o></o>
' Nm1 = Len("\\projectspace\colleagueResponses\")
<o>** You'll need to specify your path here **</o>
' Nm2 = Len(ObjFile) - Nm1<o></o>
' FileNme = Right(ObjFile, Nm2)<o></o>
** I’ve done this part to find out/set the file name**<o></o>
<o> </o>
' WBo.Activate<o></o>
' FinRow = Cells(Rows.Count, 2).End(xlUp).Row + 1<o></o>
'<o></o>
' Set WBn = Workbooks.Open(pth & FileNme, , , , Password:="Password1")<o></o>
'<o></o>
' Application.ScreenUpdating = False<o></o>
'<o></o>
' FnameMstr = Range("FName")<o></o>
' SnameMstr = Range("SName")<o></o>
' RforNomMstr = Range("RforNom")<o></o>
' NomCatMstr = Range("NomCat")<o></o>
' NamNomMstr = Range("NamNom")<o></o>
' WBo.Activate<o></o>
' Cells(FinRow, 2).Value = FnameMstr<o></o>
' Cells(FinRow, 3).Value = SnameMstr<o></o>
' Cells(FinRow, 4).Value = RforNomMstr<o></o>
' Cells(FinRow, 5).Value = NomCatMstr<o></o>
' Cells(FinRow, 6).Value = NamNomMstr<o></o>
'<o></o>
' WBn.Close savechanges:=False<o></o>
<o> </o>
'<o></o>
' Next ' loops through each file<o></o>
'<o></o>
' '<o></o>
' Range(Cells(2, 2), Cells(FinRow, 6)).Select<o></o>
' Selection.Sort Key1:=Cells(2, 3), Order1:=xlAscending, Header:=xlGuess, _<o></o>
' OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _<o></o>
' DataOption1:=xlSortNormal<o></o>
'<o></o>
' Cells(1, 2).Select<o></o>
'<o></o>
' WBo.Save<o></o>
'<o></o>
'Application.StatusBar = False<o></o>
'<o></o>
'Application.ScreenUpdating = True<o></o>
'<o></o>
'End Sub<o></o>
<o> </o>
<o></o>
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.