PDF email from Access - email

I am trying to email a submission form based on the click of a command button. I have created the code to filter the form based on the 4 primary keys. But when I run the code the FleetID portion is pulling up as blank in the Immediate Pane. The FleetID portion is provided in a combobox. Can somebody help me?
Thanks
On Error GoTo errhandle
Me.Filter = "CurrentDate= #" & Format(Me!CurrentDate, "yyyy\-mm\-dd") & "# and DiscoverTime= '" & Me!DiscoverTime & "' And TailNumber= '" & Me!TailNumber & "' And FleetID= '" & Me!FleetID & "'"
Debug.Print Me.Filter
Me.FilterOn = True
DoCmd.SendObject acSendForm, "frmETIC", acFormatPDF, "EMAIL", "", "", "Recovery Report", "Attached is the submitted Recovery Report"
exiterr:
Exit Sub
errhandle:
If Err.Number <> 2501 Then
MsgBox ("Email cancelled!")
End If
Resume exiterr

Apart from my suggestions in the comment section. I would personally create a query and a desired report from that query. Reports gives you a very neat and professional look unlike forms with their extra controls.
First create a query same like your forms datasource
Create report out of that query. Design your report with your logo footer and all other stuffs you would like to have also the printing margin.
Generate your report with where condition and use the docmd.sendobject
Alternatively
Generate your report hidden with your where condition
Save the report as PDF file using docmd.outputTo
create new outlook email object and attach the PDF file
both ways have their own advantages. i personally use the second one because its much easier for me to customize the email content/template.
Here is a function to create emails:
Function SEND_EMAIL_MESSAGE(mTo As String, mCC As String, mBC As String, mSubject As String, mBody As String, Optional useOwnSignature As Boolean = False, Optional DisplayMsg As Boolean = False, Optional isHTML As Boolean = False, Optional AttachmentPath = "") As Boolean
' Please check the reference for Microsoft Outlook 14.0 object library for outlook 2010.
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookAttach As Outlook.Attachment
Dim mSignature As String
On Error GoTo ERROR_EMAIL
' Create the Outlook session.
Set objOutlook = New Outlook.Application
' Create the message.
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
With objOutlookMsg
' Add the To recipient(s) to the message.
.To = mTo
.CC = mCC
.BCC = mBC
.Subject = mSubject
If useOwnSignature Then .BodyFormat = olFormatHTML
.Display
If useOwnSignature Then
If isHTML Then
mSignature = .HTMLBody
.HTMLBody = mBody & mSignature
Else
mSignature = .Body
.Body = mBody & mSignature
End If
Else
.Body = mBody
End If
' Add attachments to the message.
If Not IsMissing(AttachmentPath) Then
Dim mFiles() As String
If (VBA.Right(AttachmentPath, 1)) <> ";" Then AttachmentPath = AttachmentPath & ";"
mFiles = VBA.Split(AttachmentPath, ";")
Dim i As Integer
For i = 0 To UBound(mFiles) - 1
If Not mFiles(i) = "" Then Set objOutlookAttach = .Attachments.Add(mFiles(i))
Next i
End If
' Should we display the message before sending?
If DisplayMsg Then
.Display
Else
.Send
End If
End With
SEND_EMAIL_MESSAGE = True
EXIT_ROUTINE:
On Error GoTo 0
Set objOutlook = Nothing
Set objOutlookMsg = Nothing
Exit Function
ERROR_EMAIL:
SEND_EMAIL_MESSAGE = False
GoTo EXIT_ROUTINE
End Function
and you here some code you can generate the report and send to email:
strReportName = "rpt_incident_view_single"
DoCmd.OpenReport strReportName, acViewPreview, , strCriteria, acHidden
Dim tmpPath As String
tmpPath = VBA.Environ("temp")
strMyPath = tmpPath
If VBA.Right(strMyPath, 1) = "\" Then
strMyPath = strMyPath & "_" & incident_id & "_" & VBA.Format(Now, "yyyy-dd-mm") & ".pdf"
Else
strMyPath = strMyPath & "\" & "_" & incident_id & "_" & VBA.Format(Now, "dd-mm-yyyy") & ".pdf"
End If
DoCmd.OutputTo acOutputReport, strReportName, acFormatPDF, strMyPath, False
after saving the report just send it to the email function which will create new email and show it to the user:
SEND_EMAIL_MESSAGE mTo, mCC, mBcc, mSubject, mBody,,,, strMyPath
DoCmd.Close acReport, strReportName
on error resume next
VBA.Kill strMyPath
just modify your code as per your needs. good luck :)

