Delete all Word files but one via vba excel

jaenican

New Member
Joined
Mar 21, 2019
Messages
1
[FONT=&quot]I have several individual word files in a folder and one word file that is a merged version of all those word files. I would like to delete all the individual word files after the merge process, so only the Merged Word document remains. (I'm running this code via Excel, not Word)[/FONT]
[FONT=&quot]Any ideas on coding? I'm thankful for all replies![/FONT]
[FONT=&quot]I've been playing around with something like this but it doesn't work:[/FONT]
<code class="s14dydj4-7 cOzydF" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-stretch: inherit; font-size: 13px; line-height: 20px; font-family: "Noto Mono", Menlo, Monaco, Consolas; vertical-align: baseline; background: transparent; color: rgb(34, 34, 34); max-width: 100%; overflow: auto;"> Sub test()
Dim myFile As String
Dim mypath As String
Dim objWord As Object

mypath = ThisWorkbook.Worksheets(2).Cells(4, 2).Value & "" 'filepath

myFile = Dir(mypath & "*.docx", vbNormal + vbReadOnly + vbHidden) ' all docx files in the filepath
MergeOutput = mypath & "ConsolidatedMerge" & "_" & Format(Now, "d_mmm_yyyy_hh_mm_ss") 'name of merged file
While myFile.Name <> MergeOutput
myFile.Delete
myFile = Dir(mypath & "*.docx", vbNormal + vbReadOnly + vbHidden)
Wend
End Sub
</code>[FONT=&quot]Thanks in advance:)[/FONT]
[FONT=&quot]Annabelle[/FONT]
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
General outline...
Code:
Dim FS As Object, ObjFiles As Object
Set FS = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
Set ObjFiles = FS.GetFolder("Your Folder Path").Files
For Each FL In ObjFiles
If FL.Name <> MergeOutput Then
Kill FL
End If
Next FL
Set ObjFiles = Nothing
Set FS = Nothing
HTH. Dave
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,996
Members
452,373
Latest member
TimReeks

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