如何根据另一列中的选择将列中的电子邮件地址放入 Outlook 电子邮件的密件抄送字段中

How to put email address from column into BCC field of Outlook e-mail based on selection in another column

在 Excel 中,我创建了按钮并显示了一个用户窗体。我有 select 一系列电子邮件地址的代码。

我有 sheet 3 列:姓名、电子邮件地址和每个电子邮件地址的辅助电子邮件地址,我想将其添加到密件抄送字段。

Combobox1 显示电子邮件地址并将其传送到外发邮件。[​​=32=] 我找不到将下一列中的电子邮件地址添加到密件抄送字段的方法。

为清楚起见,我想 select 一个名称(第 1 列)并将一个电子邮件地址(第 2 列)传输到 .To 字段和另一个电子邮件地址(第 3 列)到 .BCC 字段。

我试过了:

How do I populate a combo box from a column in my excel spread sheet?

populate combobox in VBA with array elements

https://www.codeproject.com/Articles/401098/A-multi-selection-Drop-Down-List-using-a-generic-A

Private Sub CommandButton1_Click()

    Dim AppOutlook As Outlook.application
    Dim Mailtje As Outlook.MailItem

    Set AppOutlook = CreateObject("Outlook.Application")
    Set Mailtje = AppOutlook.CreateItem(olMailItem)
    
    Mailtje.Display
    Mailtje.To = ComboBox1.Value
    Mailtje.CC = TextBox1.Value
    Mailtje.BCC = ?
    Mailtje.Subject = ""
    Mailtje.HTMLBody = ""

End Sub

Private Sub CommandButton2_Click()

    Unload Me
        
End Sub

Private Sub TextBox1_Change()

End Sub

Private Sub UserForm_Initialize()

    Dim N As Long, i As Long
    
    With Sheets("Medewerkers")
        N = .Cells(Rows.Count, 1).End(xlUp).Row
    End With

    With ComboBox1
        .Clear
        For i = 2 To N
            .AddItem Sheets("Medewerkers").Cells(i, 2).Value
        Next i
    End With
End Sub

Vlookup 是正确的选择!非常感谢 @Tragamor 的建议。

在用户表单中,您可以select组合框中的名称(“A”行)并输入主题:

代码从此处显示的单元格中检索数据:

是的,我确实喜欢大按钮 :)

Private Sub CommandButton1_Click()

    Dim AppOutlook As Outlook.Application
    Dim Mailtje As Outlook.MailItem
    Dim rng As Range
    Dim rng2 As Range
    Dim xTo As String
    Dim xBCC As String
    
    Set rng = Range("A:B")
    Set rng2 = Range("A:C")
    
    xTo = Application.WorksheetFunction.VLookup(ComboBox1.Value, rng, 2, False)
    xBCC = Application.WorksheetFunction.VLookup(ComboBox1.Value, rng2, 3, False)
    
    Set AppOutlook = CreateObject("Outlook.Application")
    Set Mailtje = AppOutlook.CreateItem(olMailItem)
        
    Mailtje.Display
    Mailtje.To = xTo
    Mailtje.CC = Sheets("Medewerkers").Range("G2").Value
    Mailtje.BCC = xBCC
    Mailtje.Subject = TextBox1.Value
    Mailtje.HTMLBody = ""
    

End Sub

Private Sub CommandButton2_Click()

    Unload Me
        
End Sub

Private Sub UserForm_Initialize()

    Dim N As Long, i As Long
    
    With Sheets("Medewerkers")
        N = .Cells(Rows.Count, 1).End(xlUp).Row
    End With

    With ComboBox1
        .Clear
        For i = 2 To N
            .AddItem Sheets("Medewerkers").Cells(i, 1).Value
        Next i
    End With
End Sub

将 ComboBox 扩展到所有 3 列,并根据需要隐藏 2 列。

Option Explicit

Private Sub CommandButton1_Click()

    Dim AppOutlook As Outlook.Application
    Dim Mailtje As Outlook.MailItem
    Dim xTo As String, xBCC As String
    Dim i As Long
    
    With Me.ComboBox1
        i = .ListIndex
        If i < 0 Then
            MsgBox "Nothing selected", vbExclamation
            Exit Sub
        End If
        xTo = .List(i, 1)
        xBCC = .List(i, 2)
    End With
    
    Set AppOutlook = CreateObject("Outlook.Application")
    Set Mailtje = AppOutlook.CreateItem(olMailItem)
    With Mailtje
        .To = xTo
        .CC = Sheets("Medewerkers").Range("G2").Value
        .BCC = xBCC
        .Subject = TextBox1.Value
        .HTMLBody = ""
        .Display
    End With
    
End Sub

Private Sub UserForm_Initialize()

    Dim n As Long
    With Sheets("Medewerkers")
        n = .Cells(Rows.Count, 1).End(xlUp).Row
    End With

    With ComboBox1
        .Clear
        .ColumnCount = 3
        .ColumnWidths = ";0;0" ' zero width to hide
        .ColumnHeads = True
        .RowSource = "Medewerkers!A2:C" & n
    End With
    
End Sub