Consulting

Results 1 to 3 of 3

Thread: Solved: Clear Word docs

  1. #1
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    836
    Location

    Solved: Clear Word docs

    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

    [VBA]
    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
    [/VBA]

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    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.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    836
    Location
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •