vba not accessing sharepoint site getting error using FSO - excel

I am trying to get access the share point folder and files using VBA.
I have tried the same code to access the local folders and files it was working perfectly fine.
Here is my folder structure from share point. I need to access all the files inside the folder
Mainfolder
Folder 1
Folder 2
Folder 3
Folder 4
Option Explicit
Sub Somesub()
Call GetFiles("https://isharenew.xyz.com/main folder \")
End Sub
Sub GetFiles(ByVal path As String)
Dim spSite As String
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim folder As Object
Set folder = fso.GetFolder(path)
Dim subfolder As Object
Dim file As Object
For Each subfolder In folder.SubFolders
GetFiles (subfolder.path)
Next subfolder
For Each file In folder.Files
Range("L" & Rows.Count).End(xlUp).Offset(1, 0) = file.path
Next file
Set fso = Nothing
Set folder = Nothing
Set subfolder = Nothing
Set file = Nothing
End Sub
With my above code I am getting error Path Not Found
Please let me know where I am doing mistake.

Related

Traverse zip file using VBA

I need to traverse a zip files using VBA. In particular I need to, without unzipping the file, locate the xl folder in order to find the media subfolder. I then need to copy the images out of the media subfolder and save them to another folder.
Public Sub Extract_Images()
Dim fso As FileSystemObject
Dim objFile As File
Dim myFolder
Const zipDir As String = "\\...\ZIP FILES"
Const xlFolder As String = "xl"
Const mediaFolder As String = "media"
Dim picname As String
Dim zipname As String
Set fso = New FileSystemObject
Set myFolder = fso.GetFolder(zipDir)
For Each objFile In myFolder.Files
zipname = objFile.Name
Next objFile
End Sub
^That code successfully loops through the folder and gathers the names of the zip files. But I need to get into the files and traverse the structures to get to the Media folder.
Building off: https://www.rondebruin.nl/win/s7/win002.htm
Edit: - this shows how you can incorporate the extraction into your code. Just pass the full zip path and the location to where you want to extract the files. You can do this from within your existing loop.
You may need to account for media files sharing the same name if you're planning on extracting them all to the same location...
Sub Tester()
ExtractMediaFiles "C:\Users\twilliams\Desktop\tempo.zip", _
"C:\Users\twilliams\Desktop\extracted\"
End Sub
Sub ExtractMediaFiles(zipFile As Variant, outFolder As Variant)
Dim oApp As Object
Dim fileNameInZip As Variant, oNS As Object
Set oApp = CreateObject("Shell.Application")
On Error Resume Next
Set oNS = oApp.Namespace(zipFile & "\xl\media")
On Error GoTo 0
If Not oNS Is Nothing Then
For Each fileNameInZip In oNS.items
Debug.Print fileNameInZip
oApp.Namespace(outFolder).copyhere oNS.items.Item(CStr(fileNameInZip))
Next
Else
Debug.Print "No xl\media path for " & zipFile
End If
End Sub

Looping through different kinds of files in folder (VBA)

I have a question regarding looping through a folder of two different kinds of files: xlsm and mdb (Access).
I have currently written a macro that would open 1 single xlsm file and 1 single mdb file before copying some data from the xlsm to mdb file and then saving the mdb file.
Now, I would like this process to repeat through a folder that has 50 xlsm files and 50 mdb files. They have the same names, so for example the loop should do this:
Open both xlsm and mdb files called "2001".
Perform copying and pasting etc from xlsm to mdb (I have written this part).
Save the mdb file.
Close both xlsm and mdb files called "2001".
Repeat steps 1-4 for "2002", "2003", etc in the folder.
I am really new to VBA so much help is appreciated! Looking forward to any guidance at all. Merry Christmas!
I just did today sample code for listing JPG files in folder, you can adopt and modify to do exactly what you like it to do, but would be very hard to give you exact code without being able to see your solution.
Public Sub listImages(folderPath As String)
'define variables
Dim fso As Object
Dim objFolder As Object
Dim objFolders As Object
Dim objF As Object
Dim objFile As Object
Dim objFiles As Object
Dim strFileName As String
Dim strFilePath As String
Dim myList As String
'set file system object
Set fso = CreateObject("Scripting.FileSystemObject")
'set folder object
Set objFolder = fso.GetFolder(folderPath)
'set files
Set objFiles = objFolder.files
Set objFolders = objFolder.subfolders
'list all images in folder
For Each objFile In objFiles
If Right(objFile.Name, 4) = ".jpg" Then
strFileName = objFile.Name
strFilePath = objFile.Path
myList = myList & strFileName & " - " & strFilePath & vbNewLine
End If
Next
'go through all subflders
For Each objF In objFolders
Call listImages(objF.Path)
Next
Debug.Print myList
Set objFolder = Nothing
Set objFile = Nothing
Set fso = Nothing
End Sub

VBA Excel: list files in backup (prev. version) folder

Below is a very simple code for listing files in a specified folder. It works as expected.
However, when I provide one of the backup folders of this folder, it throws invalid path (backup folder path copied from win explorer window).
E.g.:
Set objFolder = objFSO.GetFolder("D:\Test")** 'works well
Set objFolder = objFSO.GetFolder("D:\Test (2015.2.12 3:12 PM)") 'not work. this is a previous/backup version of folder test created by Win7
Is there a way to list backup folder files with VBA in Excel?
Sub Example1()
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim i As Integer
'Create an instance of the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get the folder object
Set objFolder = objFSO.GetFolder("D:\Test")
'Set objFolder = objFSO.GetFolder("D:\Test (2015.2.12 3:12PM)") throws error
i = 1
'loops through each file in the directory and prints their names and path
For Each objFile In objFolder.Files
'print file name
Cells(i + 1, 1) = objFile.Name
'print file path
Cells(i + 1, 2) = objFile.Path
i = i + 1
Next objFile
End Sub
How backup folders look like:
When I copy the path and paste it, some ? chars appear in the brackets:
\localhost\C$\Users\IBALLA1\Downloads (?Friday, ?April ?17, ?2015, ??9:05 AM)
You can't use colons in file names or folders names. The : in the time you have in a folder name can't be create and can't already be there. I don't know if that's a typo in your question or not though.

Open only .xls files in a given directory using macro

Just wondering... I have a piece of code - a macro in excel that allows me to open files in a given directory. The thing is that I only want to open .xls files, and the code that I have opens all the files in a given directory.
Can anyone help me with this matter.
Thanks.
Sub FindOpenFiles()
Dim FSO As Scripting.FileSystemObject, folder As Scripting.folder, file As Scripting.file, wb As Workbook
Dim directory As String
directory = "O:\test"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set folder = FSO.GetFolder(directory)
For Each file In folder.Files
Workbooks.Open file
Next file
End Sub
Sub FindOpenFiles()
Dim FSO As Scripting.FileSystemObject, folder As Scripting.folder, file As Scripting.file, wb As Workbook
Dim directory As String
directory = "O:\test"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set folder = FSO.GetFolder(directory)
For Each file In folder.Files
If Mid(file.Name, InStrRev(file.Name, ".") + 1) = "xls" Then
Workbooks.Open directory & Application.PathSeparator & file.Name
End If
Next file
End Sub
This woks fine...
See if this works, may need a tweak or two!
For Each file In folder.Files
If Right(file, 4) = ".xls" Then
Workbooks.Open file
End If
Next file
Try this but I am not really sure
directory = "*.xls"

code to loop through all workbooks in a folder VB

I have a number of excel (.xls) stored in a folder in a local drive. I need to do some process to every file in this folder. What is the code that will
loop through every file
open the file
Do some processing and then Save & close the file
move the file to another folder after processing
To be more clear, I want go over every file and do processing to it. After finishing a file, go to another file and so till the end of all the files in the folder. I do have the code for the processing; I just need to know the code that will loop through the files and move then to another folder.
Thanks for your help in advance,
What you need is a recursive function that iterates over the tree that represents a file system. It means to iterate over all the childs of some 'parent folder'. I send you a function that does something similar, to the one you need (this is currently in usage). This function deletes all the empty folders given a parent folder.
Public Function gf_DeleteEmptyFolder(path As String) As Boolean
On Error GoTo Error_Handler
Dim fso_folder As Scripting.Folder, sub_folder As Scripting.Folder
If g_FSO.FolderExists(path) Then
Set fso_folder = g_FSO.GetFolder(path)
'-- eliminates de folder only if is empty
If 0 = fso_folder.Files.Count And 0 = fso_folder.SubFolders.Count Then
Call g_FSO.DeleteFolder(path, False)
'-- recursively calls the function
Else
For Each sub_folder In fso_folder.SubFolders
Call gf_DeleteEmptyFolder(sub_folder.path)
Next
End If
End If
gf_DeleteEmptyFolder = True
Exit Function
'~~~ on error
Error_Handler:
gf_DeleteEmptyFolder = False
End Function
If your files are stored in a simple folder, then you can use the following code to iterate each file.
Public Sub fsoProcessFilesInFolder(sFolder As String)
Dim fso As Scripting.FileSystemObject, fld As Scripting.Folder, fil As Scripting.File
Set fso = New FileSystemObject
Set fld = fso.GetFolder(sFolder)
For Each fil In fld.Files
'--- add code to process your files
Next fil
End Sub
Here's the easy VBA object way to do it:
Dim fs As FileSearch
Dim i As Integer
Dim wbk As Workbook
Set fs = Application.FileSearch
With fs
.LookIn = ThisWorkbook.Path
.FileName = "*.xls"
For i = 1 to .Execute()
Set wbk = Workbooks.Open(.FoundFiles(i))
''//DO STUFF HERE
wbk.Close(SaveChanges:=True)
Next i
End With
In VB6 you have three options, as shown in the following KB articles:
How to Search Directories to Find or List Files
HOW TO: Recursively Search Directories by Using FileSystemObject
The following code will read xlsx/xls files from given folder neglecting other files and iterate through each item.
You can use it for any set of extensions and filters.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(folderPath)
Set objFiles = objFolder.Files
'Iterate through the files in the folder
For Each Item In objFiles
If LCase(Right(Item.Name, 5)) = ".xls" Or LCase(Right(Item.Name, 4)) = ".xlsx" Then
''''''Do Stuffs Here''''''
End If
Next

Resources