简体   繁体   English

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

[英]VBA - Sending Emails Through Outlook Based on Cell Data

I've been failing miserably trying to write code for this, so I'd be happy if someone could help me create a macro in excel. 我一直为此尝试编写代码而失败,所以如果有人可以帮助我在excel中创建宏,我会很高兴。 I'm looking to send a bunch of users access credentials through outlook based off data I add to excel. 我希望根据我添加到excel中的数据,通过Outlook发送大量用户访问凭据。 Specifically, I have two worksheets: 具体来说,我有两个工作表:

1) Email Information (all static) 1)电子邮件信息(均为静态)

This contains: 其中包含:

  • Email Subject in cell C5 单元格C5中的电子邮件主题
  • Email Body in Cell C6 (Essentially this says Hello, your user credentials are below) 单元格C6中的电子邮件正文(基本上是说Hello,您的用户凭据在下面)
  • Additional Email Body in Cell C7 (This portion would say something along the lines of "please let us know if you have any questions") Both cells C6 and C7 can of course be updated to include any language 单元格C7中的其他电子邮件正文(此部分将说明“请问您是否有任何疑问”)单元格C6和C7当然都可以更新为包含任何语言

2) User Information (number of users can vary) 2)用户信息(用户数量可能有所不同)

This contains: 其中包含:

  • Column A - First Name A栏-名
  • Column B - Last Name B栏-姓氏
  • Column C - Full Name (Not really needed) C列-全名(并非必需)
  • Column D - Email Address D列-电子邮件地址
  • Column E - Password E栏-密码

Ideally, the macro would be able to look at the user information and create a new, separate email from outlook for every email address from column D with the following format: 理想情况下,该宏将能够查看用户信息,并使用以下格式为D列中的每个电子邮件地址从Outlook创建新的独立电子邮件:

  • Email To: email addresses in cell D2 until last email (User Information worksheet) 电子邮件收件人:单元格D2中的电子邮件地址,直到上一封电子邮件为止(用户信息工作表)
  • Email Subject: Cell C5 in Email information worksheet 电子邮件主题:电子邮件信息工作表中的单元格C5
  • "Hi" Firstname value from column A in User Information worksheet 用户信息工作表中列A的“ Hi”名字值
  • Email Body Part 1 from cell C6 in Email Information worksheet 电子邮件信息工作表中单元格C6的电子邮件正文部分1
  • Username: which is the email address from column D (same as email recipient) 用户名:这是D列中的电子邮件地址(与电子邮件收件人相同)
  • Password: from column E in User Information worksheet 密码:来自用户信息工作表中的E列
  • Email Body PArt 2 from cell C7 in Email Information worksheet 电子邮件信息工作表中单元格C7的电子邮件正文部分2

Hope someone has the time to help me out. 希望有人有时间帮助我。

Thanks in advance!! 提前致谢!!

EDIT 编辑

Thanks for the help, Barry. 感谢您的帮助,Barry。 Here is my code as I'm trying to reference two different worksheets. 这是我尝试引用两个不同工作表时的代码。 Can you let me know what I'm doing wrong? 你能让我知道我在做什么错吗?

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

End Sub 结束子

Based on discussions this is my edit for this solution. 根据讨论,这是我对该解决方案的编辑。

Excel Macro 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

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

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