Following on from this excellent piece of work, here:
Batch copy files to SharePoint site
I can now upload my zipped files to Sharepoint with a click of a button.
My problem is now thus: How do I delete the files I upload using the same method?
I've amended the code slightly to save different files to different SharePoint folders.
Sample below:
Public Sub CopyToSharePoint()
Dim xmlhttp
Dim sharepointUrl
Dim sharepointFolder
Dim sharepointFileName
Dim LstrFileName, strFilePath, strMonthYear, PstrFullfileName, PstrTargetURL As String
Dim LlFileLength As Long
Dim Lvarbin() As Byte
Dim LvarBinData As Variant
Dim fso, LobjXML As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim fldr As folder
Dim f As File
'Parent Sharepoint
sharepointUrl = "[SHAREPOINT PATH HERE]"
'Sets the Month%20Year
strMonthYear = Format(Now(), "mmmm yyyy") & "\"
'File Path
strFilePath = "[ARCHIVE DRIVE]" & strMonthYear
'Check to see if DRA for current month%20year exists
If Len(Dir(strFilePath, vbDirectory)) = 0 Then
MkDir "strFilePath"
End If
Set LobjXML = CreateObject("Microsoft.XMLHTTP")
'Where we're uploading files from
Set fldr = fso.GetFolder(strFilePath)
For Each f In fldr.Files
If Format(f.DateCreated, "dd/mm/yyyy") = Format(Now(), "dd/mm/yyyy") Then
If InStr(1, f.Name, "[FILESTRING1]", vbTextCompare) > 0 Then
sharepointFolder = "[SHAREPOINTSTRING1]/"
ElseIf InStr(1, f.Name, "[FILESTRING2]", vbTextCompare) > 0 Then
sharepointFolder = "[SHAREPOINTSTRING2]"
ElseIf InStr(1, f.Name, "[DONOTUPLOADTHISFILE]", vbTextCompare) > 0 Then
GoTo NextF:
Else
sharepointFolder = "[SHAREPOINTMAINFOLDER]"
End If
sharepointFileName = sharepointUrl & sharepointFolder & f.Name
PstrFullfileName = strFilePath & f.Name
LlFileLength = FileLen(PstrFullfileName) - 1
' Read the file into a byte array.
ReDim Lvarbin(LlFileLength)
Open PstrFullfileName For Binary As #1
Get #1, , Lvarbin
Close #1
' Convert to variant to PUT.
LvarBinData = Lvarbin
PstrTargetURL = sharepointUrl & sharepointFolder & f.Name
' Put the data to the server, false means synchronous.
LobjXML.Open "PUT", PstrTargetURL, False
' Send the file in.
LobjXML.Send LvarBinData
End If
NextF:
Next f
Set LobjXML = Nothing
Set fso = Nothing
End Sub
I'd not closed the request to the server, d'oh!
Setting it up in a separate instance solved it for me.
I didn't convert the filename to binary and then to variant, merely kept it as a string.
You must omit the NOTHING from the last LobjXML.SEND given in LastCoder's example. Adding this in reproduces the Run-time error I give above.
Thanks for the help, LastCoder.
Here's the amended code:
Public Sub DeleteFromSharePoint()
Dim xmlhttp
Dim sharepointUrl, sharepointFolder, sharepointFileName
Dim f, strZip As String
Dim LobjXML As Object
' Parent Sharepoint
sharepointUrl = "[SHAREPOINT URL]"
' In this test module, we're just deleting from the parent directory
sharepointFolder = ""
' Sets the report name we want to remove
f = "test"
' Sets the full .ZIP filename
' This is how reports are archived by date
strZip = f & "%20-%20" & Format(Now() - 1, "YYYY.MM.DD") & ".zip"
Set LobjXML = CreateObject("Microsoft.XMLHTTP")
sharepointFileName = sharepointUrl & sharepointFolder & strZip
' Removes the data from the server, false means synchronous
LobjXML.Open "DELETE", sharepointFileName, False
' Sends the request to remove the file
LobjXML.Send
Set LobjXML = Nothing
End Sub
Related
Iam developing a vba marco-script for a customer which downloads zip file from github, and unpack the zip folder to a folder.
The script will create a folder ´pb´ if it doesnt exist already - and if the folder already exist it will delete the folder and create a new.
It works properly on my own pc, but the customer is getting this error as shown on screenshot.
The path on client's computer is following:
C:\Users\Nicol\xxx\xxx - Carina og Nicolas - Carina og Nicolas\Analyseværktøjer\Rådgivningsværktøj
And the client is using mircosoft teams drive, but it didnt work neither on his own desktop folder. So i dont know what cause it.
I cant figure out how to solve this.
[
Here is full source code of the marco.
Option Explicit
Function versionIsOutdated(strDir As String, strPath As String)
Dim FSO As New FileSystemObject
Dim FileToRead As Variant
Dim TextString As String
Dim path As String
path = strPath & strDir
If FSO.FolderExists(path) Then
' exist, lookup versionNumber
Dim FileUrl As String
Dim objXmlHttpReq As Object
Dim objStream As Object
Dim strResult
FileUrl = "https://raw.githubusercontent.com/Securelife-A-S/pb_integration/main/version.txt"
Set objXmlHttpReq = CreateObject("MSXML2.ServerXMLHTTP.6.0")
objXmlHttpReq.Open "GET", FileUrl, False
objXmlHttpReq.send
strResult = objXmlHttpReq.responseText
Set FSO = CreateObject("Scripting.FileSystemObject")
Set FileToRead = FSO.OpenTextFile(path & "\pb_integration-main\version.txt", ForReading) 'add here the path of your text file
TextString = FileToRead.ReadAll
FileToRead.Close
Debug.Print (TextString)
Debug.Print (strResult)
Dim compResult As Integer
If StrComp(TextString, strResult) = 0 Then
Debug.Print ("Version is up to date")
versionIsOutdated = False
Else
versionIsOutdated = True
Debug.Print ("Version is outdated")
End If
Else
versionIsOutdated = True
Debug.Print ("Folder is not downloaded yet")
End If
End Function
Function MkDir(strDir As String, strPath As String)
Dim FSO As New FileSystemObject
Dim path As String
path = strPath & strDir
If FSO.FolderExists(path) Then
' exist, so delete the folder
FSO.DeleteFolder path, True
Debug.Print "Deleting folder"
End If
If Not FSO.FolderExists(path) Then
' doesn't exist, so create the folder
FSO.CreateFolder path
Debug.Print "Creating folder"
End If
End Function
Function downloadAndUnzip()
Dim FileUrl As String
Dim objXmlHttpReq As Object
Dim objStream As Object
Dim strResult
FileUrl = "https://raw.githubusercontent.com/Securelife-A-S/pb_integration/main/version.txt"
Set objXmlHttpReq = CreateObject("MSXML2.ServerXMLHTTP.6.0")
objXmlHttpReq.Open "GET", FileUrl, False
objXmlHttpReq.send
strResult = objXmlHttpReq.responseText
Debug.Print (strResult)
FileUrl = "https://github.com/Securelife-A-S/pb_integration/archive/refs/heads/main.zip"
Set objXmlHttpReq = CreateObject("MSXML2.ServerXMLHTTP.6.0")
objXmlHttpReq.Open "GET", FileUrl, False
objXmlHttpReq.send
If objXmlHttpReq.Status = 200 Then
Set objStream = CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = 1
objStream.Write objXmlHttpReq.responseBody
objStream.SaveToFile ThisWorkbook.path & "\" & "pb.zip", 2
objStream.Close
End If
Debug.Print ("Download done")
Dim ShellApp As Object
'Copy the files & folders from the zip into a folder
Set ShellApp = CreateObject("Shell.Application")
ShellApp.Namespace(ThisWorkbook.path & "\pb").CopyHere ShellApp.Namespace(ThisWorkbook.path & "\pb.zip").Items
Debug.Print ("Unpack done")
End Function
Function DeleteVBComponent()
Dim CompName As String
CompName = "Main"
'Disabling the alert message
Application.DisplayAlerts = False
'Ignore errors
On Error Resume Next
'Delete the component
Dim vbCom As Object
Set vbCom = Application.VBE.ActiveVBProject.VBComponents
vbCom.Remove VBComponent:= _
vbCom.Item(CompName)
On Error GoTo 0
'Enabling the alert message
Application.DisplayAlerts = True
End Function
Function addBasFile()
Dim path As String
Dim objModule As Object
path = ThisWorkbook.path & "\pb\pb_integration-main\Main.bas"
Set objModule = Application.VBE.ActiveVBProject.VBComponents.Import(path)
objModule.Name = "Main"
Debug.Print path
End Function
Sub Workbook_Open()
Dim asd As Boolean
asd = versionIsOutdated("pb", ThisWorkbook.path & "\")
If asd = True Then
MsgBox "Der er kommet ny version - Downloading påbegyndt"
Call MkDir("pb", ThisWorkbook.path & "\")
Call downloadAndUnzip
Call DeleteVBComponent
Call addBasFile
Application.Run ("Main.init")
End If
End Sub
Following on from this excellent piece of work, here:
Batch copy files to SharePoint site
I can now upload my zipped files to Sharepoint with a click of a button.
My problem is now thus: How do I delete the files I upload using the same method?
I've amended the code slightly to save different files to different SharePoint folders.
Sample below:
Public Sub CopyToSharePoint()
Dim xmlhttp
Dim sharepointUrl
Dim sharepointFolder
Dim sharepointFileName
Dim LstrFileName, strFilePath, strMonthYear, PstrFullfileName, PstrTargetURL As String
Dim LlFileLength As Long
Dim Lvarbin() As Byte
Dim LvarBinData As Variant
Dim fso, LobjXML As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim fldr As folder
Dim f As File
'Parent Sharepoint
sharepointUrl = "[SHAREPOINT PATH HERE]"
'Sets the Month%20Year
strMonthYear = Format(Now(), "mmmm yyyy") & "\"
'File Path
strFilePath = "[ARCHIVE DRIVE]" & strMonthYear
'Check to see if DRA for current month%20year exists
If Len(Dir(strFilePath, vbDirectory)) = 0 Then
MkDir "strFilePath"
End If
Set LobjXML = CreateObject("Microsoft.XMLHTTP")
'Where we're uploading files from
Set fldr = fso.GetFolder(strFilePath)
For Each f In fldr.Files
If Format(f.DateCreated, "dd/mm/yyyy") = Format(Now(), "dd/mm/yyyy") Then
If InStr(1, f.Name, "[FILESTRING1]", vbTextCompare) > 0 Then
sharepointFolder = "[SHAREPOINTSTRING1]/"
ElseIf InStr(1, f.Name, "[FILESTRING2]", vbTextCompare) > 0 Then
sharepointFolder = "[SHAREPOINTSTRING2]"
ElseIf InStr(1, f.Name, "[DONOTUPLOADTHISFILE]", vbTextCompare) > 0 Then
GoTo NextF:
Else
sharepointFolder = "[SHAREPOINTMAINFOLDER]"
End If
sharepointFileName = sharepointUrl & sharepointFolder & f.Name
PstrFullfileName = strFilePath & f.Name
LlFileLength = FileLen(PstrFullfileName) - 1
' Read the file into a byte array.
ReDim Lvarbin(LlFileLength)
Open PstrFullfileName For Binary As #1
Get #1, , Lvarbin
Close #1
' Convert to variant to PUT.
LvarBinData = Lvarbin
PstrTargetURL = sharepointUrl & sharepointFolder & f.Name
' Put the data to the server, false means synchronous.
LobjXML.Open "PUT", PstrTargetURL, False
' Send the file in.
LobjXML.Send LvarBinData
End If
NextF:
Next f
Set LobjXML = Nothing
Set fso = Nothing
End Sub
I'd not closed the request to the server, d'oh!
Setting it up in a separate instance solved it for me.
I didn't convert the filename to binary and then to variant, merely kept it as a string.
You must omit the NOTHING from the last LobjXML.SEND given in LastCoder's example. Adding this in reproduces the Run-time error I give above.
Thanks for the help, LastCoder.
Here's the amended code:
Public Sub DeleteFromSharePoint()
Dim xmlhttp
Dim sharepointUrl, sharepointFolder, sharepointFileName
Dim f, strZip As String
Dim LobjXML As Object
' Parent Sharepoint
sharepointUrl = "[SHAREPOINT URL]"
' In this test module, we're just deleting from the parent directory
sharepointFolder = ""
' Sets the report name we want to remove
f = "test"
' Sets the full .ZIP filename
' This is how reports are archived by date
strZip = f & "%20-%20" & Format(Now() - 1, "YYYY.MM.DD") & ".zip"
Set LobjXML = CreateObject("Microsoft.XMLHTTP")
sharepointFileName = sharepointUrl & sharepointFolder & strZip
' Removes the data from the server, false means synchronous
LobjXML.Open "DELETE", sharepointFileName, False
' Sends the request to remove the file
LobjXML.Send
Set LobjXML = Nothing
End Sub
I'm trying to upload files to SharePoint using VBA within Excel.
I found some urlmon code which has solved the file download.
I've seen code which focuses on Scripting.FileSystemObject using UNC, winhttp POST and SEND and the SP SDK but I've not been able to make the latter work due to site and software install limitations.
I need to directly upload, for e.g. to "http://example.com/foldername". I tried using Scripting.FileSystemObject with the URL.
I'm making a bold assumption that there is a VBA method other than UNC and winhttp POST/SEND for uploading files to SharePoint.
Code I've tried, copied from someone else's work on Stack Overflow.
Public Function UploadEICRs(ByVal file As String, uploadFolder As String)
Dim SharepointAddress As String
Dim LocalAddress As String
Dim objNet As Object
Dim FS As Object
' Where you will enter Sharepoint location path
SharepointAddress = "https://example.com/test_folder/"
' Where you will enter the file path, ex: Excel file
LocalAddress = file
SPFolder = SharepointAddress & uploadFolder & "/"
Debug.Print SPFolder
Set objNet = CreateObject("WScript.Network")
Set FS = CreateObject("Scripting.FileSystemObject")
If FS.FileExists(LocalAddress) Then
FS.CopyFile LocalAddress, SPFolder
End If
Set objNet = Nothing
Set FS = Nothing
End Function
Sub uploadFiles()
Dim FileSystem As Object
Dim HostFolder As String
HostFolder = GetFolder
Set FileSystem = CreateObject("Scripting.FileSystemObject")
DoFolder FileSystem.GetFolder(HostFolder)
End Sub
Sub DoFolder(folder)
Dim SubFolder
Dim LString As String
Dim LArray() As String
Dim CertFolder As String
Dim ufile As String
Dim pFolder As String
LString = folder
LArray = Split(LString, "\")
For Each SubFolder In folder.SubFolders
DoFolder SubFolder
Next
Dim file
For Each file In folder.Files
CertFolder = LArray(3)
pFolder = LArray(0) & "\" & LArray(1) & "\" & LArray(2)
Debug.Print CertFolder
Debug.Print file
Debug.Print pFolder
ufile = file
sendfile2 ufile, CertFolder, pFolder
Next
End Sub
Public Sub sendfile2(ByVal file As String, sUrl As String, fPath As String)
On Error GoTo err_Copy
Dim xmlhttp As MSXML2.XMLHTTP60
Dim sharepointUrl
Dim sharepointFileName
Dim tsIn
Dim sBody
Dim LlFileLength As Long
Dim Lvarbin() As Byte
Dim LobjXML As Object
Dim LstrFileName As String
Dim LvarBinData As Variant
Dim PstrFullfileName As String
Dim PstrTargetURL As String
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim fldr
Dim f
Dim pw As String
Dim UserName As String
Dim RetVal
Dim I As Integer
Dim totFiles As Integer
Dim Start As Date, Finish As Date
Debug.Print file
Debug.Print sUrl
sharepointUrl = "https://example.com/folder/folder"
Set LobjXML = CreateObject("Microsoft.XMLHTTP")
mypath = sharepointUrl & "/" & sUrl
Debug.Print mypath
LobjXML.Open "HEAD", mypath, False 'Check for Directory
LobjXML.Send
If LobjXML.StatusText = "NOT FOUND" Then
'Create directory if not there
LobjXML.Open "MKCOL", mypath, False
LobjXML.Send
End If
Set fldr = fso.GetFolder(fPath & "\" & sUrl)
Debug.Print fldr
totFiles = fldr.Files.Count
For Each f In fldr.Files
sharepointFileName = sharepointUrl & "/" & sUrl & "/" & f.Name
Debug.Print sharepointFileName
PstrFullfileName = fPath & "\" & sUrl & "\" & f.Name
LlFileLength = FileLen(PstrFullfileName) - 1
Debug.Print PstrFullfileName
' Read the file into a byte array.
If LlFileLength <> 0 Then
ReDim Lvarbin(LlFileLength)
Open PstrFullfileName For Binary As #1
Get #1, , Lvarbin
Close #1
End If
' Convert to variant to PUT.
LvarBinData = Lvarbin
PstrTargetURL = sharepointUrl & "/" & sUrl & "/" & f.Name
' Put the data to the server, false means synchronous.
LobjXML.Open "PUT", PstrTargetURL, False
' Send the file in.
LobjXML.Send LvarBinData
'End If
I = I + 1
'RetVal = SysCmd(acSysCmdSetStatus, "File " & I & " of " & totFiles & " copied...")
Next f
'RetVal = SysCmd(acSysCmdClearStatus)
Set LobjXML = Nothing
Set fso = Nothing
err_Copy:
If Err <> 0 Then
MsgBox Err & " " & Err.Description
End If
End Sub
Function GetFolder() As String
Dim fldr As FileDialog
Dim sItem As String
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Select a Folder"
.AllowMultiSelect = False
.InitialFileName = Application.DefaultFilePath
If .Show <> -1 Then GoTo NextCode
sItem = .SelectedItems(1)
End With
NextCode:
GetFolder = sItem
Set fldr = Nothing
End Function
You have to use the SharePoint API, in order to securely log in and add files to a document library. If you can make HTTP calls from your VBA code, then you can use the SharePoint REST API, or you can download the SharePoint 2013 Client Components SDK, and then reference the Client-Side Object Model (CSOM) .dlls from VBA. Beware that most of Microsoft's examples are in C#, but are adaptable to VB.
In the document I have a button to do a save as, this function takes a path and creates the filename based off a cell and the date. This has been working fine until a path came up that has a period in it, it will locate the path correctly but is no longer filling in the filename.
Sub SaveWorkbookAsNewFile()
Dim NewFileType As String
Dim NewFile As String
Dim newfilename As String
Dim cellname As String
Dim monthnum As String
Dim monthtxt As String
Dim daynum As String
Dim yearnum As String
Dim yeartxt As String
Dim SaveArea As String
Dim q As Long
If Worksheets.Count <= 6 Then MsgBox "You must run the report before saving it.", vbInformation, "Save Error": End
SaveArea = Sheet1.Range("K12")
cellname = Sheet1.Range("K20")
'********************************************************************
Dim objFSO As Object, objFolder As Object, objSubFolder As Object
Dim varDirectory As Variant
Dim flag As Boolean
Dim strDirectory As String, goodfolder As String
Dim NumMonth As Integer
NumMonth = 0
q = 0
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(SaveArea)
NumMonth = Month(Date)
For Each objSubFolder In objFolder.subfolders
If InStr(1, UCase(objSubFolder.Name), UCase(MonthName(NumMonth, True)), vbTextCompare) > 1 Then goodfolder = objSubFolder.Name: Exit For
Next objSubFolder
If Not goodfolder = "" Then SaveArea = SaveArea & goodfolder & "\"
'********************************************************************
monthnum = Month(Date)
monthtxt = UCase(MonthName(monthnum, True))
daynum = Day(Date)
yearnum = Year(Date)
yeartxt = Right(yearnum, 2)
newfilename = cellname & "-" & monthtxt & "-" & daynum & "-" & yeartxt
Application.ScreenUpdating = False ' Prevents screen refreshing.
NewFileType = "Excel Macro-Enabled Workbook (*.xlsm), *.xlsm," & _
"All files (*.*), *.*"
NewFile = Application.GetSaveAsFilename( _
InitialFileName:=SaveArea & newfilename, _
fileFilter:=NewFileType)
If NewFile <> "" And NewFile <> "False" Then
ActiveWorkbook.SaveAs FileName:=NewFile, _
FileFormat:=xlOpenXMLWorkbookMacroEnabled, _
CreateBackup:=False, _
ConflictResolution:=xlUserResolution
End If
Application.ScreenUpdating = True
End Sub
A working path (SaveArea) is as follows: \\TestServer\Test\Test\Standards\Test\Test 1\
A broken path (SaveArea) is as follows: \\TestServer\Test\Test\Standards\Test\Test. 1\
Both bring up the save as dialog, but the path with the period does not populate a filename. Is there a way to make this work when the path includes a period?
Edit: I've found a similar post here but it doesn't have a solution to fix the problem.
To get this to work, add the file extension to the InitialFileName parameter like below:
NewFile = Application.GetSaveAsFilename( _
InitialFileName:=SaveArea & newfilename & ".xlsm", _
fileFilter:=NewFileType)
I've written a macro that downloads zip files containing CSVs from a website. The downloading and unzipping is going perfectly, however when I try to loop through the CSVs searching for the occurrence of a specific string, the macro simply quits after opening about a thousand. There is no error message, it simply stops working, leaving the last CSV it was working on open.
Here is my code:
Sub OpenSearch()
Dim ROW, j As Integer
Workbooks.Open Filename:=FileNameFolder & FileListCSV(i)
For j = 1 To 7
ROW = 3
Do Until IsEmpty(Cells(ROW, 6))
If Cells(ROW, 6) = WantedID(j, 1) Then
MsgBox "WE HAVE A MATCH!"
End If
ROW = ROW + 1
Loop
Next j
Workbooks(FileListCSV(i)).Close False
Kill FileNameFolder & FileListCSV(i)
End Sub
I did not include the main module that calls the this sub and downloads and unzips the files, because on its own, that works perfectly. It only stops working when the sub I copied here is being called.
The Filename comes from a public variable defined in the main module, WantedID contains the strings I need to find in the CSVs.
I've tried to put Application.Wait in the first line, but it did not solve the problem. Also how far the macro gets is completely random. It never stops after the same number of CSVs opened and closed.
UPDATE: Here is the code (parent sub) for the downloading and unzipping. I did not come up with this on my own, but copied it from an online source I cannot recall:
Public FileListCSV(1 To 288) As String
Public i As Integer
Public FileNameFolder As Variant
Public WantedID As Variant
Sub DownloadandUnpackFile()
Dim myURL As String
Dim YearNUM As Integer
Dim MonthNUM As Integer
Dim StarMonth, EndMonth As Integer
Dim DayNUM As Integer
Dim YearSTR As String
Dim MonthSTR As String
Dim DaySTR As String
Dim FixURL As String
Dim TargetFileName As String
Dim FSO As Object
Dim oApp As Object
Dim Fname As Variant
Dim DefPath As String
Dim strDate As String
Dim StrFile As String
Dim FileList(1 To 288) As String
Application.ScreenUpdating = False
FixURL = "http://www.nemweb.com.au/REPORTS/ARCHIVE/Dispatch_SCADA PUBLIC_DISPATCHSCADA_"
WantedID = Range(Cells(2, 1), Cells(8, 1))
YearNUM = 2016
StarMonth = 12
EndMonth = 12
For YearNUM = 2015 To 2016
For MonthNUM = StarMonth To EndMonth
For DayNUM = 1 To 31
YearSTR = CStr(YearNUM)
If MonthNUM < 10 Then
MonthSTR = "0" & CStr(MonthNUM)
Else:
MonthSTR = CStr(MonthNUM)
End If
If DayNUM < 10 Then
DaySTR = "0" & CStr(DayNUM)
Else:
DaySTR = CStr(DayNUM)
End If
myURL = FixURL & YearSTR & MonthSTR & DaySTR & ".zip"
Cells(1, 1) = myURL
Dim WinHttpReq As Object
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
WinHttpReq.Open "GET", myURL, False
WinHttpReq.Send
myURL = WinHttpReq.ResponseBody
If WinHttpReq.Status = 200 Then
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
oStream.Write WinHttpReq.ResponseBody
TargetFileName = "C:\Users\istvan.szabo\Documents \Basslink\AEMO RAW DATA\RAWRAW\" & YearSTR & MonthSTR & DaySTR & ".zip"
oStream.SaveToFile (TargetFileName)
oStream.Close
End If
'try unzippin'
Fname = TargetFileName
If Fname = False Then
'Do nothing
Else
'Root folder for the new folder.
'You can also use DefPath = "C:\Users\Ron\test\"
DefPath = Application.DefaultFilePath
If Right(DefPath, 1) <> "\" Then
DefPath = DefPath & "\"
End If
FileNameFolder = "C:\Users\istvan.szabo\Documents\Basslink\AEMO RAW DATA\RAWRAW\" & YearSTR & MonthSTR & DaySTR
'Make the normal folder in DefPath
MkDir FileNameFolder
'Extract the files into the newly created folder
Set oApp = CreateObject("Shell.Application")
oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(Fname).items
On Error Resume Next
Set FSO = CreateObject("scripting.filesystemobject")
FSO.deletefolder Environ("Temp") & "\Temporary Directory*", True
i = 1
StrFile = Dir(FileNameFolder & "\")
Do While Len(StrFile) > 0
FileList(i) = FileNameFolder & "\" & StrFile
FileListCSV(i) = Left(StrFile, Len(StrFile) - 3) & "csv"
StrFile = Dir
i = i + 1
Loop
'unzip the unzipped
For i = 1 To 288
Fname = FileList(i)
If Fname = False Then
'Do nothing
Else:
DefPath = Application.DefaultFilePath
If Right(DefPath, 1) <> "\" Then
DefPath = DefPath & "\"
End If
FileNameFolder = "C:\Users\istvan.szabo\Documents\Basslink\AEMO RAW DATA\"
Set oApp = CreateObject("Shell.Application")
oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(Fname).items
On Error Resume Next
Set FSO = CreateObject("scripting.filesystemobject")
FSO.deletefolder Environ("Temp") & "\Temporary Directory*", True
Call OpenSearch
End If
Next i
End If
Next DayNUM
Next MonthNUM
StarMonth = 1
EndMonth = 5
Next YearNUM
Application.ScreenUpdating = True
End Sub
You could check the file without opening it. That would save you time and resources. Here is a quick draw of the code I would use:
Sub OpenSearch()
Dim ROW, j As Integer
Dim fileID
Dim buf As String
Dim tmp As Variant
Open FileNameFolder & FileListCSV(i) For Input As #1
For j = 1 To 7
ROW = 3
Do Until EOF(1)
Line Input #1, buf
'Remove double quotes
buf = Replace(buf, """", "")
'Split line to a array
tmp = Split(buf, ",")
'5 is the 6th column in excel tmp index starts with 0
fileID = tmp(5)
If fileID = WantedID(j, 1) Then
MsgBox "WE HAVE A MATCH!"
End If
ROW = ROW + 1
Loop
Next j
Close #1
Kill FileNameFolder & FileListCSV(i)
End Sub
EDIT: Also try to add a resource cleanup code, for example: Set WinHttpReq = Nothing, Set oStream = Nothing etc.
In line with other advice in the comments: -
You should close of resources when you are done with them using Set WinHttpReq = Nothing for example. This can avoid memory problems that are similar to the issue you are seeing.
It is also advisable to remove On Error Resume Next. This is hiding errors and you may well be missing results that you need. It would also allow for more information during errors.
I took your two code blocks and wrote them into one that I believe will be stable during running and make it to the end, Run this and let us know if it did resolve the issue. I did it this way as there was a lot of small changes that went towards what I suspect will be more stable and make it to the end.
Sub DownloadandUnpackFile()
Dim FSO As New FileSystemObject
Dim DteDate As Date
Dim Fl As File
Dim Fl_Root As File
Dim Fldr As Folder
Dim Fldr_Root As Folder
Dim LngCounter As Long
Dim LngCounter2 As Long
Dim oApp As Object
Dim oStream As Object
Dim oWinHttpReq As Object
Dim RngIDs As Range
Dim StrURL As String
Dim StrRootURL As String
Dim VntFile As Variant
Dim VntFolder As Variant
Dim VntRootFile As Variant
Dim VntRootFolder As Variant
Dim WkBk As Workbook
Dim WkSht As Worksheet
'This will speed up processing, but you might not see progress while it is working
Application.ScreenUpdating = False
'Set variables
StrRootURL = "http://www.nemweb.com.au/REPORTS/ARCHIVE/Dispatch_SCADA/PUBLIC_DISPATCHSCADA_"
'You should be a little more explicit here for clarity, refernce a worksheet
'E.g. StrID = ThisWorkbook.Worksheets("Sheet1").Range(Cells(2, 1), Cells(8, 1))
Set RngIDs = Range(Cells(2, 1), Cells(8, 1))
Set oWinHttpReq = CreateObject("Microsoft.XMLHTTP")
Set oApp = CreateObject("Shell.Application")
'Loop from 21/Feb/2015 to today
For DteDate = CDate("21/Feb/2015") To Date
StrURL = StrRootURL & Format(DteDate, "YYYYMMDD") & ".zip"
Debug.Print StrURL
oWinHttpReq.Open "GET", StrURL, False
oWinHttpReq.Send
StrURL = oWinHttpReq.ResponseBody
If oWinHttpReq.Status = 200 Then
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
oStream.Write oWinHttpReq.ResponseBody
VntRootFile = Environ("UserProfile") & "\Documents\Basslink\AEMO RAW DATA\RAWRAW\" & Format(DteDate, "YYYYMMDD") & ".zip"
oStream.SaveToFile VntRootFile
oStream.Close
Set oStream = Nothing
VntRootFolder = Environ("UserProfile") & "\Documents\Basslink\AEMO RAW DATA\RAWRAW\" & Format(DteDate, "YYYYMMDD") & "\"
FSO.CreateFolder VntRootFolder
oApp.Namespace(VntRootFolder).CopyHere oApp.Namespace(VntRootFile).Items
Set Fldr_Root = FSO.GetFolder(VntRootFolder)
'Unzip the zipped zips
For Each Fl_Root In Fldr_Root.Files
If Right(LCase(Fl_Root.Name), 4) = ".zip" Then
VntFolder = Fl_Root.ParentFolder & "\" & Left(Fl_Root.Name, Len(Fl_Root.Name) - 4) & "\"
FSO.CreateFolder VntFolder
VntFile = Fl_Root.Path
oApp.Namespace(VntFolder).CopyHere oApp.Namespace(VntFile).Items
Set Fldr = FSO.GetFolder(VntFolder)
For Each Fl In Fldr.Files
If Right(LCase(Fl.Name), 4) = ".csv" Then
Set WkBk = Application.Workbooks.Open(Fl.Path)
Set WkSht = WkBk.Worksheets(1)
For LngCounter = 1 To RngIDs.Rows.Count
LngCounter2 = 1
Do Until WkSht.Cells(LngCounter2, 6) = ""
If WkSht.Cells(LngCounter2, 6) = RngIDs.Cells(LngCounter, 1) Then
Debug.Print "FOUND: " & Fl.Name & ": " & WkSht.Cells(LngCounter2, 6).Address
End If
LngCounter2 = LngCounter2 + 1
Loop
Next
Set WkSht = Nothing
WkBk.Close 0
Set WkBk = Nothing
End If
DoEvents
Next
Set Fldr = Nothing
End If
Next
Fldr_Root.Delete True
Set Fldr_Root = Nothing
FSO.DeleteFile VntRootFile, True
End If
DoEvents
Next
Set oApp = Nothing
Set oWinHttpReq = Nothing
Set RngIDs = Nothing
Application.ScreenUpdating = True
End Sub
Changes I have made: -
I used early binding to FileSystemObject simply to make it easier
to write up. You will need the 'Windows Scripting Runtime' reference
added (Tools > References > tick 'Windows Scripting Runtime')
I iterated through dates as a single loop rather then three loops of
strings working as a date
I set IDs to be a range and note a variant
I opened references once, reuse them (i.e. oApp), and then close
them
I added DoEvents to give time back to the computer to run anything it
may need to, this maintains a health system.
I used Debug.Print to add information to the immediate window instead
of msgbox, but you should really list the finds out in a separate
worksheet, (debug.print has a size limit so you may end up only
seeing X number of results as others are truncated off.