Almost a month ago, I have already posted a question that is somewhat similar with this current question of mine.
Sending Outlook Email with multiple recipients from Excel file
But today, I want to develop codes that do not care whether the TO field only contains 1 recipient and perhaps EMPTY for CC. I was able to come up with this two types of codes:
A.
'Set Recipients
Range("A2").Select
Set Recipient = Range(ActiveCell, ActiveCell.End(xlDown))
'Set Recipients
Range("B2").Select
Set CC = Range(ActiveCell, ActiveCell.End(xlDown))
On Error Resume Next
With OutlookMailItem
.Display
'Assign Recipients in TO field
For Each sTo In Recipient
Set myRecipient = OutlookMailItem.Recipients.Add(sTo)
myRecipient.Type = olTo
myRecipient.Resolve
If Not myRecipient.Resolved Then
myRecipient.Delete
End If
Next sTo
'Assign CCs in CC field
For Each sCc In CC
Set myCc = OutlookMailItem.Recipients.Add(sCc)
myCc.Type = olCC
myCc.Resolve
If Not myCc.Resolved Then
myCc.Delete
End If
Next sCc
End With
However, these codes only works with two and more email addresses. When I tried to supply only 1 value for TO and none for CC, it displays Run-time error '-2147352567 (80020009)' : There must be at least one name or contact group in the To, Cc, or Bcc box.
B.
For Each sTo in Recipients
receiver = receiver & sTo.Value & ";"
Next
For Each sCc in CC
CCs = CCs & sCc.Value & ";"
Next
But these codes lead to an unresponsive Excel file.
Is there an error with my codes? Or any suggestions on how I can make my TO and CC fields dynamic. Dynamic in a sense that I could assign ONE or MORE for TO and NONE or MORE for CC.