使用Excel VBA向多个用户发送带有表中相应信息的电子邮件

使用Excel VBA向多个用户发送带有表中相应信息的电子邮件,excel,vba,email,html-email,Excel,Vba,Email,Html Email,我有一张表格,如下所示。我想发送一封电子邮件在一次点击所有用户与相应的PIN码 用户应收到如下电子邮件: 发送至(用户\电子邮件) 电子邮件正文: “您好(用户名)您的pin码是(密码)” 我有电子邮件服务器代码,工作正常。我只需要找到每个用户对应PIN码的值并发送给他,然后移动到下一行并执行相同的操作,直到该行为空。我想一键完成 Public Function Send() '=========== Dim variables ========= Dim str_body As String

我有一张表格,如下所示。我想发送一封电子邮件在一次点击所有用户与相应的PIN码

用户应收到如下电子邮件:

发送至(用户\电子邮件)

电子邮件正文:

“您好(用户名)您的pin码是(密码)”

我有电子邮件服务器代码,工作正常。我只需要找到每个用户对应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====================================================================```
公共函数Send()
'=============Dim变量=========
作为字符串的Dim str_主体
暗名称作为范围
暗pincode As范围
Dim user_电子邮件作为范围
'=============Dim变量=========
'==================================================================
'===================<电子邮件服务器>==================================
设置cdomsg=CreateObject(“CDO.message”)
使用cdomsg.Configuration.Fields
.项目(”http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.项目(”http://schemas.microsoft.com/cdo/configuration/smtpserver“”=“smtp.gmail.com”
.项目(”http://schemas.microsoft.com/cdo/configuration/smptserverport") = "587"
.项目(”http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.项目(”http://schemas.microsoft.com/cdo/configuration/smtpusessl“”=真
.项目(”http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
.项目(”http://schemas.microsoft.com/cdo/configuration/sendusername)等等blah@blah.com"
.项目(”http://schemas.microsoft.com/cdo/configuration/sendpassword“”=“诸如此类”
.更新
以
'==================================================================
'该过程是在数据库中查找具有相应信息的值
'==================================================================
((我需要调用值的过程))
'===================================================================
'===================================================================
使用cdomsg
strBody=strBody&“
此电子邮件是从应用程序发送的” strBody=strBody&“您好:&Name&”您的pin码是:&pincode&“
” '==================================================================== .To=用户\电子邮件 .From=”blah@gmail.com" .Subject=“Pin码应用” .HTMLBody=strBody .发送 MsgBox“登录详细信息已发送到您的电子邮件” 以 设置cdomsg=Nothing 端函数====================================================================```
与其更改现有的工作函数,最简单的方法可能是创建一个在列表中循环的子函数,每次调用该函数发送邮件。循环的方法有很多种,举个例子:

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
作为行号,使用
1
3
作为列号,使用
Cells()
属性来获取变量

然后移动要在函数(或子函数)中调用的变量,以匹配我们调用的变量:

Public Function Send(Name As String, user_email As String, pincode As String)
这将循环浏览列表并向每个用户发送邮件。

您可能希望将消息框移到主循环的末尾,除非您希望为每个发送的邮件关闭一个消息框。

为每个循环一个简单的
,调用
Send()
函数?1。查找列A中的最后一行,如图2所示。循环浏览列
A
中的行,例如
for i=2 to LastRow
,然后选择
user\u email=Range(“B”&i).Value2
等值。我现在将尝试并给您反馈:)