Related

EMAIL wont Outlook Email using Access form

MS Access 2019 form supplies data to send an email via Outlook 2019.
The recipient (email address) will not populate the email "To" box unless it is text, but will not if it is an email. When 'proving the VBA data transfer via msgbox, it will. I suspect it could be my dim declaration type but I have tried others such as string and variant(illustrated)
The code below will allow the MsgBox to run, but nothing else.
My form fields are named" 'Name' txtRecip, and 'EMail' txtEmail
Any ideas?
Thanks in advance
Fred
Private Sub btnSendEmail_Click()
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Dim objOutlookAttach As Outlook.Attachment
Dim objEmail As Variant
' Create the Outlook session.
Set objOutlook = CreateObject("Outlook.Application") ' Create the message.
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
With objOutlookMsg
' Add the To recipient(s) to the message.
objEmail = [txtEmail].Value
response = MsgBox(objEmail)
Set objOutlookRecip = .Recipients.Add(objEmail)
objOutlookRecip.Type = olTo
'response = MsgBox(txtName & Chr(13) & txtEmail)
' Add the CC recipient(s) to the message.
' Set objOutlookRecip = .Recipients.Add("")
' objOutlookRecip.Type = olCC
' Set the Subject, Body, and Importance of the message.
.Subject = "B2B - Brethren Outreach"
.Body = txtName.Value & "," & Chr(13) & Chr(13) & "

Email will not send through VBS script

I need to send a very simple email with only text to a few recipients, but I'm getting an error.
I don't have an SMTP server to send emails through, but I do have an outlook and I'm logged in through the desktop app.
Here's the script so far:
Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)
objMail.Display
objMail.to = "recipient#whatever.com"
objMail.Subject = "Test"
objMail.Body = "test"
objMail.Send
objOutlook.Quit
Set objMail = Nothing
Set objOutlook = Nothing
When the script is run, WSH gives the error
line: 10
char: 1
error: Operation Aborted
source: (null)
This is the objMail.Send line.
And my outlook pops up with the proper recipient/subject/body, but it doesn't send.
I can't find anything related to this issue or a work around besides using an SMTP server which as far as I know I can't do.
I have a function defined and in daily use which accepts the various items for creating and sending the email. Remember if you have to create your Outlook instance, you need to log on with the appropriate mail profile in order to send anything. The profile we use here is just called "Outlook". Check what yours is called and include the Namespace stuff I have in mine.
Dim sComputer : sComputer = "." ' selects local machine
Dim oWMIService : Set oWMIService = GetObject("winmgmts:\\" & sComputer & "\root\cimv2")
Dim colItems : Set colItems = oWMIService.ExecQuery ("Select * from Win32_Process Where Name = 'outlook.exe'")
Dim oOutlook : Set oOutlook = CreateObject("Outlook.Application")
Dim oNamespace : Set oNamespace = oOutlook.GetNamespace("MAPI")
If colItems.Count = 0 Then
LOG_Write "Outlook isn't open, logging onto it..."
oNamespace.Logon "Outlook",,False,True ' name of Outlook profile
bOpenedOutlook = True
End If
Dim oFolder : Set oFolder = oNamespace.GetDefaultFolder(olFolderInbox)
oFolder.Display ' Make Outlook visible
Here is basic vbscript simple email
' For Example...
Email_List = "0m3r#Email.com;"
Set App = CreateObject("Outlook.Application")
Set Mail = App.CreateItem(0)
With Mail
.To = Email_List
.CC = ""
.BCC = ""
.Subject = "Hello World"
.HTMLBody = "Bla Bla!!!"
'.Body = strbody
'You can add a file like this
' .Attachments.Add (FilePath)
'use .Send (to send) or .Display (to display the email and edit before sending)
.Display
.send
End With
Set Mail = Nothing
Set App = Nothing
Save is as name.vbs

Email Signatures with Outlook Formatting

