Delete PDF after sending

Waddy

Registered User.
Local time
Today, 08:03
Joined
Nov 26, 2018
Messages
32
Hi,

I hope someone can help, below is code that pdfs the current form and puts in this in an email using outlook to the users inputted into the code after clicking send on the form.

At present this leaves the PDF file in the directory the access file is located. Is there some code I could add at the end, so once sent it will delete the pdf file in that directory.

Function Palletemail()
Dim myOb As Object
Dim oMail As Object
Dim AutoSend As Boolean
Dim fileName As String, todayDate As String

Set myOb = CreateObject("Outlook.Application")
Set oMail = myOb.CreateItem(olMailItem)


'Export report in same folder as db with date stamp
todayDate = Format(Date, "DDMMYYYY")
fileName = Application.CurrentProject.Path & "\PalletIssue_" & todayDate & ".pdf"
DoCmd.OutputTo acForm, "frmPalletIssueFormWarehouse", acFormatPDF, fileName, False
'Set whether to send email automatically or make user press Send
AutoSend = True

With oMail
.Body = "Message"
.Subject = "PalletIssue"
.To = "user@email.com"
.CC = "user2@email.com"
.Attachments.Add fileName

If AutoSend Then
.Send
Else
.Display
End If
End With

Set oMail = Nothing
Set oApp = Nothing
End Function
 
Kill Application.CurrentProject.Path & "\PalletIssue_" & todayDate & ".pdf"
 
Hi. You might also consider putting a delay in case the code finish processing before the computer does. Just a thought...
 
Last edited:
Thank you, I will give that a shot :)
 
I am having a problem :), I think the code works. As you mentioned I have issues with it deleting before it sends as I get an error on the output line of the code. I tried to delay by running a separate function after the ok button on a message box was pressed via marcos after sending. I was thinking it would just follow that order but it does not. I hope this is not as clear as mud:)
 
DoCmd.OutputTo acForm, "frmPalletIssueFormWarehouse", acFormatPDF, fileName, False

Error on this line, as soon as you take delete function or code out it all works as it should.
 
I am having a problem :), I think the code works. As you mentioned I have issues with it deleting before it sends as I get an error on the output line of the code. I tried to delay by running a separate function after the ok button on a message box was pressed via marcos after sending. I was thinking it would just follow that order but it does not. I hope this is not as clear as mud:)
Hi. Based on what I see in your code, have you considered using the SendObject method instead? Just curious...

Otherwise, you'll have to find a good spot to add the delay, or maybe even a "check" to make sure it's safe to delete the file.
 
I *thought* when you attached a file, a copy was placed in the temp user folder for the user?, as you can modify the original and it will not be reflected in in the attached version.?
 
Hi. Based on what I see in your code, have you considered using the SendObject method instead? Just curious...

Otherwise, you'll have to find a good spot to add the delay, or maybe even a "check" to make sure it's safe to delete the file.
I have used the sendObject function but this does not work for some users (most) but it did not work for a couple.

This method works for everyone.
 
I have used the sendObject function but this does not work for some users (most) but it did not work for a couple.

This method works for everyone.
I see. Then, like I said, you'll have to find the spot where you can check if it's safe to delete the file or not. Maybe you can check if the email has already been sent. Just a thought...
 

Users who are viewing this thread

Back
Top Bottom