繁体   English   中英

VBA:循环打开多封电子邮件

[英]VBA: Opening Multiple Emails in Loop

现在,如果在 Column1 中找到用户表单 (TextBox1INC) 中给出的 ID,我有打开电子邮件的代码,但假设我有两封电子邮件或任何数字,我想打开所有这些,而不仅仅是一。 如何将循环放入此代码中以使其工作?

Private Sub CommandButton8showemail_Click()

Dim wsArch As Worksheet
Dim lastrow, a As Long
Dim strEmail, strEmailLoc As String
Dim OutMejlik As Outlook.Application
Dim msg As Outlook.MailItem

Set wsArch = ThisWorkbook.Sheets("Emails_arch")
lastrow = Sheets("Emails_arch").Range("A" & Rows.Count).End(xlUp).Row

With wsArch
    For a = lastrow To 2 Step -1
        If .Cells(a, 1).Value = TextBox1INC.Text Then
        strEmailLoc = .Cells(a, 2).Value
        Set OutMejlik = CreateObject("Outlook.Application")
        Set msg = OutMejlik.Session.OpenSharedItem(strEmailLoc)
        msg.Display
        Exit Sub
        End If
    Next a
End With

End Sub

目前在循环中,一旦第一个项目显示给用户,您就会退出:

    For a = lastrow To 2 Step -1
        If .Cells(a, 1).Value = TextBox1INC.Text Then
        strEmailLoc = .Cells(a, 2).Value
        Set OutMejlik = CreateObject("Outlook.Application")
        Set msg = OutMejlik.Session.OpenSharedItem(strEmailLoc)
        msg.Display
        Exit Sub
        End If
    Next a
End With

如果删除Exit Sub部分,代码将继续运行并根据需要打开项目。 但我也建议在循环外创建一个新的 Outlook 应用程序以避免每次都创建(即使 Outlook 是一个 singleton 并且只能创建一个实例)。

Set OutMejlik = CreateObject("Outlook.Application")

With wsArch
    For a = lastrow To 2 Step -1
        If .Cells(a, 1).Value = TextBox1INC.Text Then
          strEmailLoc = .Cells(a, 2).Value
         
          Dim msg As Outlook.MailItem
          Set msg = OutMejlik.Session.OpenSharedItem(strEmailLoc)
          msg.Display
        End If
    Next a
End With

暂无
暂无

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

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