[英]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: 其中包含:
2) User Information (number of users can vary) 2)用户信息(用户数量可能有所不同)
This contains: 其中包含:
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创建新的独立电子邮件:
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. 根据讨论,这是我对该解决方案的编辑。
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.