|
|
Our Hot Pick: Rising Antivirus 2006 - Certified by TUV & Checkmark! Get 10% discount by entering this coupon code: ONDISCOUNT10
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
|
|
Appoliges I meant delete file from folder and not FOLDER Pete wrote:
[Quoted Text] > 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
|
|
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"
|
|
Thanks Helmut
Just a quick question
will your code fit in with my existing code as this currently works well?
Helmut Weber wrote:
[Quoted Text] > 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"
|
|
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"
|
|
Any sugestions how I get my existing code to carryout delete file function? Helmut Weber wrote:
[Quoted Text] > 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"
|
|
Hi
I have managed to get it working utilising some of your code many thanks Pete wrote:
[Quoted Text] > Any sugestions how I get my existing code to carryout delete file > function? > Helmut Weber wrote: > > > 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"
|
|
|