Adding multiple CC in Outlook Mail - vba

I have this line of code I've tried to add multiple CC's in the Outlook Mail. But it only returns ;. I have found this sample at MSDN.
Dim ccMail as String
Dim ccRow as Long
Dim objMail as Object
ccRow = Cells(Rows.count, 16).End(xlUp).Row
With objMail
.Subject = Sheet1.TextBox1.Value
For k = 4 To ccRow
ccMail = ccMail & ";" & Cells(k, 1).Value
Next k
.cc = ccMail
end with
All of the CC Recipients is found in column P.
Any help?Thanks.

You used With objMail but did not specify Worksheet for Cells(k, 1).Value. This is likely to result in an error.
In addition, I presume you want to refer to ws.Cells(k, 16) instead since what you want is column P.

Here is an Example on how to loop using Do-Until Loops to get cell Values.
Option Explicit
Sub Example()
Dim olApp As Object
Dim olMail As Object
Dim olRecip As Object
Dim iRow As Long
Dim Recip As String
Dim Sht As Worksheet
iRow = 2
Set olApp = CreateObject("Outlook.Application")
Set olMail = olApp.CreateItem(0)
Set Sht = ActiveWorkbook.Sheets("Sheet1")
With Sht
With olMail
Do Until IsEmpty(Cells(iRow, 16))
Recip = Cells(iRow, 16).Value
Set olRecip = .Recipients.Add(Recip)
olRecip.Type olCC
olRecip.Resolve
iRow = iRow + 1
Loop
.Subject = "Subject"
.Body = "Hi " & .Body
.Display
End With
End With
Set olApp = Nothing
End Sub
Do-Until loop and IsEmpty

Related

How do I add Date range criteria to email import

