• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 387
  • Last Modified:

Delete word documents by VBA

Hi,

I have access application does sorting records and then export and open them in word. Every time I export the records it will create a new file name myFile 1, myFile 2 and so on...My question is how can I delete all the documents after I'm done with them that include the one that not opening as well as opening. I assigned them in a folder called C:\myFolder. Is it possible to create a command to do all of that?

Thanks in Advance
Andrew  
0
ishadowme
Asked:
ishadowme
  • 5
  • 4
2 Solutions
 
shanesuebsahakarnCommented:
To delete all .doc objects in a folder:

Kill "C:\MyFolder\*.doc"
0
 
ishadowmeAuthor Commented:
Hi,

Shane, that works fine on all the files that not opening but it will have permission error when some files still open.

Thanks for your reply.
Andrew
0
 
shanesuebsahakarnCommented:
You'll have to close the ones that you have open first. You cannot delete open Word documents. If you want to just delete the ones that are not opened, either trap for the error or skip it:

On Error Resume Next
Kill ...
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
ishadowmeAuthor Commented:

Shane,

It there a way to close all opening files at once?
0
 
shanesuebsahakarnCommented:
Depends...how did you open them? Manually, or by using code?
0
 
ishadowmeAuthor Commented:
I opened them by VBA
0
 
shanesuebsahakarnCommented:
Please post the code that you have used.
0
 
ishadowmeAuthor Commented:
Shane,

Here is my code:

Dim rsFinalResults As DAO.Recordset
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Dim strCopyFile As String
Dim counter As Integer
Dim i As Integer
Dim sql As String

    counter = 1
   
    If Dir$("c:\myFolder", vbDirectory) = "" Then
        MkDir "c:\myFolder"
    End If
   
    If Dir$("C:\myFolder\myFile " & counter & ".txt") = "" Then
        strCopyFile = "C:\myFolder\myFile " & counter & ".txt"
    Else
        Do Until Dir$("C:\myFolder\myFile " & counter & ".txt") = ""
                counter = counter + 1
                strCopyFile = "C:\myFolder\myFile " & counter & ".txt"
        Loop
    End If
   
    Open strCopyFile For Output As #1
   
    sql = "SELECT * FROM tblfinalResults " & "ORDER BY FinalResults"
   
    Set rsFinalResults = CurrentDb.OpenRecordset(sql)
       
        rsFinalResults.MoveFirst
           
       
        Do While Not rsFinalResults.EOF
             Print #1, rsFinalResults!FinalResults;
            rsFinalResults.MoveNext
        Loop

        Close #1

    Set wrdApp = CreateObject("word.Application")
    wrdApp.Visible = True
    wrdApp.Documents.Open strCopyFile

    txtStart.SetFocus
    cmdCopy.Enabled = False

    rsFinalResults.Close
    Set rsFinalResults = Nothing
   
End Sub
0
 
Bat17Commented:
Set wrdApp = GetObject(, "word.Application")

wrdApp.Documents.Close False
wrdApp.Quit
Set wrdApp = Nothing

Should close ALL open word documents without saving them.

Peter
0
 
ishadowmeAuthor Commented:
Hi,

Thanks for your comments. Actually my goal is only closing those files are opened by this code, other documents still need to be opened. By using your codes and modify a bit I can make it work now.

Andrew
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now