How can I set Sensitivity or custom Sensitivity in an email? - vba

I need to send emails with a confidential tag.
Our in-house custom tag is "Patient-Confidential", there is no "Confidential" under "Sensitivity" but "Patient-Confidential" is the 3rd item on the "Confidential" drop-down list.
The default Outlook tag for confidential is .Sensitivity=3, but it doesn't change the sensitivity.
How can I assign sensitivity, either MS Outlook "Confidential" or "Patient-Confidential"?
I successively tried the following variations:
With OutMail
.To = recipient
.Subject = strsubject
.Body = strbody
.Sensitivity = "Patient-Confidential"
.Sensitivity = 3
.oLSensitivity = 3
.oLSensitivity = "Patient-Confidential"
.Display
End With

Use the following code:
With OutMail
.To = recipient
.Subject = strsubject
.Body = strbody
.Sensitivity = OlSensitivity.olConfidential
.Display
End With
The MailItem.Sensitivity property returns or sets a constant in the OlSensitivity enumeration indicating the sensitivity for the Outlook item.

Related

Outlook VBA to send Automated email with PDF attachment

Need help sending an automated email in outlook using VBA while attaching a PDF from a folder location. Below is what I have thus far. I'm stuck on the part to attach the PDF
Sub ViolationProcessingTest()
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "<BODY style = font-size:11pt;font-family:Calibri>" & _
"Hello, <br><br> Please process the attached violations."
On Error Resume Next
With OutMail
.To = "email#email.com"
.CC = "email#email.com"
.Subject = "Violations Processing"
.Display
.HTMLBody = strbody & .HTMLBody
.Attachments.Add = "Folder path location with PDF"
End With
On Error GoTo 0
Set OutMail = Nothing
End Sub
Firstly, it is a good idea to get rid of the On Error Resume Next line - errors are raised for a reason, otherwise you won't even know why or where your code is failing.
Secondly, your code is almost OK, you just need to get rid of = (it is a method call, not a property assignment) and specify a fully qualified PDF file name:
.Attachments.Add "c:\SomeFolder\TheFile.PDF"
The Attachments.Add method (not a property) ceates a new attachment in the Attachments collection. The source of the attachment can be a file (represented by the full file system path with a file name) or an Outlook item that constitutes the attachment. So, the code should like that:
With OutMail
.To = "email#email.com"
.CC = "email#email.com"
.Subject = "Violations Processing"
.HTMLBody = strbody & .HTMLBody
.Attachments.Add filePathToYourFile
.Display
End With
You may find the How To: Create and send an Outlook message programmatically article helpful.

How to send mail from shared email account?