Ok, so I have the following code on a button on a form in access.
Private Sub Request_FTP_Click()
Dim olApp As Object
Dim objMail As Object
On Error Resume Next 'Keep going if there is an error
Set olApp = GetObject(, "Outlook.Application") 'See if Outlook is open
If Err Then 'Outlook is not open
Set olApp = CreateObject("Outlook.Application") 'Create a new instance
End If
Set objMail = olApp.CreateItem(olMailItem)
With objMail
.display
End With
signature = objMail.Body
With objMail
.To = "kristian#hebsdigital.com"
.Cc = ""
.Subject = "Please create FTP for " + Property_Name.Value
.Body = "Hi Ilya, " & vbNewLine & vbNewLine & "Could you please create an FTP for " & Property_Name & vbNewLine & vbNewLine & "Thank you," & signature
.send
End With
End Sub
The problem is that I want the signature to be with outlook formatting. (colors, font, etc.) Right now it loads like this:
Name
Last Name
HYPERLINK "http://www.hebsdigital.com/"Web • HYPERLINK "http://hebsdigital.com/blog/"Blog
Does anyone have a solution?
To get the HTML formatting you have to create the string in html format and then use .HTMLBody
Also Outlook is one Application where CreateObject doesn't create a new instance. It will pick up the existing instance.
For Example
Private Sub Request_FTP_Click()
Dim olApp As Object, objMail As Object
Dim Sig as String
Set olApp = CreateObject("Outlook.Application")
Set objMail = olApp.CreateItem(olMailItem)
Sig = "Hi Ilya, <br>" & vbNewLine & vbNewLine & _
"Could you please create an FTP for <br>" & Property_Name & vbNewLine & _
"Thank you,<br>" & _
"Web" & vbNewLine & _
"Blog "
With objMail
.To = "kristian#hebsdigital.com"
.Cc = ""
.Subject = "Please create FTP for " & Property_Name.Value
.htmlbody = Sig
.Display
End With
End Sub

ASP Email - objCDOMail.Send server error

I have to build an email for for a basic website which has to be in ASP, which is something I know nothing about.
I have built the form and it all seems to work error free up until the point where the email has to be sent, specifically this line:
objCDOMail.Send
The error reported in the browser is:
Server Error 500 - Internal server error. There is a problem with the
resource you are looking for, and it cannot be displayed.
Would anyone have any suggestions as to why this might be happening?
All suggestions will be very much appreciated!
The full code is as follows:
<%
Dim name, email, comments
Dim objCDOMail 'The CDO object
name = Request.Form("name")
email = Request.Form("email")
comments = Request.Form("query")
message = "<HTML><table border='0' width='80%' bgcolor='#FFFFFF'><tr><td> " & _
"<p><strong>Name:</strong> " & name & _
"<p><strong>E-mail:</strong> " & email & _
"<p><strong>Comments:</strong> " & comments & _
"<p><hr></td></tr></table></html>"
Response.Write("" & message & "<br />")
Set objCDOMail = CreateObject("CDO.Message")
objCDOMail.From = "user#domain.co.uk"
objCDOMail.To = "user#domain.co.uk"
objCDOMail.Subject = ""
objCDOMail.HTMLBody = message
objCDOMail.Send
Set objCDOMail = Nothing
%>
It is probably something to do with the server. IIS6 and IIS7 deal with it in different ways. However this should help you get around it. It is code I have pulled from an existing site and altered a little. You'll have to augment to your own purposes:
Response.Buffer = True
Dim strBody
Dim objCDOMail
Dim strMyEmailAddress
Dim strCCEmailAddress
Dim strBCCEmailAddress
Dim strReturnEmailAddress
'THIS IS WHERE YOU CHANGE THE EMAIL ADDRESS
strMyEmailAddress = "you#yourdomain.com"
'strBCCEmailAddress = "bcc#yourdomain.com"
strReturnEmailAddress = Request.Form("Email")
strBody = strBody & "<br /><br /><span><strong>Name:</strong></span> " & Request.Form("name")
strBody = strBody & "<br /><br /><span><strong>E-mail:</strong></span> " & Request.Form("email")
strBody = strBody & "<br /><br /><span><strong>Company:</strong></span> " & Request.Form("company")
strBody = strBody & "<br /><br /><span><strong>Enquiry:</strong></span> " & Request.Form("enquiry")
strBody = strBody & "</p></div></body>"
If Len(strReturnEmailAddress) < 5 OR NOT Instr(1, strReturnEmailAddress, " ") = 0 OR InStr(1, strReturnEmailAddress, "#", 1) < 2 OR InStrRev(strReturnEmailAddress, ".") < InStr(1,strReturnEmailAddress, "#", 1) Then
strReturnEmailAddress = strMyEmailAddress
End If
Set objCDOMail = Server.CreateObject("CDO.Message")
objCDOMail.From = Request.Form("name") & " <" & strReturnEmailAddress & ">"
objCDOMail.To = strMyEmailAddress
objCDOMail.Cc = strCCEmailAddress
objCDOMail.Bcc = strBCCEmailAddress
objCDOMail.Subject = "Your Email Subject"
objCDOMail.HTMLBody = strBody '**This is for HTML Emails**
objCDOMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing")=2
objCDOMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver")="localhost"
objCDOMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport")=25
objCDOMail.Configuration.Fields.Update
objCDOMail.Send
Set objCDOMail = Nothing
The last few lines deal with the server and use use the localhost for sending mail. You can change this depending on your SMTP server.

