Deleting Source .CSV file after embedding it to worksheet via VBA


Hi…I’m trying to embed a .csv file to an excel sheet and then delete the source file using Windows 10 and Excel 2016. My code embeds the sourcefile to the worksheet ok but when it tries to delete the original .csv file I get a “Run-time error ’70’: Permission denied message. I’ve tried the same code using a .txt file instead and it seems to run fine. Is there any way to release the .csv file from excel so it can be deleted without having to convert my file to a .txt?


Public Sub EmbedFile()

    Dim oFile As OLEObject

    Dim fPath As String

    fPath = “C:TempTest.csv”

    Set oFile = ActiveSheet.OLEObjects.Add(Filename:=fPath, Link:=False, DisplayAsIcon:=True, IconIndex:=0, IconLabel:=fPath)

    Set oFile = Nothing

    Kill fPath

End Sub

By: Jeff Dahlquist

Leave a Reply

Your email address will not be published. Required fields are marked *