With code you have provided I have tried to modify and i trying to do same job but in sharepoints and I am not able to achieve it. below is the code i am trying
Sub OpnZip()
Dim latestZipFile As String
' Update the SharePoint site URL
latestZipFile = getLatestZipFile("
https://mine.sharepoint.com/sites/My-FinSys/eCFProd/CEWL/")
If Len(latestZipFile) = 0 Then
MsgBox "No Zip files found!", vbExclamation
Exit Sub
End If
On Error Resume Next
' Update the SharePoint site URL for source and destination
DownloadFileFromSharePoint "
https:// mine.sharepoint.com/sites/My -FinSys/eCFProd/CEWL/" & latestZipFile, "C:\Temp\" & latestZipFile
If Err.Number <> 0 Then
MsgBox "Error: " & Err.Description, vbCritical
Err.Clear
Exit Sub
End If
On Error GoTo 0
' Now, you can use your existing code to unzip the downloaded file
UnzipAFile "C:\Temp\" & latestZipFile, "\\ mine.sharepoint.com/\sites\GRP-SOP-AppMgmtOps\Shared Documents\General\01 Knowledge Acquisition\Enterprise\RCM\CEWL and Suspense Report\"
' Optionally, clean up the downloaded file from the local directory
Kill "C:\Temp\" & latestZipFile
MsgBox "Operation completed successfully!", vbInformation
End Sub
Function getLatestZipFile(ByVal sourcePath As String) As String
Dim xmlHTTP As Object
Dim responseText As String
Dim latestZipFile As String
Dim currentDate As Date
Dim startIndex As Long
Dim endIndex As Long
Dim fileName As String
' Send HTTP request to get file names from SharePoint
Set xmlHTTP = CreateObject("MSXML2.ServerXMLHTTP")
xmlHTTP.Open "GET", sourcePath & "?\$top=1000", False
xmlHTTP.send
If xmlHTTP.Status = 200 Then
responseText = xmlHTTP.responseText
' Extract dates directly from filenames
startIndex = InStr(responseText, "_ChargeErrorWorklistAll.zip")
While startIndex > 0
' Extract the full filename
endIndex = InStr(startIndex, responseText, """")
fileName = Mid(responseText, startIndex, endIndex - startIndex + 5)
' Extract date from the filename
currentDate = CDate(Mid(fileName, 1, 10))
Debug.Print "Found file: " & fileName & " with date: " & currentDate
' Check if it's the latest file
If currentDate > latestDate Then
latestDate = currentDate
latestZipFile = fileName
End If
' Move to the next occurrence
startIndex = InStr(startIndex + 1, responseText, "_ChargeErrorWorklistAll.zip")
Wend
Debug.Print "Latest file found: " & latestZipFile
Else
Debug.Print "Failed to get file list from SharePoint. HTTP Status: " & xmlHTTP.Status
Err.Raise vbObjectError + 1, "getLatestZipFile", "Failed to get file list from SharePoint."
End If
getLatestZipFile = latestZipFile
End Function
Sub DownloadFileFromSharePoint(sourceURL As String, destinationPath As String)
Dim xmlHTTP As Object
Set xmlHTTP = CreateObject("MSXML2.ServerXMLHTTP")
xmlHTTP.Open "GET", sourceURL, False
xmlHTTP.send
If xmlHTTP.Status = 200 Then
Dim oStream As Object
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
oStream.Write xmlHTTP.responseBody
oStream.SaveToFile destinationPath, 2
oStream.Close
Else
Err.Raise vbObjectError + 1, "DownloadFileFromSharePoint", "Failed to download file from SharePoint."
End If
End Sub
Sub UnzipAFile(zippedFileFullName As Variant, unzipToPath As Variant)
Dim sh As Object
Set sh = CreateObject("Shell.Application")
sh.Namespace(unzipToPath).CopyHere sh.Namespace(zippedFileFullName).Items, 16
End Sub
Output I am getting is no Zip file found. Can you please help me here?