Right now I have a workbook with some VBA that will email one user (email address is hard coded) when a button is pressed. It works great. However, I was wondering if it were possible to "cc" the email to the the user who is pressing the button to email. It could be from 10-15 different people.
Right now the below code will email "orders@myemail.com" a copy of the sheet called "Print", and in the inbox it comes from the correct user. Somehow it is able to tap into the users email and send it automatically for them, so I'm thinking there must be a way for them to CC themselves too.
All email accounts will be on Microsoft Oulook.
Here is the code that emails one person (I got it from http://www.rondebruin.nl/win/s1/outlook/amail2.htm ):
'Sub that emails the 3rd sheet in the body of an email
Sub Mail_Sheet_Outlook_Body()
Call UnProtect
Application.ReferenceStyle = xlA1
'RangetoHTML function is copied in the module after this sub.
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set rng = Nothing
Set rng = Sheets("Print").UsedRange
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = "orders@myemail.com"
.CC = ""
.BCC = ""
.Subject = "New Order from Employee"
.HTMLBody = RangetoHTML(rng)
.Send 'or use .Display
End With
On Error GoTo 0
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Set OutMail = Nothing
Set OutApp = Nothing
Call Protect
End Sub
So to reiterate what I'm asking, is there anyway that when user1@email.com sends in an order/sends an email, that it "cc"s it to himself, and when user2@email.com does the same thing it only cc's himself. Dynamically changes based on who's account has the workbook open.
Try with application.Session.CurrentUser.Address to get email id
Sub EmailWithCCTome()
Dim outlookobj As Object
Dim emailitem As Object
Set outlookobj = CreateObject("Outlook.Application")
Set emailitem = outlookobj.CreateItem(olMailItem)
With emailitem
.To = toemail
.CC = outlookobj.Session.CurrentUser.Address
End With
You'll have to program some way to capture the email address you want to cc somewhere on your sheet / form and store it as a string variable (or pass the .Value reference directly to the .CC field) and / or use the sheet reference. Something like:
Sub Mail_Sheet_Outlook_Body()
Call UnProtect
Application.ReferenceStyle = xlA1
'RangetoHTML function is copied in the module after this sub.
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
**Dim ccEmail As String**
{your code here}
ccEmail = Sheet1.Range("A1").Value ' or where ever you capture the email
{more of your code here}
With OutMail
.To = "orders@myemail.com"
**.CC = ccEmail**
{rest of your code}
There's no reliable way for Excel / VBA to know an email address without you capturing it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.