Results 1 to 2 of 2
  1. #1
    Star Lounger
    Join Date
    Mar 2001
    St Georges du Bois, Pays de la Loire
    Thanked 0 Times in 0 Posts

    Closing active and deleting file & folder (Word 97 SP2)

    I have a macro that e-mails a form to recipients. The macro creates a hard-disc folder and saves to it so that the info on the form can be mailed. It is for use on an intranet.

    what I would like to do is to delate the saved file and it's folder from the hard-disc after use. However if I clase the doc after the e-mail the sub stops as well. The macro is below (with many thanks to Chris Rae and lots of tips from this forum).

    Any ideas please?

    Sub MailDoc()

    If Len(Dir("c:hold_file", vbDirectory)) > 0 Then

    MkDir "c:hold_file"
    End If

    ChangeFileOpenDirectory "C:hold_file"

    Kill "C:hold_file*.*"

    ActiveDocument.SaveAs FileName:="bsg401.doc"

    Dim myOutlook As Object
    Dim myMailItem As Object
    ' Make instance
    Set myOutlook = CreateObject("Outlook.Application")
    ' Make mail item
    Set myMailItem = myOutlook.createitem(0)
    ' Set recipient (internal mail)

    myMailItem.Recipients.Add "solomon, david"
    'myMailItem.Recipients.Add "Pearce, Muriele

    ' Set subject
    myMailItem.Subject = "Starters & Leavers - Leakage Contractors"
    ' Set body
    myMailItem.body = "The attached form BSG 401 contains Leakage Contractor personnel details for your attention."
    ' Set open doc as attachement
    myMailItem.Attachments.Add ActiveDocument.FullName
    ' And send it!
    ' Close instance
    Set myOutlook = Nothing


    End Sub

  2. #2
    Super Moderator
    Join Date
    Dec 2000
    New York, NY
    Thanked 29 Times in 27 Posts

    Re: Closing active and deleting file & folder (Word 97 SP2)

    Not sure I'm reading the code correctly (for instance, where is the statement that closes the doc after the e-mail is sent?) - anyway:

    First question is: where is the macro stored? - If you're storing the macro in either the document or in the template which the document is based on, then the expectation would be that the macro would stop running as soon as you close the document.
    In that case you'd want to store the macro in a global template or add-in so that the code can continue running after the document is closed.

    If you already have the macro in a global template or add-in, the problem might be that the Outlook send action which you've set into motion, doesn't have time to complete before you are deleting the file.
    The following is the Timer function example from VBA help - there have been some better examples of how to do this in the VB/VBA forum that would be worth a search for:

    <pre>Dim PauseTime, Start, Finish, TotalTime
    If (MsgBox("Press Yes to pause for 5 seconds", 4)) = vbYes Then
    PauseTime = 5 ' Set duration.
    Start = Timer ' Set start time.
    Do While Timer < Start + PauseTime
    DoEvents ' Yield to other processes.
    Finish = Timer ' Set end time.
    TotalTime = Finish - Start ' Calculate total time.
    MsgBox "Paused for " & TotalTime & " seconds"
    End If


Posting Permissions

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