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

    Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » Closing active and deleting file & folder (Word 97 SP2)

    Author
    Topic
    #360926

    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

    Else
    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!
    myMailItem.send
    ‘ Close instance
    Set myOutlook = Nothing

    sent_message.Show

    End Sub

    Viewing 0 reply threads
    Author
    Replies
    • #544649

      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:

      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.
          Loop
          Finish = Timer    ' Set end time.
          TotalTime = Finish - Start    ' Calculate total time.
          MsgBox "Paused for " & TotalTime & " seconds"
      Else
          End
      End If
      

      Gary

    Viewing 0 reply threads
    Reply To: Closing active and deleting file & folder (Word 97 SP2)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: