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
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