I followed the below example.
https://www.slipstick.com/developer/create-a-new-message-using-vba/
I want to display address book name when automatically making the email draft.
For example, in this case,
I am able to display "BZ#gmail.com" in the address field but I want to display an address book's name "Business Team" because "Business Team" is easier to see.
Here is my code. it is almost same as the example code.
Public Sub CreateNewMessage()
Dim objMsg As MailItem
Set objMsg = Application.CreateItem(olMailItem)
With objMsg
.To = "Alias#domain.com"
.CC= "Alias2#domain.com"
.BCC = "Alias3#domain.com"
.Subject = "This is the subject"
.Categories = "Test"
.VotingOptions = "Yes;No;Maybe;"
.BodyFormat = olFormatPlain ' send plain text message
.Importance = olImportanceHigh
.Sensitivity = olConfidential
.Attachments.Add ("path-to-file.docx")
' Calculate a date using DateAdd or enter an explicit date
.ExpiryTime = DateAdd("m", 6, Now) '6 months from now
.DeferredDeliveryTime = #8/1/2012 6:00:00 PM#
.Display
End With
Set objMsg = Nothing
End Sub
I can send only by "alisas#domain.com" and when I see the opened draft, email address displayed.
How can I display address book name on draft?
For the first part of your question, refer to Recipients.Add Method (Outlook).
For CC's, you need to change the recipient type, see answer for this SO question.
Related
I would like to reply to an email without including the original email, but include the sender as the recipient, the subject, and my signature.
Currently I have Set objDoc = ActiveInspector.WordEditor. I tried looking for the body of the email here, so I can replace it with other text.
I would like to retain the Subject of the email I am replying to, the Sender, and the Recipient.
Here is the code I have for my current reply:
With oReply
.BCC = bccField
.CC = ccField
.HTMLBody = "<HTML><Body><span>my reply here</span><Body></HTML>" & .HTMLBody & "<span>" & "Additional stuff" & "</span>"
End With
I tried taking out the .HTMLBody in between the <span>s, which does take out the original user's message but also takes out my signature.
Instead of using the reply functionality, just create a new mailItem and transfer the details over.
Example below uses the original email. So wherever you have Set oReply = (something).Reply you will replace or update that with Set originalMailItem = (Something). This (something) should be the original mail you are attempting to reply to.
In order to effectively insert the signature, we have to .Display the mailItem. Otherwise outlook will note generate the signature. Make sure the signature settings are correct on your outlook client to default to the desired signature for new mail items.
Set newMail = outApp.CreateItem(olMailItem) 'Create a new mail instead of replying to existing
With newMail
.Display
.HTMLBody = "<HTML><Body><span>my reply here</span><Body></HTML>" _
& "<span>" & "Additional stuff" & "</span>" _
& .HTMLBody 'HTMLBody already contains the signature once the email was displayed so we just tack it onto the end.
.To = originalMailItem.SenderEmailAddress
.CC = originalMailItem.CC
.BCC = originalMailItem.BCC
.Subject = originalMailItem.Subject
'.Send 'To send the reply
End With
I'm trying to use Word VBA to send a document to an email recipient. For the most part, it is not difficult. I have this code so far:
With oItem
'Set the recipient for the new email
.To = "person1#mail.com"
'Set the recipient for a copy
.CC = "ccperson#mail.com"
'Set the subject
.Subject = "Blah blah"
End With
My problem is that I have several sender email addresses configured in Outlook, and Outlook is picking the wrong one by default.
Is there a way to specify a sender email address using the method above? Needless to say, the intuitive code line for specifying a sender address (.From = me#wherever.com) does not work. Thank you.
UPDATE:
I finally got my code to work after modifying it using the suggestions from peakpeak and Dimitry below. My changes were
1) to include a reference to the Microsoft Outlook 16 object library so that I could get access to the Outlook.MailItem datatype. The mail would send fine with the code above (without the reference), but would always send with the wrong From address.
2) Declare the mail item as Outlook.MailItem. This seemed to enable the SentOnBehalfOfName field.
3) Used my desired From: email address in the SentOnBehalfOfName field.
Here is the working code:
Dim MAPIMailItem As Outlook.MailItem
Set MAPIMailItem = olkApp.CreateItem(olMailItem) 'Create a new mail message
With MAPIMailItem
.BodyFormat = olFormatPlain
.to = strTo
' SentOnBehalfOfName sets the From field on my machine,
' AFTER I declared MAPIMailItem as Outlook.MailItem
.SentOnBehalfOfName = "fromAddress#foo.com"
.Subject = strSubject
.body = strBody
.attachments.Add strAtt
'.send
.Display
End With
I use this code:
Dim WantedAccount as String ' Set to preferred account name
Set MAPISession = objOutlook.Application.Session 'Get the MAPI Outlook session
Set MAPIMailItem = objOutlook.CreateItem(olMailItem) 'Create a new mail message
With MAPIMailItem
For Each Account In MAPISession.Accounts
If Account = WantedAccount Then
.SendUsingAccount = Account
Exit For
End If
Next
If you are sending through an Exchange account, set the MailItem.SentOnBehalfOfName property (assuming you have the right to send on behalf of the specified mailbox). If you are sending through a POP3/SMTP account, set the MailItem.SendUsingAccount property.
I have a program that sends and email out to user when they have been added in.
I want the email to cc in multiple members of the IT team, however, I can only get it to CC to one person.
Below is my code:
objMail = CType(objOutlook.CreateItem(Outlook.OlItemType.olMailItem), Outlook.MailItem)
' Set the properties of the email.
With objMail
.Subject = "Website Credentials"
.To = "chris.downs#test.com"
.CC = "benji#test.com, Alicia#test.com"
.Body = "Hi"
.Send()
End With
This causes the email not to send at all. I have also attempted the below and this only CC's the last person not both.
' Set the properties of the email.
With objMail
.Subject = "Website Credentials"
.To = "chris.downs#test.com"
.CC = "benji#test.com"
.CC = "Alicia#test.com"
.Body = "Hi"
.Send()
End With
Is there a simple way of doing this that I'm missing?
Outlook, unlike standard email clients, separates entries on the TO, CC, and BCC lines with ;, not ,. Change your CC line to
.CC = "benji#test.com; Alicia#test.com"
and it should send to both.
Your first try wasnt that bad.
Just replace the comma with a semicolon.
I think it should work like this
.CC = "benji#test.com; Alicia#test.com"
I'm trying to write a macro that sends an automatic notification to specific addresses before sending the original email. (Like a cc, without actually using cc.)
The content of the original formatted email, (including text, tables, and pictures,) should be copied and pasted into a new email which is then automatically sent. Everything works when I just display the message, but not when actually sending the email.
Here is my code:
Dim objMsg As Outlook.MailItem
Dim activeMailMessage As Outlook.MailItem
Dim BodyText As Object
' Create the message.
Set objMsg = Application.CreateItem(olMailItem)
'copy body of current item
Set activeMailMessage = ActiveInspector.CurrentItem
activeMailMessage.GetInspector().WordEditor.Range.FormattedText.Copy
'paste body into new email
Set BodyText = objMsg.GetInspector.WordEditor.Range
BodyText.Paste
'set up and send notification email
With objMsg
.To = "test#domain.com"
.Subject = "text"
.Send
End With
The text should be pasted into the body like this, but it won't paste:
With objMsg
.To = "test#domain.com"
.Subject = "test"
.body = bodytext.paste
.Send
End With
When I use .display the correct content is displayed. But when I send it directly (without first using .display), all of all information is lost and an empty email is sent. What can I do?
I could add a bcc in the original email to achieve the same result, but the original email does not always send, whereas this notification should be.
Your code is never actually setting the Body of the e-mail in the objMsg object. It is working when you have objMsg displayed because your interacting with the 'Inspector'.
If you directly set either the HTMLBody (if you want to retain formatting), or the Body property on objMsg then it will work as in the below example.
With objMsg
.HTMLBody = activeMailMessage.HTMLBody
.To = "test#domain.com"
.Subject = "text"
.Send
End With
Bob, regarding your question on images that are embedded within the e-mail being lost with the above approach. An alternate solution could be to use the MailItem's Copy method to create your new MailItem exactly as the original Item. This will also retain who the e-mail is being sent to you need to clear this to make sure only the intended recipients receive it.
Dim objMsg As Outlook.MailItem
Dim activeMailMessage As Outlook.MailItem
' Create the new message.
Set objMsg = Application.CreateItem(olMailItem)
' Assign the current item to activeMailMessage
Set activeMailMessage = ActiveInspector.CurrentItem
' Copy the current item to create a new message
Set objMsg = activeMailMessage.Copy
' Clear any existing recipients of the e-mail, as these will be retained in the Copy
While objMsg.Recipients.Count > 0
objMsg.Recipients.Remove 1
Wend
'set up and send notification email
With objMsg
.To = "test#domain.com"
.Subject = "text"
.Send
End With
This should retain your images and other attachments as they were in the original e-mail.
Try to call the Save method after calling the Paste method.
Basically I need to send a report each day to dealers we disburse funds to. I have a table that has the dealer names, ID number, and their email addresses.
For each report I need to loop through this table and send an email to the corrisponding dealer by ID number.
The Dealer information:
SELECT DealerDetail.dealer_id, DealerDetail.dealer_name, DealerDetail.email
FROM DealerDetail;
The query my report uses:
SELECT LoanDetail.Book_Date, LoanDetail.DLR_Name,
LoanDetail.DLR_ID, LoanDetail.Cust_ID, LoanDetail.Amt_Fin,
LoanDetail.APR, LoanDetail.Buy_Rate, LoanDetail.Pmt_Amt,
LoanDetail.TERM, LoanDetail.Part, LoanDetail.[Flat Fee]
FROM LoanDetail
WHERE (((LoanDetail.Book_Date)=Date()-1));
Another way would be to loop through and create an outlook instance to send the email, shown in the code below
Private Function SendEmail(attachment, subject,strContactEmail,strEmailText,strCc)
Dim olLook As Object 'Start MS Outlook
Dim olNewEmail As Object 'New email in Outlook
Set olLook = CreateObject("Outlook.Application")
Set olNewEmail = olLook.createitem(0)
With olNewEmail 'Attach template
.To = strContactEmail
.cc = strCc
.body = strEmailText
.subject = subject
.attachments.Add (attachment)
.display
End With
.send
End Function
Hope this helps!
One way would be to loop through the dealer table and use SendObject to send the report:
Dim rs AS DAO.Recordset
Set rs = CurrentDB.OpenRecordset("SELECT email FROM DealerDetail")
Do While Not rs.EOF
DoCmd.SendObject acReport, "ReportName", _
acFormatPDF, rs!Email, , , "Report", "Here is the report", True
rs.MoveNext
Loop