VBA-通过基于单元格数据的Outlook发送电子邮件

时间:2018-08-30 16:40:00

标签: excel vba excel-vba

我一直为此尝试编写代码而失败,所以如果有人可以帮助我在excel中创建宏,我会很高兴。我希望根据我添加到excel中的数据,通过Outlook发送大量用户访问凭据。具体来说,我有两个工作表:

1)电子邮件信息(均为静态)

其中包含:

  • 在单元格C5中发送电子邮件主题
  • 单元格C6中的电子邮件正文(本质上说,您好,您的用户凭据在下面)
  • 单元格C7中的其他电子邮件正文(此部分将说明“请问您是否有任何疑问”),当然可以对单元格C6和C7进行更新以包含任何语言

2)用户信息(用户数可以变化)

其中包含:

  • A列-名
  • B列-姓氏
  • C列-全名(并非真正需要)
  • D列-电子邮件地址
  • E列-密码

理想情况下,该宏将能够查看用户信息,并使用以下格式为D列中的每个电子邮件地址从Outlook创建新的独立电子邮件:

  • 电子邮件收件人:单元格D2中的电子邮件地址,直到最后一封电子邮件为止(用户信息工作表)
  • 电子邮件主题:电子邮件信息工作表中的单元格C5
  • “用户信息”工作表A列中的“嗨”姓氏值
  • 电子邮件信息工作表中单元格C6中的电子邮件正文第1部分
  • 用户名:D列中的电子邮件地址(与电子邮件收件人相同)
  • 密码:来自用户信息工作表中的E列
  • 电子邮件信息工作表中单元格C7中的电子邮件正文部分2

希望有人有时间帮助我。

提前谢谢!

EDIT

感谢您的帮助,Barry。这是我尝试引用两个不同工作表时的代码。你能让我知道我在做什么错吗?

Sub GenerateEmail()
Dim sEmailBodyp1 As String
Dim sEmailBodyp2 As String
Dim sEmailSubject As String
Dim sEmailTo As String
Dim sFirstName As String
Dim sPassword As String
Dim OutApp As Object
Dim OutMail As Object
Dim EmailSheet As Worksheet
Dim UserSheet As Worksheet
Dim UsedRange As Range

Set EmailSheet = Sheets("Email Information")
Set UserSheet = Sheets("User Information")
Set sEmailSubject = EmailSheet.Cells("C5")
Set sEmailBodyp1 = EmailSheet.Cells("C6")
Set sEmailBodyp2 = EmailSheet.Cells("C7")
Set UsedRange = UserSheet.UsedRange

For Each Row In UsedRange.Rows
    sFirstName = Row.Columns(1)
    sEmailTo = Row.Columns(4)
    sPassword = Row.Columns(5)
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    With OutMail
        .To = sEmailTo
        .Subject = sEmailSubject
        .Body = "Hi " + sFirstName + "," + vbCrLf + vbCrLf + sEmailBodyp1 + vbCrLf + vbCrLf + "Username: " + sEmailTo + vbCrLf + "Password: " + sPassword + vbCrLf + vbCrLf + sEmailBodyp2
        .Display
    End With

    Set OutMail = Nothing
Next

Set OutApp = Nothing

结束子

1 个答案:

答案 0 :(得分:0)

根据讨论,这是我对该解决方案的修改。

Excel宏

Public Sub GenerateEmail()
Dim sEmailBodyp1 As String
Dim sEmailBodyp2 As String
Dim sEmailSubject As String
Dim sEmailTo As String
Dim sFirstName As String
Dim sPassword As String
Dim OutApp As Object
Dim OutMail As Object
Dim EmailSheet As Worksheet
Dim UserSheet As Worksheet
Dim UsedRange As Range

Set EmailSheet = Sheets("Email Information")
Set UserSheet = Sheets("User Information")

sEmailSubject = EmailSheet.Range("C5").Value
sEmailBodyp1 = EmailSheet.Range("C6").Value
sEmailBodyp2 = EmailSheet.Range("C7").Value

Set UsedRange = UserSheet.UsedRange

For Each Row In UsedRange.Rows.Offset(1, 0).Resize(UsedRange.Rows.Count - 1, UsedRange.Columns.Count)

        sFirstName = Row.Columns(1)
        sEmailTo = Row.Columns(4)
        sPassword = Row.Columns(5)
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)

        With OutMail
            .To = sEmailTo
            .Subject = sEmailSubject
            .Body = "Hi " + sFirstName + "," + vbCrLf + vbCrLf + sEmailBodyp1 + vbCrLf + vbCrLf + "Username: " + sEmailTo + vbCrLf + "Password: " + sPassword + vbCrLf + vbCrLf + sEmailBodyp2
            .Display
        End With

        Set OutMail = Nothing

Next

Set OutApp = Nothing
End Sub