【问题标题】:How to loop sending emails如何循环发送电子邮件
【发布时间】:2014-05-31 05:24:50
【问题描述】:

我有一个文件夹test,其中包含以下文件user1.xlsx , user2.xlsx , user3.xlsx 在我的工作电子表格work.xlsx 我有相应的地址

user1.xlsx  user1name@gmail.com
user2.xlsx  user2name@yahoo.com
 ...

如何将带有附加 user1 、 user2 .xlsx 文件的电子邮件发送到相应的电子邮件

'Email
Dim OutApp As Object
    Dim OutMail As Object
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    On Error Resume Next
    With OutMail
        .To = userVar
        .SentOnBehalfOfName = "xxxx"
        .CC = ""
        .BCC = ""
        .Subject = "...
        .Body = "...
        .Attachments. .. ??
        .Send
    End With
    On Error GoTo 0
    Set OutMail = Nothing
    Set OutApp = Nothing


'Close

【问题讨论】:

    标签: vba email


    【解决方案1】:

    您查看过this MS KB 吗?它详细介绍了用于发送电子邮件的 VBA,如下所示:

    Sub SendMessage(DisplayMsg As Boolean, Optional AttachmentPath)
          Dim objOutlook As Outlook.Application
          Dim objOutlookMsg As Outlook.MailItem
          Dim objOutlookRecip As Outlook.Recipient
          Dim objOutlookAttach As Outlook.Attachment
    
          ' Create the Outlook session.
          Set objOutlook = CreateObject("Outlook.Application")
    
          ' Create the message.
          Set objOutlookMsg  = objOutlook.CreateItem(olMailItem)
    
          With objOutlookMsg
              ' Add the To recipient(s) to the message.
              Set objOutlookRecip = .Recipients.Add("Nancy Davolio")
              objOutlookRecip.Type = olTo
    
              ' Add the CC recipient(s) to the message.
              Set objOutlookRecip = .Recipients.Add("Michael Suyama")
              objOutlookRecip.Type = olCC
    
             ' Add the BCC recipient(s) to the message.
              Set objOutlookRecip = .Recipients.Add("Andrew Fuller")
              objOutlookRecip.Type = olBCC
    
             ' Set the Subject, Body, and Importance of the message.
             .Subject = "This is an Automation test with Microsoft Outlook"
             .Body = "This is the body of the message." &vbCrLf & vbCrLf
             .Importance = olImportanceHigh  'High importance
    
             ' Add attachments to the message.
             If Not IsMissing(AttachmentPath) Then
                 Set objOutlookAttach = .Attachments.Add(AttachmentPath)
             End If
    
             ' Resolve each Recipient's name.
             For Each ObjOutlookRecip In .Recipients
                 objOutlookRecip.Resolve
             Next
    
             ' Should we display the message before sending?
             If DisplayMsg Then
                 .Display
             Else
                 .Save
                 .Send
             End If
          End With
          Set objOutlook = Nothing
      End Sub
    

    请注意,您必须使用 AttachmentPath Set objOutlookAttach,这与文件的位置(硬编码或use current directory as path)。您的循环应该针对指定范围内的每个电子邮件地址,获取相应的文件名(从相邻单元格),将其附加到 AttachmentPath 变量,然后用于设置 objOutlookAttach。

    更新:可以在here 找到更多最新的相关 MS 文章以获取更多参考和指导。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-05
      • 2021-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-22
      相关资源
      最近更新 更多