I am trying send several items at the same time.
I have two mail addresses in my Outlook. The first is a personal work mail (like j.doe#company.com) and second is a shared mail account (like support#company.com).
I tried:
Set objOutlook = CreateObject("Outlook.Application")
Set objMailMessage = objOutlook.CreateItem(0)
Dim oAccount As Outlook.account
Set oAccount = Session.accounts.Item(2)
objMailMessage.SendUsingAccount = oAccount
objMailMessage.display
I found this won't work with shared email accounts.
Most forums advise to use .SentOnBehalfOfName. When I try it on one email, the email has something like this in the Sender box j.doe#company.com - Sent on behalf of name: "support#company.com.
When I send mail from Outlook manually, it only shows the shared account on the received message. (It is not a big deal, but would be nicer if the code would work in the same style as manually.)
Also, when I put the code in a loop, half mails are sent with shared accounts and half with personal account.
And here is the code with .SentOnBehalfOfName which is going to be looped.
Set objOutlook = CreateObject("Outlook.Application")
Set objMailMessage = objOutlook.CreateItem(0)
With objMailMessage
.To = email
.Subject = msgSubj
.CC = ccp
.BCC = "support#company.com"
.SentOnBehalfOfName = "support#company.com"
.HTMLBody = msgText & "<br>" & "<br>" & msgSign
.Attachments.Add path
If rev > 0 Then
.Save
Else
.Send
End If
End With
This code attempts to ensure the .SentOnBehalfOf is consistent.
Option Explicit
Sub sendFromOtherMailbox()
' Code in Outlook, not called from another application
Dim objMailMessage As mailItem
Dim uMailbox As recipient
' Should not be necessary but this is used later to ensure
' the entry in .SentOnBehalfOfName is what you think it is.
Set uMailbox = Session.CreateRecipient("Preferably display name here rather than email address.")
uMailbox.Resolve
' An email address always resolves so the "If Resolved" test is not useful
' if an email address was used in .CreateRecipient
If uMailbox.Resolved Then
Set objMailMessage = CreateItem(olMailItem)
With objMailMessage
.Subject = "send From Other Mailbox"
.SentOnBehalfOfName = uMailbox
.Display
End With
End If
End Sub

Join MS Access data in a column of emails from a filtered sub table into a string for emailing

I have a form that is Company info, in a sub form is a joined table that has company contacts. I would like a button in the parent form that takes all the contacts for that company and creates a string value based on all their email addresses in the email column. This will open an email window for me that has all their emails in the to field.
If this I am new to access and VB but am experienced in DBO and C#, so I can likely understand any answers with not too much explanation.
Not sure what you need for the first part, but here is the code for the email:
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application") 'Defines Outlook
Set OutMail = OutApp.CreateItem(0) 'Creates an email
With OutMail
.To = ""
.Cc = ""
.BCC = ""
.Subject = ""
.Body = ""
.attachments.Add ""
.Display 'To show email
.Send 'To send email
End With

Want to trigger Automate reply after click on a link in Outlook mail

I am doing an Automation on outlook where End user will receive a mail where their will be a link by clicking on which an auto reply mail will be trigger and go to respective person. The code i Tried So far is mentioned below.
Sub MailURL()
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "<HTML><BODY>"
strbody = strbody & "xxx#xxx.com"
strbody = strbody & "</BODY></HTML>"
On Error Resume Next
With OutMail
.to = "abc.domain.com"
.Subject = "Testing URL"
.HTMLBody = strbody
' .Send
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
I understand that it is considered as a Spamming, but that was a requirement for our project, where we will send a mail with some data to end user and if user need some change in that data then he can click on that link and we receive a mail with requested changes so we make changes and update it in database. Thanks.
If I understand what you want, you need to add a link on the body that if clicked, will create a new mail with a pre-defined recipient. If that is so, this part:
"xxx#xxx.com"
should be:
"Relpy here"
You can also add a subject like this:
"Relpy here"
HTML syntax to add hyperlink is using your_linked_text. In your case, you need to add mailto: in front of the linked email address to create a new mail.
Note: This will not automatically send the reply (as you've said that can be considered spamming so let's not do that.)
here is a revision of your code
it worked for me (outlook2016), once i changed abc.domain.com to abc#domain.com
the "OutMail.Send" statement put the message into the outbox
the "OutMail.Display (True)" statement put the "new email" window on top of all other windows so that the user could verify the info and click "send"
Sub MailURL()
Dim strbody As String
strbody = "<HTML><BODY>"
strbody = strbody & "xxx#xxx.com"
strbody = strbody & "</BODY></HTML>"
Dim OutMail As Outlook.MailItem ' define the actual object ... then "help info" pops up on screen as you type
Set OutMail = Application.CreateItem(olMailItem) ' "Application" object already exists
With OutMail
.To = "abc#domain.com"
.Subject = "Testing URL"
.HTMLBody = strbody
' you can use only one of the following two lines
' .Send ' this puts the email message into the outbox
.Display (True) ' this show the email on top of everything else. just have the user click "send"
End With
Set OutMail = Nothing
End Sub

Preserve time in hour:min format in Outlook mail when value sent from Excel

When sent to Outlook email the time format of hour:min is changed to a number e.g. .463.
Private Sub CommandButton1_Click()
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)
EndDate = ActiveCell.Value
With OutMail
.To = "Enter Specific contact email address"
.CC = ""
.BCC = ""
.Subject = "Enter subject, along the lines of Reminder, due to end etc."
.body = EndDate
.Display
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
You just need to change the body to have the format you want. Look below:
.body = Format(EndDate, "HH:MM")
That's the only thing that needs to be changed. You can refer to here for other available formats like "HH:MM AMPM".
P.S.
What #ScottCraner proposed (EndDate = ActiveCell.Text) does work too. But I personally prefer the first solution as it keeps you enable to perform calculations on the EndDate variable, if necessary.