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

Sinodus

New Member
Joined
Jan 8, 2019
Messages
2
[FONT=&quot]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?[/FONT]
[FONT=&quot]Public Sub EmbedFile()[/FONT]
[FONT=&quot] Dim oFile As OLEObject[/FONT]
[FONT=&quot] Dim fPath As String[/FONT]
[FONT=&quot] fPath = “C:TempTest.csv”[/FONT]
[FONT=&quot] Set oFile = ActiveSheet.OLEObjects.Add(Filename:=fPath, Link:=False, DisplayAsIcon:=True, IconIndex:=0, IconLabel:=fPath)[/FONT]
[FONT=&quot] Set oFile = Nothing[/FONT]
[FONT=&quot] Kill fPath[/FONT]
[FONT=&quot]End Sub[/FONT]
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Welcome to MrExcel forums.

That error occurs because the .csv file is still open as a workbook at the Kill command. The solution is to close it (Test.csv) before deleting it:
Code:
    Application.Workbooks("Test.csv").Close
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,632
Latest member
jladair

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