简体   繁体   English

如何将一封电子邮件发送到 Excel 工作簿中的电子邮件列表? excel VBA

[英]How to send one email to list of emails in Excel workbook? Excel VBA

This is my current code, but I want to be able to send to a list of emails that is in my workbook.这是我当前的代码,但我希望能够发送到我的工作簿中的电子邮件列表。 How would I go about that with what I have for the mailing section of my code.我将如何使用代码的邮件部分来解决这个问题。 I want to have column R named mailing list and it will send to whatever emails are inserted into that column/list all together.我想将 R 列命名为邮件列表,它将一起发送到插入该列/列表中的任何电子邮件。 Think when I try stuff, I'm missing a component somehow.想想当我尝试东西时,我不知何故丢失了一个组件。

Sub SendReminderMail1()

  Dim wb1 As Workbook
  Dim wb2 As Workbook
  Dim TempFilePath As String
  Dim TempFileName As String
  Dim FileExtStr As String
  Dim OutApp As Object
  Dim OutMail As Object



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

Set wb1 = ActiveWorkbook


TempFilePath = Environ$("temp") & "\"
TempFileName = "Copy of " & wb1.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")
FileExtStr = "." & LCase(Right(wb1.Name, Len(wb1.Name) - InStrRev(wb1.Name, ".", , 1)))

wb1.SaveCopyAs TempFilePath & TempFileName & FileExtStr
Set wb2 = Workbooks.Open(TempFilePath & TempFileName & FileExtStr)

wb2.Worksheets(1).Range("A1").Value = "Copy created on " & Format(Date, "dd-mmm-yyyy")

wb2.Save


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


On Error Resume Next
With OutMail
    .To = " "
    .CC = ""
    .BCC = ""
    .Subject = "Rotations needed for ."
    .Body = "Hey there,  equipment needs to be rotated."
    .Attachments.Add wb2.FullName

    .Display   'or use .Send to send with display proof reading

End With
On Error GoTo 0
wb2.Close savechanges:=False

Kill TempFilePath & TempFileName & FileExtStr

Set OutMail = Nothing
Set OutApp = Nothing

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


MsgBox "Your Automated Email for BP Rotations was successfully ran at " & TimeValue(Now), vbInformation

End Sub

In your code I've you set the recipients fields to empty strings:在您的代码中,我将收件人字段设置为空字符串:

With OutMail
    .To = " "
    .CC = ""
    .BCC = ""

Instead, you need to read values from the column R and add recipients for the email.相反,您需要从 R 列读取值并为电子邮件添加收件人。 To add recipients I'd recommend using the Recipients collection which can be retrieved using the corresponding property of the MailItem class.要添加收件人,我建议使用Recipients集合,该集合可以使用MailItem类的相应属性进行检索。

Dim recipients As Outlook.Recipients = Nothing

Set recipients = mail.Recipients

' now we add new recipietns to the e-mail
        recipientTo = recipients.Add("Eugene")
        recipientTo.Type = Outlook.OlMailRecipientType.olTo
        recipientCC = recipients.Add("Dmitry")
        recipientCC.Type = Outlook.OlMailRecipientType.olCC
        recipientBCC = recipients.Add("eugene.astafiev@somedomain.com")
        recipientBCC.Type = Outlook.OlMailRecipientType.olBCC
        recipients.ResolveAll()

Read more about that in the How To: Fill TO,CC and BCC fields in Outlook programmatically article.如何:以编程方式填写 Outlook中的收件人、抄送和密件抄送字段一文中了解更多相关信息。

Mail Merge not your cup of tea huh...邮件合并不是你的那杯茶吧...

Maybe what you need is a Do while Loop where it references a cell in a Table of people, then moves down each step till the E-mail is blank just chugging through Row after Row of E-mails driving that sweet CPU usage up.也许您需要的是一个 Do while 循环,它引用人员表中的一个单元格,然后向下移动每一步,直到电子邮件是空白的,只是通过一排又一排的电子邮件来推动甜蜜的 CPU 使用率。

Like a user programmed Mail Merge but in Excel and not in a Word Processor... Like Mail Merge... In Word, but not in Word, In Excel In VBA...就像用户编程的邮件合并但在 Excel 中而不是在字处理器中......就像邮件合并......在 Word 中,但不是在 Word 中,在 Excel 中在 VBA 中......

在此处输入图片说明

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

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