简体   繁体   English

如何在向唯一人发送一组行的同时在 VBA 宏代码中嵌入 CC 和 BCC

[英]How to Embedd CC and BCC in the VBA Macro Code while send set of rows to unique person

I have got a macro which would eMail a row or rows to each person in a range.我有一个宏,可以将一行或多行通过电子邮件发送给范围内的每个人。 I just want to know how to add CC and BCC which are same in every email.I am amature to Excel VBA.我只想知道如何在每封电子邮件中添加相同的 CC 和 BCC。我对 Excel VBA 很熟。 Please help.请帮忙。

here is the code这是代码

Sub Send_Row_Or_Rows_1()

Dim OutApp As Object
Dim OutMail As Object
Dim rng As Range
Dim Ash As Worksheet
Dim Cws As Worksheet
Dim Rcount As Long
Dim Rnum As Long
Dim FilterRange As Range
Dim FieldNum As Integer
Dim mailAddress As String
Dim StrBody As String

On Error GoTo cleanup
Set OutApp = CreateObject("Outlook.Application")

With Application
    .EnableEvents = False
    .ScreenUpdating = False
End With


Set Ash = ActiveSheet

'Set filter range and filter column (Column with names)
Set FilterRange = Ash.Range("A1:H" & Ash.Rows.Count)
FieldNum = 1    'Filter column = A because the filter range start in A

'Add a worksheet for the unique list and copy the unique list in A1
Set Cws = Worksheets.Add
FilterRange.Columns(FieldNum).AdvancedFilter _
        Action:=xlFilterCopy, _
        CopyToRange:=Cws.Range("A1"), _
        CriteriaRange:="", Unique:=True

'Count of the unique values + the header cell
Rcount = Application.WorksheetFunction.CountA(Cws.Columns(1))

'If there are unique values start the loop
If Rcount >= 2 Then
    For Rnum = 2 To Rcount

        'Filter the FilterRange on the FieldNum column
        FilterRange.AutoFilter Field:=FieldNum, _
                               Criteria1:=Cws.Cells(Rnum, 1).Value

        'Look for the mail address in the MailInfo worksheet
        mailAddress = ""
        On Error Resume Next
        mailAddress = Application.WorksheetFunction. _
                      VLookup(Cws.Cells(Rnum, 1).Value, _
                            Worksheets("Mailinfo").Range("A1:B" & _
                            Worksheets("Mailinfo").Rows.Count), 2, False)
        On Error GoTo 0

        If mailAddress <> "" Then
            With Ash.AutoFilter.Range
                On Error Resume Next
                Set rng = .SpecialCells(xlCellTypeVisible)
                On Error GoTo 0
            End With

            Set OutMail = OutApp.CreateItem(0)

            On Error Resume Next

            With OutMail
                .to = mailAddress
                .Subject = "Test mail"
                .HTMLBody = StrBody & RangetoHTML(rng)
                .Display  'Or use Send

                StrBody = Sheets("Sheet2").Range("A1").Value & "<br>" & "<br>" & _
          Sheets("Sheet2").Range("A2").Value & "<br>" & "<br>" & _
          Sheets("Sheet2").Range("A3").Value & "<br><br><br>"

            End With
            On Error GoTo 0

            Set OutMail = Nothing
        End If

        'Close AutoFilter
        Ash.AutoFilterMode = False

    Next Rnum
End If
cleanup:
Set OutApp = Nothing
Application.DisplayAlerts = False
Cws.Delete
Application.DisplayAlerts = True

With Application
    .EnableEvents = True
    .ScreenUpdating = True
End With
End Sub

Should be应该

         With OutMail
                .to = mailAddress
                .cc = "email address"
                .Bcc ="email address"

If you want to add more than one email then如果您想添加不止一封电子邮件,那么

.cc = "email address; email address"

I will advise using a separate sub-routine for sending the email.我会建议使用单独的子程序来发送电子邮件。 Use the existing sub-routine to classify the data and call the below sub-routine whenever you want to send the email.使用现有的子程序对数据进行分类,并在您想要发送电子邮件时调用以下子程序。 This will resolve your problem of adding and resolving the bcc and cc mail addresses and in addition, will do excellent memory management with the outlook instance.这将解决您添加和解析 bcc 和 cc 邮件地址的问题,此外,还将对 Outlook 实例进行出色的内存管理。

Please use the below code:请使用以下代码:

Sub SendEmail(ByVal str_To_EmailAddress As String, ByVal strSubject As String, ByVal strHTMLBody As String)
Dim OutApp As Object
Dim oMsg As Object
Dim objRecip As Object

Dim str_CC_EmailAddress As String
Dim str_BCC_EmailAddress As String

Set OutApp = CreateObject("Outlook.Application")
Set oMsg = OutApp.ActiveInspector.CurrentItem

str_CC_EmailAddress = "ABC@example.com"
str_BCC_EmailAddress = "XYZ@example.com"

With oMsg
    'Add to Email Address
    Set objRecip = oMsg.Recipients.Add(strToEmailAddress)
    objRecip.Type = olTo
    objRecip.Resolve

    'Add CC Email Address
    Set objRecip = oMsg.Recipients.Add(str_CC_EmailAddress)
    objRecip.Type = olCC
    objRecip.Resolve

    'Add BCC Email Address
    Set objRecip = oMsg.Recipients.Add(str_BCC_EmailAddress)
    objRecip.Type = olBCC
    objRecip.Resolve

    'Add Subject
    .Subject = strSubject

    'Add Body
    .BodyFormat = olFormatHTML

    'Display or Send
    .Display '.Send
End With

Set oMsg = Nothing

End Sub

Please construct the strings of email addresses separated by semicolons (;).请构造以分号 (;) 分隔的电子邮件地址字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM