PDA

View Full Version : Solved: Clear Word docs



Dave
11-11-2006, 06:58 AM
Using XL VBA, I've been wondering if their is a quicker way to clear all Word documents in a folder. Here's the code I'm currently using. It seems like their should be some code like.. for each doc in folder clear contents without opening and saving.. or to that effect. Perhaps this is the only way? Any help will be appreciated. Dave


Sub Cleardocs2()
'Opens "Sdir" folder and clears all Word docs
Dim Sdir As String, f As Variant
Dim wdapp As Object, a As Date, b As Date
a = Now()
Sdir = "C:\Records" 'folder DIR

With Application.FileSearch
.NewSearch
.LookIn = Sdir
.FileType = msoFileTypeWordDocuments
.Execute
Set wdapp = CreateObject("Word.Application")
For Each f In .FoundFiles
wdapp.documents.Open Filename:=f
With wdapp.activedocument
.Range(0, .Characters.Count).Delete
End With
wdapp.activedocument.Close savechanges:=True
Next f
End With
wdapp.Quit
Set wdapp = Nothing
b = Now()
MsgBox "Files cleared. Time: " & Format(b - a, "h:mm:ss")
Exit Sub

ErFix:
On Error GoTo 0
MsgBox "File error"
wdapp.Quit
Set wdapp = Nothing
End Sub

lucas
11-11-2006, 09:36 AM
Hi Dave,
This works as is and I don't think you can clear the docs without opening and performing the clear operation and then saving them as you are doing here. Maybe someone else has a differing opinion.

Dave
11-11-2006, 10:01 AM
I've thought of a better plan. I will just copy the folder, which originally contains blank documents, to a backup folder. Then when I need a new folder full of blank docs, I'll copy the backup folder to the original folder. Probably instantly! It was taking 19 or so secs to clear 81 docs....too long. I'll mark this solved. Dave
edit: Thanks Lucas. If anyone has a solution or opinion please post.