向 table 和 Excel VBA 中包含相应信息的多个用户发送电子邮件
Sending email to multiple users with corresponding information in the table with Excel VBA
我有一个 table 如下信息。我想通过单击向所有具有相应 PIN 码的用户发送一封电子邮件。
用户应该收到这样的电子邮件:
Send to (User_Email)
body of the email :
" Hello (user_name) your pin code is (code)"
我有电子邮件服务器代码并且工作正常。我只需要为每个用户找到相应 PIN 码的值并将其发送给他的功能过程,然后移动到下一行并做同样的事情直到该行为空。我想一键搞定
Public Function Send()
'=========== Dim variables =========
Dim str_body As String
Dim Name As Range
Dim pincode As Range
Dim user_email As Range
'=========== Dim variables =========
'==================================================================
'================< Email server >==================================
Set cdomsg = CreateObject("CDO.message")
With cdomsg.Configuration.Fields
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/smptserverport") = "587"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "blah blah@blah.com"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "blah"
.Update
End With
'==================================================================
' The procedure is to find the value in database with correspond information
'==================================================================
(( I need the procedure to call the values ))
'===================================================================
'===================================================================
With cdomsg
strBody = strBody & "<span><br/>This Email was sent from Application </span>"
strBody = strBody & "<span>Hello : " & Name & " your pin code is: " & pincode & "</span><br/>"
'====================================================================
.To = user_email
.From = "blah@gmail.com"
.Subject = "Pin code application"
.HTMLBody = strBody
.Send
MsgBox "Login details has been sent to your Email"
End With
Set cdomsg = Nothing
End Function====================================================================```
与其更改现有的工作函数,最简单的方法可能是创建一个循环遍历列表的子函数,每次都调用该函数来发送邮件。循环的方式有很多种,举个例子:
Sub MainLoop()
For i = 2 To Range("A" & Rows.Count).End(xlUp).Row
Call Send(Cells(i, 1), Cells(i, 2), Cells(i, 3))
Next
End Sub
这从第 2 行开始 i = 2
一直到最后一行。由于我们需要的数据总是在同一列中,因此我们使用 i
作为行号,使用 1
到 3
作为列号,并使用 Cells()
属性 来获取我们的变量。
然后移动要在函数(或子函数)中调用的变量以匹配我们调用的内容:
Public Function Send(Name As String, user_email As String, pincode As String)
这将遍历列表并向每个用户发送一封邮件。
您可能希望将消息框移至主循环的末尾,除非您希望针对每封发送的邮件关闭一个消息框。
我有一个 table 如下信息。我想通过单击向所有具有相应 PIN 码的用户发送一封电子邮件。
用户应该收到这样的电子邮件:
Send to (User_Email)
body of the email :
" Hello (user_name) your pin code is (code)"
我有电子邮件服务器代码并且工作正常。我只需要为每个用户找到相应 PIN 码的值并将其发送给他的功能过程,然后移动到下一行并做同样的事情直到该行为空。我想一键搞定
Public Function Send()
'=========== Dim variables =========
Dim str_body As String
Dim Name As Range
Dim pincode As Range
Dim user_email As Range
'=========== Dim variables =========
'==================================================================
'================< Email server >==================================
Set cdomsg = CreateObject("CDO.message")
With cdomsg.Configuration.Fields
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/smptserverport") = "587"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "blah blah@blah.com"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "blah"
.Update
End With
'==================================================================
' The procedure is to find the value in database with correspond information
'==================================================================
(( I need the procedure to call the values ))
'===================================================================
'===================================================================
With cdomsg
strBody = strBody & "<span><br/>This Email was sent from Application </span>"
strBody = strBody & "<span>Hello : " & Name & " your pin code is: " & pincode & "</span><br/>"
'====================================================================
.To = user_email
.From = "blah@gmail.com"
.Subject = "Pin code application"
.HTMLBody = strBody
.Send
MsgBox "Login details has been sent to your Email"
End With
Set cdomsg = Nothing
End Function====================================================================```
与其更改现有的工作函数,最简单的方法可能是创建一个循环遍历列表的子函数,每次都调用该函数来发送邮件。循环的方式有很多种,举个例子:
Sub MainLoop()
For i = 2 To Range("A" & Rows.Count).End(xlUp).Row
Call Send(Cells(i, 1), Cells(i, 2), Cells(i, 3))
Next
End Sub
这从第 2 行开始 i = 2
一直到最后一行。由于我们需要的数据总是在同一列中,因此我们使用 i
作为行号,使用 1
到 3
作为列号,并使用 Cells()
属性 来获取我们的变量。
然后移动要在函数(或子函数)中调用的变量以匹配我们调用的内容:
Public Function Send(Name As String, user_email As String, pincode As String)
这将遍历列表并向每个用户发送一封邮件。
您可能希望将消息框移至主循环的末尾,除非您希望针对每封发送的邮件关闭一个消息框。