首页 > 解决方案 > 从存储在 Excel 中的员工 ID 获取电子邮件地址

问题描述

我需要根据员工 ID 获取电子邮件地址。

我从 Excel 中复制员工 ID 并将它们粘贴到 Outlook 邮件的“收件人”字段中。

我无法使用宏代码单击 ctrl + K。

我使用下面的代码将员工 ID 粘贴到 Outlook 应用程序中。

Sub Combinedata()

    Dim i As Integer
    Dim count As Integer
    Dim s As String
    Dim outRec As Object

    count = Cells(Rows.count, "A").End(xlUp).Row
    For i = 1 To count
        s = s & Cells(i, 1) & ";"
    Next
    'Range("D2") = s

    Set OutApp = CreateObject("Outlook.Application")

    On Error GoTo 0    
    Set OutMail = OutApp.CreateItem(0)
    On Error Resume Next
    With OutMail                   
        .To = s                  
        .Display                                    
    End With
    On Error GoTo 0
    Set OutMail = Nothing

End Sub

标签: excelvbaoutlook

解决方案


.Recipients.ResolveAll复制 ctrl + K。

Option Explicit ' Consider this mandatory
' Tools | Options | Editor tab
' Require Variable Declaration
'
' If desperate declare as variant

Sub Combinedata()

Dim i As Long
Dim count As Long

Dim s As String

Dim OutApp As Object
Dim outMail As Object

count = Cells(Rows.count, "A").End(xlUp).Row
For i = 1 To count
    s = s & Cells(i, 1) & ";"
Next

Set OutApp = CreateObject("Outlook.Application")
Set outMail = OutApp.CreateItem(0)

With outMail
    .To = s
    .Recipients.ResolveAll
    .Display
End With

End Sub

推荐阅读