Kill file code not deleting the file in question

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,226
Office Version
  1. 2007
Platform
  1. Windows
Hi,
My code has saved a pdf file in a specified folder.
The code then activates another worksheet in the same workbook.
Some operations are now done.
A userform now opens & i require when i click the OK button to kill / delete the file that was saved as mentioned above.

This is the userform code.
When i click OK on the userform i check in the folder & the file in questioned hasnt been killed / deleted.

Do you see an issue with this code please

Rich (BB code):
Private Sub CloseForm_Click()
Dim MyFile As String
Unload ValueInInvoiceCell

 MyFile = "C:\Users\Ian\Desktop\REMOTES ETC\DR\DR SCREEN SHOT PDF\" & Range("G13").Value & ".pdf"
    If Dir(MyFile) <> "" Then Kill MyFile 'SAVED INVOICE HAS NOW BEEN DELETED

End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Maybe cell G13 stores the file name with an extension.
Also check if the pdf file is closed.

Artik
 
Upvote 0
OK
So testing this i see an issue.

The worksheet that has the value in cell G13 i put a command button on that page & when i clicked it the file was deleted.

So i go to the worksheet that the userform is on & do the same BUT it doesnt get deleted.

Does this tell you anything ?
 
Upvote 0
Notation:
VBA Code:
Range("G13").Value
refers to cell G13 in the ACTIVE sheet. If the cell is in another sheet when the form is displayed, add the full reference:
VBA Code:
Worksheets("Sheet Name").Range("G13").Value

Artik
 
Upvote 0
Solution
Could you put the code here i need to use as i understand what youve advised but lost with doing it
Thanks
 
Upvote 0
Got it many thanks


Rich (BB code):
Private Sub CloseForm_Click()
Dim MyFile As String

 MyFile = "C:\Users\Ian\Desktop\REMOTES ETC\DR\DR COPY INVOICES\" & Worksheets("INV").Range("G13").Value & ".pdf"
If Dir(MyFile) <> "" Then Kill MyFile 'SAVED INVOICE HAS NOW BEEN DELETED
Unload ValueInInvoiceCell
End Sub
 
Upvote 0
Hi,
Please advise on this.
I used the same code on a command button on the Active sheet BUT the file was not deleted even though i see the Msgbox Generatedpdf deleted.
I also tried the original code & Range("G13").Value & ".pdf" & it did the same.

I have check file name & path & both ok

Rich (BB code):
Else
     Range("L4").Value = Range("L4").Value + 1
     Range("G27:L36").ClearContents
     Range("G46:G50").ClearContents
     Range("L18").ClearContents
     Range("G13").ClearContents
     Range("G13").Select
     ActiveWorkbook.Save
    
     MyFile = "C:\Users\Ian\Desktop\REMOTES ETC\DR\DR COPY INVOICES\" & Worksheets("INV").Range("G13").Value & ".pdf"
    If Dir(MyFile) <> "" Then Kill MyFile 'GENERATED PDF HAS NOW BEEN DELETED
    MsgBox "Generated pdf deleted"
    End If
    End With
    End With
   


End Sub
 
Upvote 0
I moved the code up for deleting pdf & now working.
The code above was clearing field so the kill code then couldnt see value in cell G13
 
Upvote 0

Forum statistics

Threads
1,215,102
Messages
6,123,099
Members
449,096
Latest member
provoking

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