Delete PDF after sending (1 Viewer)

Waddy

Registered User.
Local time
Today, 23:06
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
 

Isaac

Lifelong Learner
Local time
Today, 16:06
Joined
Mar 14, 2017
Messages
8,738
Kill Application.CurrentProject.Path & "\PalletIssue_" & todayDate & ".pdf"
 

theDBguy

I’m here to help
Staff member
Local time
Today, 16:06
Joined
Oct 29, 2018
Messages
21,358
Hi. You might also consider putting a delay in case the code finish processing before the computer does. Just a thought...
 
Last edited:

Waddy

Registered User.
Local time
Today, 23:06
Joined
Nov 26, 2018
Messages
32
Thank you, I will give that a shot :)
 

Waddy

Registered User.
Local time
Today, 23:06
Joined
Nov 26, 2018
Messages
32
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:)
 

Waddy

Registered User.
Local time
Today, 23:06
Joined
Nov 26, 2018
Messages
32
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.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 16:06
Joined
Oct 29, 2018
Messages
21,358
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.
 

Gasman

Enthusiastic Amateur
Local time
Today, 23:06
Joined
Sep 21, 2011
Messages
14,048
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.?
 

Waddy

Registered User.
Local time
Today, 23:06
Joined
Nov 26, 2018
Messages
32
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.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 16:06
Joined
Oct 29, 2018
Messages
21,358
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

Top Bottom