Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Delete word documents by VBA

Posted on 2004-10-08
10
Medium Priority
?
385 Views
Last Modified: 2008-02-01
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
Comment
Question by:ishadowme
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 41

Accepted Solution

by:
shanesuebsahakarn earned 150 total points
ID: 12264216
To delete all .doc objects in a folder:

Kill "C:\MyFolder\*.doc"
0
 

Author Comment

by:ishadowme
ID: 12264384
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
 
LVL 41

Expert Comment

by:shanesuebsahakarn
ID: 12264392
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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

by:ishadowme
ID: 12264515

Shane,

It there a way to close all opening files at once?
0
 
LVL 41

Expert Comment

by:shanesuebsahakarn
ID: 12264522
Depends...how did you open them? Manually, or by using code?
0
 

Author Comment

by:ishadowme
ID: 12264539
I opened them by VBA
0
 
LVL 41

Expert Comment

by:shanesuebsahakarn
ID: 12264549
Please post the code that you have used.
0
 

Author Comment

by:ishadowme
ID: 12264568
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
 
LVL 9

Assisted Solution

by:Bat17
Bat17 earned 225 total points
ID: 12277493
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
 

Author Comment

by:ishadowme
ID: 12282159
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question