Delete file from SharePoint site using VBA?

jasonfish11

Board Regular
Joined
May 14, 2015
Messages
56
Hi all,
I've searched the internet trying to find a way to delete a file from a sharepoint site. I am struggling to make what I've found work, and honestly it's probably because I don't quite understand the coding and have something slightly off.

Essentially what I'm trying to do is "move" a file to a different folder when a checkbox in that file is changed (checkbox is assigned to cell G3). I understand I can't move a file that's open, but I figured I could save a new file then since the new file would be the active one, I could go back and delete the old file.

I am able to make all of this work when the file is not on SharePoint by using the coding of "Kill OldFile" but after extensive search online I've realized that doesn't work in SharePoint and I've tried various "solutions" that I've found to no success. Hopefully you all can point me in the right direction.

Here is what I have...

Code:
    Dim Filname As String    Dim FilPath As String
    Dim NewFilPath As String
    Dim OldFile As String
    Dim LobjXML As Object
 
    Filname = Sheets("Dashboard").Range("B12").Value & " " & Sheets("Dashboard").Range("B11").Value
    FilPath = ActiveWorkbook.Path
    OldFile = FilPath & "/" & Filname & ".xlsm"
    
    If FilPath = "http://teams/sites/CREW/AssociateMovementExcel/Sensitive" And Sheets("Dashboard").Range("G3") = False Then
        NewFilPath = Sheets("File Path").Range("B3").Value & Filname & ".xlsm"
        Application.DisplayAlerts = False
        ActiveWorkbook.SaveAs Filename:=NewFilPath, FileFormat:=52
[COLOR=#ff0000]        Set LobjXML = CreateObject("Microsoft.xmlhttp")[/COLOR]
[COLOR=#ff0000]            LobjXML.Open "Delete", OldFile, False[/COLOR]
[COLOR=#ff0000]            LobjXML.Send[/COLOR]
[COLOR=#ff0000]          Set LobjXML = Nothing[/COLOR]
        Application.DisplayAlerts = True
    End If

Everything works except the red text, and I'll be perfectly honest I pulled this from online and am not 100% certain what some of the coding means (like the "Microsoft.xmlhttp" and "Delete". I feel like I should have something else in those areas and that the poster expected someone to update those fields. I just don't know what they should be updated to.

Lastly I've confirmed "OldFile" does show the correct path and name of the file I'm trying to delete.

Any help is welcomed.

Thanks in advance.
 
Last edited:

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

Forum statistics

Threads
1,223,710
Messages
6,174,019
Members
452,542
Latest member
Bricklin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top