Adding current user to CC list - vba

I'm trying to combine two actions.
'Assign To List
ToList = Worksheets("VBA_Code").Range("B5")
'Assign CC List
CCList = Worksheets("VBA_Code").Range("B6")
On Error Resume Next
With OutMail
.to = ToList
.CC = CCList
.BCC = OutApp.GetNamespace("MAPI").Session.CurrentUser.AddressEntry
I have a list of users to be CC'ed. I don't think I need to include the sender under BCC, as the code above does, so I wanted to include the sender in my CC, along with the list of those in the CC list.
Is there a way to combine those two actions/calls?

The To, CC and BCC list in Outlook are semi-colon delimited. So, just append like so:
.CC = CCList & ";" & OutApp.GetNamespace("MAPI").Session.CurrentUser.AddressEntry

Related

VBA: MailItem.CC Doesn't Seem to be Working

I've got some code designed to automatically send emails with attachments. It's been working 100% fine but I am now required to CC people as well. I thought it should be simple and added the .CC property to my MailItem but for the life of me I just can't get it to work.
I've searched the web and other questions on here but haven't been able to get anything to work.
I tried
EmailItem.Recipients.Add (CCStr)
EmailItem.Recipients.Add(CCStr).Type = olCC
but that didn't work either. The code still sends to the email listed by EmailItem.To just fine but the CC just doesn't work. I've posted my code below, any and all help is greatly appreciated.
Dim Cust_Email As String
Dim Email As Outlook.Application
Set Email = New Outlook.Application
Dim EmailItem As Outlook.MailItem
Dim CCStr As String
E1: Cust_Email = InputBox("Please enter the email of your desired recipient." & vbCrLf &
vbCrLf & "Separate mutliple emails with a semicolon: ( ; )", "Who are we sending to?", "
[Enter email here]")
If StrPtr(Cust_Email) = 0 Then
GoTo Final
ElseIf Cust_Email = vbNullString Or Cust_Email = "[Enter email here]" Then
MsgBox "You must enter an email address to forward the quote(s) to.", vbOKOnly,
"Silly goose."
GoTo E1
End If
'
CCStr = InputBox("Please enter the email for any recipients you would like cc'd on these
emails. (Leave blank if none.)", "Carbon Copy?", "")
Set EmailItem = OutApp.CreateItem(olMailItem)
EmailItem.To = Cust_Email
EmailItem.CC = CCStr
EmailItem.Subject = "Title"
EmailItem.HTMLBody = "Text"
EmailItem.Attachments.Add (filename & ".pdf")
EmailItem.Send
Final: End Sub
Thank you!
In the code you have tried to add two separate recipients to the collection:
EmailItem.Recipients.Add (CCStr)
EmailItem.Recipients.Add(CCStr).Type = olCC
Each time you call the Recipients.Add method a new recipient is added to the collection. You need to use the following approach instead:
recipientCC = recipients.Add("Eugene Astafiev")
recipientCC.Type = OlMailRecipientType.olCC;
After setting up all recipients for the item don't forget to call the Recipients.ResolveAll method which attempts to resolve all the Recipient objects in the Recipients collection against the Address Book.
You can read more about that in the How To: Fill TO,CC and BCC fields in Outlook programmatically article.

Send excel table as an attachment

Need your help.
I have an excel sheet of item master with stock availability and store email address are in heralding cells vertically & under the email address there are Y & N mapping done against entire style code in the in the item master.
I am looking for a VBA code that should filter database one by one for the email address mentioned in the heading cells and copy table where Y found and send the table as an attachment.
Subject would be "Updated SOH". Kindly refer database sample mentioned below.
I use a macro for sending emails all the time, here is the unedited code:
Sub SendEmail(Optional ToAddresses As String, Optional CcAddresses As String, _
Optional BccAddresses As String, Optional Subject As String, _
Optional Body As String, Optional AttachFiles As Variant = False, Optional AutoSend As Boolean = False)
'Adapted from https://www.rondebruin.nl/win/s1/outlook/bmail4.htm
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = ToAddresses
.CC = CcAddresses
.Bcc = BccAddresses
.Subject = Subject
If Body Like "*</*>*" Then
.HtmlBody = Body
Else
.Body = Body
End If
If Not AttachFiles = False Then
If IsArray(AttachFiles) Then
For x = LBound(AttachFiles) To UBound(AttachFiles)
.Attachments.Add (AttachFiles(x))
Next
Else
.Attachments.Add (AttachFiles)
End If
End If
If AutoSend = True Then
.Send
Else
.Display
End If
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
I can't see your screenshot due to a problem with my browser (sorry), but hopefully this should help as at some point in your loop in another sub you can just do something like this;
Call SendEmail (ToAddresses:= "someone#example.com; someoneelse#exmpl.com", _
Subject:="Updated SOH", _
Attachfiles:="c:/Users/Me/Documents/Myfile.txt", _
Autosend:=True)
Some notes on the usage of the sub:
All the arguments are optional
To, CC and Bcc addresses are strings, if you want multiple addresses in any of those fields just separate them with a semicolon, like with any normal email.
Subject and Body are strings too. Body can be HTML or plain t text, it will detect which.
Attachfiles needs to be either a string, or a one-dimensional string array (if attaching multiple files). It should be the full filename including path - see the example I gave above.
If AutoSend is True, the email will send without you ever seeing it (unless there's an error e.g. you didn't enter a valid address to send it to). If it's False (which it defaults to) it will show you the email instead and you can edit it before you hit the send button.
Hope that helps you or anyone reading this!! I find it really useful myself and there's no reason why others shouldn't benefit - as per the first comment in the code I adapted it from an online source anyway.

How to replace the body of a reply to an email and keep signature?

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

Including multiple CC's in an Outlook Mail

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"

How can I create email message with address name?

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.