Folder delete code

P

Pete

New to this group so Hi to you all, Im after some help as Im new to
VBA/code and its adictive.

Im after help finishing the code below off, I would like to add a
commandbutton to allow the selected files to be deleted from the target
folder.

Private Sub CommandButton1_Click()


activeDir = "C:\my path"

documents.Open (activeDir & "\" & ListBox1.Value)
RAOPEN.hide




End Sub

Private Sub ListBox1_Click()

End Sub

Private Sub UserForm_Initialize()
Dim objFSO As Object, TargetFolder As Object, FC As Object

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set TargetFolder = objFSO.GetFolder("C:\my path")
Set FC = TargetFolder.Files
HowMany = FC.Count


For Each File In FC
ListBox1.AddItem (File.Name)
Debug.Print File.Name
Next File


End Sub

Regards

Pete
 
H

Helmut Weber

Hi Pete,

just to get you started.
this is the way, i would try it:

Option Explicit
Const sPath = "C:\test\word\"
' -------------------------
Private Sub CommandButton1_Click()
Documents.Open (sPath & ListBox1.Value)
Me.Hide
End Sub
' -------------------------
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim sFulname As String
With ListBox1
sFulname = sPath & .List(.ListIndex)
Kill sFulname
.RemoveItem .ListIndex
End With
End Sub
' -------------------------
Private Sub UserForm_Initialize()
Dim sFile As String
sFile = Dir(sPath, vbNormal)
While sFile <> ""
ListBox1.AddItem sFile
sFile = Dir
Wend
ListBox1.ListIndex = 0
End Sub

Lots of error handling is still missing.

Whether you want to use dir() or the filesystem.object,
is up to you. Some use it, some avoid it.

HTH

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
P

Pete

Thanks Helmut

Just a quick question

will your code fit in with my existing code as this currently works
well?
 
H

Helmut Weber

Probably not,

you would have to replace most of your code.

But adapting to your code
what's in the double-click event only,
shouldn't be too difficult.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top