PDA

View Full Version : Solved: Code delete .pdf file don't work



niwat2811
12-19-2012, 10:50 PM
My code for delete .pdf file don't work. How can I fix it.
Sub DeletePDF1()
Dim rs As String
Dim objFSO, fso
Set objFSO = CreateObject("Scripting.FileSystemObject")
rs = Sheets("Sheet1").Range("I8")
Set mainFolder = objFSO.GetFolder("D:\SavePDF\")
For Each myFile In mainFolder.Files
If myFile.Name Like rs & ".pdf" Then
Kill ("D:\SavePDF\myFile")
End If
Next
End Sub

mohanvijay
12-19-2012, 11:20 PM
use

"*.pdf"

niwat2811
12-19-2012, 11:28 PM
Still does not work.

mohanvijay
12-20-2012, 02:35 AM
i did not see the rs

What is in the rs variable? the file name for delete

niwat2811
12-20-2012, 03:02 AM
Yes, it is the name of the file for deletion.

mohanvijay
12-20-2012, 04:56 AM
Change like below

Kill ("D:\SavePDF\" & myFile.Name)


you can also use the following code

Dim rs As String
rs = Sheets("Sheet1").Range("I8") & ".pdf"
On Error Resume Next
Kill rs
On Error GoTo 0

niwat2811
12-20-2012, 07:51 AM
don't work

Kenneth Hobs
12-20-2012, 02:48 PM
Sub KillPDFs()
Dim path As String
path = "x:\pdf\test"
CreateObject("Wscript.Shell").Exec ("cmd /c del " & path & "\*.pdf")
End Sub

niwat2811
12-20-2012, 06:14 PM
I have a folder named SavePDF located at D: \ SavePDF and each file in the folder as a pdf file 1111.pdf, 2222.pdf, 3333.pdf, 4444.pdf, 5555.pdf., And when I open the workbook excel. where sheet1 if I input I8 as 2222. DeletePDF1 name and press run macro to delete a file which is named 2222.pdf D: \ SavePDF.

Kenneth Hobs
12-20-2012, 07:24 PM
You lost me. Are you saying that now you just want to delete a file with it's base name in a named range called DeletePDF1? IF so, a simple Kill will do:

Kill "D:\SavePDF\" & Range("DeletePDF1").Value & ".pdf"

OF course it is best to see if the drive, folder, and file exists before you use Kill(). To do that, the Dir() can be use and if "" then it does not exist.

niwat2811
12-21-2012, 01:15 AM
Hi Kenneth Hobs (http://www.vbaexpress.com/forum/member.php?u=3661)

Thanks very much for your time. It's working
Thanks very much again.

Aussiebear
12-22-2012, 05:27 AM
If a thread you create has been solved to your satisfaction, than simply use the thread tools drop down to mark the thread as solved. I have done this for you this time