Send email from Excel in Exchange environment

I have a userform that helps different users fill in data into the spreadsheet. As soon as the data is inserted it should also be sent by email to a few recipients, depending on the options filled in the form.
This happens within a corporate environment using Exchange. I would create a new email account for this file to be able to send the email as an entity and not use the user's email account.
Is this possible? How? I have googled for it and all I can find is how to create a mail message that the user sends from his account.
I've used the code below (source) to send e-mails from Excel-VBA. I've only tested it with my own e-mail account, but I assume you could have it send mail from a different account (msgOne.from = ...), as long as the user has permission to send from that account on the Exchange server.
Dim cdoConfig
Dim msgOne
Set cdoConfig = CreateObject("CDO.Configuration")
With cdoConfig.Fields
.Item(cdoSendUsingMethod) = cdoSendUsingPort
.Item(cdoSMTPServerPort) = 25 '465 ' (your port number) usually is 25
.Item(cdoSMTPServer) = "smtp.mysmtpserver.com" ' your SMTP server goes here
'.Item(cdoSendUserName) = "My Username"
'.Item(cdoSendPassword) = "myPassword"
.Update
End With
Set msgOne = CreateObject("CDO.Message")
Set msgOne.Configuration = cdoConfig
msgOne.To = "someone#somewhere.com"
msgOne.from = "me#here.com"
msgOne.subject = "Test CDO"
msgOne.TextBody = "It works just fine."
msgOne.Send
Unfortunately, I can't test this hypothesis at this time, as I'm only set up to send from one account. Let me know how it works out!
If the excel application is running on a machine with outlook, you can something along the following.
Function SendEmailWithOutlook(er As emailRecord,
recipients As String,
cc As String,
subject As String,
body As String,
attachmentPath As String) As Boolean
Dim errorMsg As String
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error GoTo errHandle
If (er.useTestEmail = True) Then
recipients = er.emailTest
cc = er.emailTest
End If
With OutMail
If er.emailFrom <> "" Then
.sentOnBehalfOfName = er.emailFrom
End If
.To = recipients
.cc = cc
.bcc = er.emailBcc
.subject = subject
.htmlBody = body
If attachmentPath <> "" Then
.Attachments.Add attachmentPath
End If
.Send 'or use .Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
SendEmailWithOutlook = True
Exit Function
errHandle:
errorMsg = "Error sending mail via outlook: " & Err.Description & vbCrLf
errorMsg = errorMsg & "OnBehalfOf:" & er.emailFrom & vbCrLf
errorMsg = errorMsg & "Recipients: " & recipients & vbCrLf
errorMsg = errorMsg & "CC: " & cc & vbCrLf
errorMsg = errorMsg & "BCC: " & er.emailBcc
MsgBox errorMsg
SendEmailWithOutlook = False
End Function
Add a reference to Microsoft Outlook 14.0 Object Library
Why not use the Outlook Object Model?
You can give the current user the right to send on behalf of the specified user, then set MailItem.SentOnBehalfOfName and MailItem.ReplyRecipients (if necessary) properties before callign MailItem.Send.