Uploading a file to SharePoint, not using UNC or drive mapping - excel

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.

Related

Run-time error '52' (Bad file name or number) - Excel VBA createFolder - Mircosoft teams drive

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

Using kill() - VBA script error with sharepoint path not Windows Path [duplicate]

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

Select a previous file in a folder and copy it to a new one

I need to do the following:
Allow the user to select any number of files, in any format, and copy them to a new folder.
Create the destination folder if it doesn't exist. In this case, the folder name should be given by the content of the C2 & C3 cells (Range("C2").Value & Range("C3").Text & "\").
Private Sub CommandButton4_Click()
Dim strDirname As String
Dim strDefpath As String
Dim strPathname As String
Dim strFilename As String
Dim FSO
Dim sFile As FileDialog
Dim sSFolder As String
Dim sDFolder As String
strDirname = Range("C2").Value & Range("C3").Text
MkDir strDefpath & "\" & strDirname
strPathname = strDefpath & "\" & strDirname & "\" & strFilename
Set sFile = Application.FileDialog(msoFileDialogOpen)
sDFolder = strDirname & "\"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set FSO = New FileSystemObject
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
If Not .Show Then Exit Sub
Set xFolder = FSO.GetFolder(.SelectedItems(1))
For Each xFile In xFolder.Files
On Error Resume Next
xRow = Application.Match(xFile.Name, Range("A:A"), 0)
On Error GoTo 0
Next
End With
End Sub
I know the error is here...
Set xFolder = FSO.GetFolder(.SelectedItems(1))
...because I'm asking it to get a file, not a folder.
It is not very clear to me what you are trying to do but, if you intend to select a folder, you have to use it
Application.FileDialog (msoFileDialogFolderPicker)
instead of
Application.FileDialog (msoFileDialogFilePicker)
Your posted code shows so little resemblance to what you Q asks for, I've disregarded it.
This code follows the description. You may need to alter certain details to fully match your needs
Sub Demo()
Dim FilePicker As FileDialog
Dim DefaultPath As String
Dim DestinationFolderName As String
Dim SelectedFile As Variant
Dim DestinationFolder As Folder
Dim FSO As FileSystemObject
DefaultPath = "C:\Data" ' <~~ update to suit, or get it from somewhere else
' Validate Default Path
If Right$(DefaultPath, 1) <> Application.PathSeparator Then
DefaultPath = DefaultPath & Application.PathSeparator
End If
If Not FSO.FolderExists(DefaultPath) Then Exit Sub
' Get Destination Folder, add trailing \ if required
DestinationFolderName = Range("C2").Value & Range("C3").Value
If Right$(DestinationFolderName, 1) <> Application.PathSeparator Then
DestinationFolderName = DestinationFolderName & Application.PathSeparator
End If
Set FSO = New FileSystemObject
' Get reference to Destination folder, create it if required
If FSO.FolderExists(DefaultPath & DestinationFolderName) Then
Set DestinationFolder = FSO.GetFolder(DefaultPath & DestinationFolderName)
Else
Set DestinationFolder = FSO.CreateFolder(DefaultPath & DestinationFolderName)
End If
' File Selection Dialog
Set FilePicker = Application.FileDialog(msoFileDialogFilePicker)
With FilePicker
.AllowMultiSelect = True ' allow user to select multiple files
.InitialFileName = DefaultPath ' set initial folder for dialog
If .Show = False Then Exit Sub ' check if user cancels
For Each SelectedFile In .SelectedItems ' loop over selected files
If SelectedFile Like DefaultPath & "*" Then 'Optional: disallow browsing higher than default folder
FSO.CopyFile SelectedFile, DefaultPath & DestinationFolderName, True ' Copy file, overwrite is it exists
End If
Next
End With
End Sub

Recursive Search Through Subfolders BACK to Root Directory

I have a function that works to search through the subfolders of a given directory and finds the file name I need. However, it only goes through one set of subfolders, finding the first one and then going through to the end of the subfolders. However, it then just stops. I have looked through various threads and tried different options but no joy.
I need it to then loop back to the root directory (say, sPath=C:\Windows) and look at the next subfolder, go through that whole directory, come back to the root folder, and so on until it finds the file it needs. I cannot seem to get that part to work, hoping someone here can help point out what I am missing. I am trying to keep this set at a higher level root folder rather than have to start lower in in the directory to get it to work. Here is the function:
Function recurse(sPath As String, strname As String, strName3 As String)
Dim FSO As New FileSystemObject
Dim myFolder As Scripting.Folder
Dim mySubFolder As Scripting.Folder
Dim myFile As Scripting.file
Dim strJDFile As String
Dim strDir As String
Dim strJDName As String
Set myFolder = FSO.GetFolder(sPath)
' strName = Range("a2").Offset(0, 3)
strName3 = Replace(strName3, "/", " ")
For Each mySubFolder In myFolder.SubFolders
Debug.Print " mySubFolder: " & mySubFolder
For Each myFile In mySubFolder.Files
If "*" & myFile.Name & "*" Like "*" & strName3 & "*" Then
strJDName = myFile.Name
strDir = mySubFolder & "\"
strJDFile = strDir & strJDName
recurse = strJDFile
Exit Function
Else
Debug.Print " myFile.name: " & myFile.Name
End If
Next
recurse = recurse(mySubFolder.Path, strname, strName3)
Next
End Function
Here is a routine you may be able to adapt to your use, if you are running Excel under Windows.
Pick a base folder using the Excel folder picker routine
Enter a file name mask (eg: Book1.xls*)
Uses the Dir command window command to check all the folders and subfolders for files that start with Book1.xls
The results of the command are written to a temporary file (which is deleted at the end of the macro)
There is a way to write it directly to a VBA variable, but I see too much screen flicker when I've done that.
The results are then collected into a vba array, and written to a worksheet, but you can do whatever you want with the results.
Option Explicit
'set references to
' Microsoft Scripting Runtime
' Windows Script Host Object model
Sub FindFile()
Dim WSH As WshShell, lErrCode As Long
Dim FSO As FileSystemObject, TS As TextStream
Dim sTemp As String
Dim sBasePath As String
Dim vFiles As Variant, vFullList() As String
Dim I As Long
Dim sFileName As String
sTemp = Environ("Temp") & "\FileList.txt"
'Select base folder
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
If .Show = -1 Then 'if OK is pressed
sBasePath = .SelectedItems(1)
Else
Exit Sub
End If
End With
'File name mask
sFileName = InputBox("Entire File Mask", "File Finder")
Set WSH = New WshShell
lErrCode = WSH.Run("CMD /c dir """ & sBasePath & "\*" & sFileName & """ /A-D /B /S > " & sTemp, xlHidden, True)
If Not lErrCode = 0 Then
MsgBox "Problem Reading Directory" & _
vbLf & "Error Code " & lErrCode
Exit Sub
End If
Set FSO = New FileSystemObject
Set TS = FSO.OpenTextFile(sTemp, ForReading, False, TristateFalse)
vFiles = Split(TS.ReadAll, vbLf)
TS.Close
FSO.DeleteFile sTemp
Set FSO = Nothing
Set WSH = Nothing
ReDim vFullList(1 To UBound(vFiles), 1 To 1)
For I = 1 To UBound(vFiles)
vFullList(I, 1) = vFiles(I)
Next I
Dim rDest As Range
Set rDest = Cells(1, 2).Resize(UBound(vFullList, 1), UBound(vFullList, 2))
With rDest
.EntireColumn.Clear
.Value = vFullList
.EntireColumn.AutoFit
End With
End Sub

Deleting Sharepoint Files using XMLHTTP

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

Resources