I have some coding which imports several folders worth of stored emails into Excel and it works as expected. However the coding brings back all stored emails from the folders, I would like it to return only the previous month's emails. I have some formulas in place which are dynamic and automatically update the previous months start and end date, I know I need to reference these ranges within the coding.
How would I add the date criteria to the below coding (if at all possible)?
TIA
Sub test()
Dim olApp As Outlook.Application
Dim olNS As Outlook.Namespace
Dim olFldr As Outlook.MAPIFolder
Dim olItem As Object
Dim olMailItem As Outlook.MailItem
Dim ws As Worksheet
Dim iRow As Long
Dim hdr As Variant
Dim iFldr As Long
Set ws = ThisWorkbook.Worksheets("EmailImport")
Set olApp = New Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
With ws
iRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
Application.ScreenUpdating = False
For iFldr = 1 To 18
Select Case iFldr
Case 1
Set olFldr = olNS.Folders("test1").Folders("test1").Folders("test1")
Set olFldr = olFldr.Folders("test1a")
Case 2
Set olFldr = olNS.Folders("test1").Folders("test1").Folders("test1")
Set olFldr = olFldr.Folders("test1b")
Case Else
End Select
For Each olItem In olFldr.Items
If olItem.Class = olMail Then
Set olMailItem = olItem
With olMailItem
iRow = iRow + 1
If Not .Sender Is Nothing Then ws.Cells(iRow, "A") = .Sender
ws.Cells(iRow, "B") = .SenderEmailAddress
ws.Cells(iRow, "C") = .SenderName
ws.Cells(iRow, "D") = .Subject
ws.Cells(iRow, "E") = .ReceivedTime
ws.Cells(iRow, "F") = .Categories
ws.Cells(iRow, "G") = .TaskCompletedDate
ws.Cells(iRow, "H") = olFldr.Name
End With
End If
Next olItem
Next iFldr
With ws
hdr = Array("Sender", "SenderEmailAddress", "SenderName", "Subject", "ReceivedTime", "Categories", "TaskCompletedDate", "Folder")
.Range("A1").Resize(, UBound(hdr)) = hdr
.Columns.AutoFit
End With
End Sub
You could calculate the difference between when it was sent and when you are processing it:
Dim Difference As Long
Difference = DateDiff("d", olItem.SentOn, Now)
If Difference < 30 Then
' Do Stuff
End If
If you want a date comparison this should be possible via an equality statement but make sure the dates are converted to CDate values beforehand
You can create a restriction on the ReceivedTime property and pass it to Items.Restrict (which returns a new restricted Items collection:
([ReceivedTime] > '04/01/2021') AND ([ReceivedTime] < '04/21/2021')

How to change email id in from field in outlook

I need help to find a way to change the e-mail with which email will be sent using VBA. Below is my current code and emails are being sent with my e-mail and i need to change it to group email id.
Dim olApp As Object
Dim olMail As Object
Dim olRecip As Object
Dim olAtmt As Object
Dim iRow As Long
Dim Recip As String
Dim Subject As String
Dim Atmt As String
Dim sMsgBody As String
Dim strfrom As String
iRow = 2
Set olApp = CreateObject("Outlook.Application")
Dim Sht As Worksheet
Set Sht = ThisWorkbook.Worksheets("Sheet1")
Do Until IsEmpty(Sht.Cells(iRow, 1))
Recip = Sht.Cells(iRow, 1).Value
Subject = Sht.Cells(iRow, 2).Value
Atmt = Sht.Cells(iRow, 3).Value ' Attachment Path
Set olMail = olApp.CreateItem(0)
With olMail
Set olRecip = .Recipients.Add(Recip)
.Subject = Subject
.body = sMsgBody
.Display
.
Set olAtmt = .Attachments.Add(Atmt)
olRecip.Resolve
End With
iRow = iRow + 1
Loop
Set olApp = Nothing
If you want to change the sender account, use the Sender property
If you want to send it with your own account but with a different mail address, use SentOnBehalfOfName property. That's what I usually do.
With olMail
Set olRecip = .Recipients.Add(Recip)
' chose either :
.Sender = "anything#yourcompany.com"
' or
.SentOnBehalfOfName = "anything#yourcompany.com"

Copy email subject in outlook to excel using vba with two email address?

I have two email address. The first is address1#domain.com.vn and the second is address2#domain.com.vn.
I want to copy email subject in microsoft outlook with second address address2#domain.com.vn to excel using vba. I use bellow code but it do not work.
Sub GetFromInbox()
Dim olapp As Outlook.Application
Dim olNs As Namespace
Dim Fldr As MAPIFolder
Dim olMail As Variant
Dim Pst_Folder_Name
Dim MailboxName
'Dim date1 As Date
Dim i As Integer
Sheets("sheet1").Visible = True
Sheets("sheet1").Select
Cells.Select
Selection.ClearContents
Cells(1, 1).Value = "Date"
Set olapp = New Outlook.Application
Set olNs = olapp.GetNamespace("MAPI")
Set Fldr = olNs.ActiveExplorer.CurrentFolder.Items
MailboxName = "address2#domain.com.vn"
Pst_Folder_Name = "Inbox"
Set Fldr = Outlook.Session.Folders(MailboxName).Folders(Pst_Folder_Name)
i = 2
For Each olMail In Fldr.Items
'For Each olMail In olapp.CurrentFolder.Items
ActiveSheet.Cells(i, 1).Value = olMail.ReceivedTime
ActiveSheet.Cells(i, 3).Value = olMail.Subject
ActiveSheet.Cells(i, 4).Value = olMail.SenderName
i = i + 1
Next olMail
End Sub
try this
Sub GetFromInbox()
Dim olapp As Outlook.Application
Dim olNs As Outlook.Namespace
Dim Fldr As Outlook.MAPIFolder
Dim olMail As Outlook.MailItem
Dim Pst_Folder_Name As String, MailboxName As String
Dim i As Long
MailboxName = "address2#domain.com.vn"
Pst_Folder_Name = "Inbox"
Set olapp = New Outlook.Application
Set olNs = olapp.GetNamespace("MAPI")
Set Fldr = olNs.Folders(MailboxName).Folders(Pst_Folder_Name)
With Sheets("sheet1")
.Cells.ClearContents
.Cells(1, 1).Value = "Date"
i = 2
For Each olMail In Fldr.Items
'For Each olMail In olapp.CurrentFolder.Items
.Cells(i, 1).Value = olMail.ReceivedTime
.Cells(i, 3).Value = olMail.Subject
.Cells(i, 4).Value = olMail.SenderName
i = i + 1
Next olMail
End With
olapp.Quit
Set olapp = Nothing
End Sub
If your using ActiveExplorer.CurrentFolder then you don't need to set your email Inbox, code should run on currently displayed folder in explorer.
Example
Option Explicit
Public Sub Example()
Dim Folder As MAPIFolder
Dim CurrentExplorer As Explorer
Dim Item As Object
Dim App As Outlook.Application
Dim Items As Outlook.Items
Dim LastRow As Long, i As Long
Dim xlStarted As Boolean
Dim Book As Workbook
Dim Sht As Worksheet
Set App = Outlook.Application
Set Folder = App.ActiveExplorer.CurrentFolder
Set Items = Folder.Items
Set Book = ActiveWorkbook
Set Sht = Book.Worksheets("Sheet1")
LastRow = Sht.Range("A" & Sht.Rows.Count).End(xlUp).Row
i = LastRow + 1
For Each Item In Items
If Item.Class = olMail Then
Sht.Cells(i, 1) = Item.ReceivedTime
Sht.Cells(i, 2) = Item.SenderName
Sht.Cells(i, 3) = Item.Subject
i = i + 1
Book.Save
End If
Next
Set Item = Nothing
Set Items = Nothing
Set Folder = Nothing
Set App = Nothing
End Sub

Automatically send an email if a specific cell value exists; include adjacent value in body

I have been working on an xlsm sheet that as part of its function produces a result of "No Data" in column J if it cannot find a match in its other data files.
What I need is to have Excel loop through Column J and automatically generate an email if the value in J = "No Data" and in the body of the email I need to include the cell offset value from Column F of the same Row.
I have used the Ron De Bruin code and modified it with Looping code from a similar function elsewhere in the project.
I cannot get this to function and could use some direction. Here is the code I have up to this point
Private Sub EmailIC()
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
'This macro adapted from: http://www.rondebruin.nl/win/s1/outlook/bmail4.htm
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim wbXLoc As String, wbX As Workbook, wsX As Worksheet, wsXName As String
Dim Xlr As Long
Dim rngX As Range, cel As Range, order As Range
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
wbXLoc = "C:\Users\Cole\Downloads\Dads Work\XDock\AutoXrpt.xlsm"
wsXName = "AutoX"
Set wsX = wbX.Sheets(wsXName)
'Loop through Column J to determine if = "No Data"
With wbX
Xlr = .Sheets("AutoX").Cells(Rows.Count, 1).End(xlUp).Row
Set rngX = wbX.Sheets("AutoX").Range("J2:J" & Xlr)
End With
'do the loop and find
For Each cel In rngX
If cel.Value = "No Data" Then
On Error Resume Next
With OutMail
.to = "robe******#msn.com"
.CC = ""
.BCC = ""
.Subject = "Need Pick Face please!"
.Body = rngX.cel.Offset(0, -4).Value
.Send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End If
Next cel
End Sub
What Om3r has looks good, they pointed out that you needed to set the wsX variable to an actual sheet before being able to set the range variable rngX. This might be why your loop might not have worked. Hard to say without knowing what error was thrown when you ran your code.
Also, be sure to have the object library for Outlook enabled. Check under the ribbon Tools>References and make sure your Outlook Library is listed.
you may want to try this (commented) code:
Option Explicit
Private Sub EmailIC()
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
'This macro adapted from: http://www.rondebruin.nl/win/s1/outlook/bmail4.htm
Dim OutApp As Outlook.Application
Dim wbXLoc As String, wsXName As String
Dim cel As Range, order As Range
Set OutApp = CreateObject("Outlook.Application")
wbXLoc = "C:\Users\Cole\Downloads\Dads Work\XDock\AutoXrpt.xlsm"
wsXName = "AutoX"
With Workbooks.Open(wbXLoc).Worksheets(wsXName) '<-- open 'wbXLoc' workbook and reference its 'wsXName' worksheet
With .Range("J1", .Cells(.Rows.Count, "J").End(xlUp)) '<--| reference its column "J" cells from row 1 down to its last non empty cell
.AutoFilter field:=1, Criteria1:="No Data" '<--| filter referenced cells with "No Data" criteria
If Application.WorksheetFunction.Subtotal(103, .Cells) > 1 Then '<--| if any cell ha been filtered othre than the header (in row 1)
For Each cel In .Resize(.Rows.Count - 1).Offset(1).SpecialCells(XlCellType.xlCellTypeVisible) '<-- loop through filtered cells (skippin header in row 1)
With OutApp.CreateItem(olMailItem) '<-- cerate and reference a new mail item
.to = "robe******#msn.com"
.CC = ""
.BCC = ""
.Subject = "Need Pick Face please!"
.Body = cel.Offset(0, -4).Value
.Send
End With
Next cel
End If
End With
End With
ActiveWorkbook.Close False '<--| close opened workbook discarding changes (i.e. autofiltering)
OutApp.Quit '<-- quit Outlook
Set OutApp = Nothing
End Sub
little confused to what you doing, but this should get you started-
Option Explicit
Private Sub EmailIC()
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
'This macro adapted from: http://www.rondebruin.nl/win/s1/outlook/bmail4.htm
Dim OutApp As Object ' Outlook.Application
Dim OutMail As Outlook.MailItem
' Dim wbXLoc As String
' Dim wbX As Workbook
Dim wsX As Worksheet
' Dim wsXName As String
' Dim Xlr As Long
Dim rngX As Range
Dim cel As Range
' Dim order As Range
Set OutApp = CreateObject("Outlook.Application")
' wbXLoc = "C:\Users\0m3r\Desktop\Macro-VBA\0m3r.xlsm"
' wsXName = "Sheet2"
Set wsX = ThisWorkbook.Worksheets("AutoX")
' wsXName = "AutoX"
' Set wsX = wbX.Sheets(wsXName)
'Loop through Column J to determine if = "No Data"
' With wbX
' Xlr = .Sheets("AutoX").Cells(Rows.Count, 1).End(xlUp).Row
' Set rngX = wbX.Sheets("AutoX").Range("J2:J" & Xlr)
' End With
Set rngX = wsX.Range("J2", Range("J65536").End(xlUp))
'do the loop and find
For Each cel In rngX
If cel.Value = "No Data" Then
Set OutMail = OutApp.CreateItem(olMailItem)
Debug.Print cel.Value
Debug.Print cel.Offset(0, -4).Value
' On Error Resume Next
With OutMail
.To = "robe******#msn.com"
.CC = ""
.BCC = ""
.Subject = "Need Pick Face please!"
.Body = cel.Offset(0, -4).Value
.Display
End With
On Error GoTo 0
End If
Next cel
Set OutMail = Nothing
Set OutApp = Nothing
End Sub

Sending multiple attachments from excel sheet with VBA

I have the existing code to send a mail from a Sheet in my Excel file -
Sub CreateMail()
Dim objOutlook As Object
Dim objMail As Object
Dim rngTo As Range
Dim rngSubject As Range
Dim rngBody As Range
Dim rngAttach As Range
Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)
Application.ScreenUpdating = False
Worksheets("Mail List").Activate
With ActiveSheet
Set rngTo = .Range("B1")
Set rngSubject = .Range("B2")
Set rngBody = .Range("B3")
Set rngAttach = .Range("B4")
End With
With objMail
.To = rngTo.Value
.Subject = rngSubject.Value
.body = rngBody.Value
.Attachments.Add rngAttach.Value
.display 'Instead of .Display, you can use .Send to send the email _
or .Save to save a copy in the drafts folder
End With
Set objOutlook = Nothing
Set objMail = Nothing
Set rngTo = Nothing
Set rngSubject = Nothing
Set rngBody = Nothing
Set rngAttach = Nothing
End Sub
However, I want to include a number of attachments, and hence the
Set rngAttach = .Range("B4") does not help to do this.
Any help on this?
Thanks in advance!
Enclose your .Attachments.Add statement in loop. Something like below might work
For i = 4 To 6
.Attachments.Add Range("B" & i).Value
Next i
To make it Dynamic you can set the upper limit of i to the last row in Column B
For i = 4 To Range("B" & rows.count).end(xlUp).row
.Attachments.Add Range("B" & i).Value
Next i
This updated code:
Looks for file names from B4
Uses Dir to ensure the attached files actually exist at the specified path
Tidies up the worksheet code (Activate is unnecessary)
Sub CreateMail()
Dim objOutlook As Object
Dim objMail As Object
Dim rngTo As Range
Dim rngSubject As Range
Dim rngBody As Range
Dim rngAttach As Range
Dim rng2 As Range
Dim ws As Worksheet
Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)
Application.ScreenUpdating = False
Set ws = Worksheets("Mail List")
With ws
Set rngTo = .Range("B1")
Set rngSubject = .Range("B2")
Set rngBody = .Range("B3")
Set rngAttach = ws.Range(ws.[b4], ws.Cells(Rows.Count, "B").End(xlUp))
End With
With objMail
.To = rngTo.Value
.Subject = rngSubject.Value
.body = rngBody.Value
For Each rng1 In rngAttach.Cells
If Len(Dir(rng1)) > 0 Then .Attachments.Add rng1.Value
Next
.display 'Instead of .Display, you can use .Send to send the email _
or .Save to save a copy in the drafts folder
End With
Set objOutlook = Nothing
Set objMail = Nothing
Set rngTo = Nothing
Set rngSubject = Nothing
Set rngBody = Nothing
Set rngAttach = Nothing
End Sub