How to add CC in VB codes while Creating Excel macros to be sent via outlook application?

Excelviewer

New Member
Joined
Sep 4, 2012
Messages
6
Hi Friends,

Please can anyone help me out in this below task.

I am creating a macro file on which the excel file contents needs to be sent through outlook application, I have codings to add 'To' field, can anyone help me out to add 'CC' & 'From' fields.

Private Sub CommandButton1_Click()
Dim strbody As String
Set myolapp = CreateObject("Outlook.Application")
Set mailitem = myolapp.createitem(olmailitem)
Set myrecipient = mailitem.Recipients.Add(xyz@gmail.com)
mailitem.Subject = "Answers"
strbody = Sheet4.Range("b3").Value & vbNewLine & vbNewLine & Sheet4.Range("b7").Value & vbNewLine & Sheet4.Range("b8").Value & vbNewLine & Sheet4.Range("b9").Value
mailitem.body = strbody
ActiveSheet.Copy
Set WB = ActiveWorkbook
Filename = "copysheet.xls"
On Error Resume Next
Kill "C:\" & Filename
On Error GoTo 0
WB.SaveAs Filename:="Q:\Shared\MOP Bridge Gas\BG Passback sheet\New Template" & Filename
mailitem.Attachments.Add WB.FullName
mailitem.display
End Sub



Thanks,
Excelviewer
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Alternatively, does this help?
Code:
Private Sub CommandButton1_Click()

Dim wb As Workbook


With CreateObject("Outlook.Application").createitem(0)
    .To = ""
    .cc = "Kyle@gmail.com"
    .bcc = ""
    .SentOnBehalfOfName = "ExcelViewer@gmail.com"
    .Subject = "Answers"
    .body = Sheet4.Range("b3").Value & vbNewLine & vbNewLine & _
            Sheet4.Range("b7").Value & vbNewLine & _
            Sheet4.Range("b8").Value & vbNewLine & _
            Sheet4.Range("b9").Value
    ActiveSheet.Copy
    Set wb = ActiveWorkbook
    wb.SaveAs "Q:\Shared\MOP Bridge Gas\BG Passback sheet\New Template\copysheet.xls"
    .attachements.Add wb.FullName
    .display
End With


End Sub
 
Upvote 0
Hi Kyle,

Thanks for your codings.
Its working good but I need small updations on this.

Can you please help me out???
1. I want to add multiple mail ids in TO and CC fields EX: "xyz@gmail.com, kyle@gmail.com , john@gmail.com"
2. Need to move the entire workbook through mail including sheet1,2 & 3
3. No need to save the copy of workbook which has been sent in mail.

Thanks for your Help :)

Regards,
ExcelViewer
 
Upvote 0
My code above was just a rewrite of your existing code.

Has the workbook already been saved? If it has, you can use the below:
Code:
Private Sub CommandButton1_Click()


Dim wb As Workbook


With CreateObject("Outlook.Application").createitem(0)
    'Either use the recipients collection - default is to Field
    .Recipients.Add "Kyle@gmail.com"
    .Recipients.Add "xyz@gmail.com"
    'For anything other than the to field you'll need to include a type
    With .Recipients.Add("john@gmail.com")
        .Type = 2 '- Set the type for cc
    End With
    '---- or ----
    'You can build the To/CC fields by string
    
    '.To = "Kyle@gmail.com; xyz@gmail.com"
    '.cc = "john@gmail.com"
    .bcc = ""
    .SentOnBehalfOfName = "ExcelViewer@gmail.com"
    .Subject = "Answers"
    .body = Sheet4.Range("b3").Value & vbNewLine & vbNewLine & _
            Sheet4.Range("b7").Value & vbNewLine & _
            Sheet4.Range("b8").Value & vbNewLine & _
            Sheet4.Range("b9").Value
    .attachments.Add ThisWorkbook.FullName
    .display
End With


End Sub
 
Upvote 0

Forum statistics

Threads
1,215,493
Messages
6,125,131
Members
449,206
Latest member
burgsrus

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top