我已将许多Excel工作表导入Access准备导入Salesforce.com(统一并将不同的字段名称映射到SFDC将识别的字段),因此我可以从任何一个表批量发送电子邮件( Excel表格)通过VBA和Outlook。
我发现代码在我身上非常适合我:http://www.jephens.com/2007/05/13/how-to-send-e-mail-from-ms-access-using-outlook/它就像一个魅力。我特别喜欢它一个接一个地发送电子邮件而不是每个BCC的大量消息。
我想做的是更改代码以获得更多功能,但我对VBA的了解非常有限。
我的问题是双重的。第一种是当我的表没有列表中每个条目的电子邮件地址时,查询会出错。我假设这是一个简单的问题,即丢弃一些代码,告诉查询跳过丢失的电子邮件地址并转到下一个。然而,我不太了解VBA,所以我希望有人可以帮助我解决我应该放置代码的地方和位置。
第二件事是我设置了电子邮件,因此任何收到电子邮件的收件人都可以通过退回电子邮件取消订阅来退出。然后我回到我通过电子邮件发送的表格(列表),并在我创建的“电子邮件退出”字段下面的条目旁边放一个“1”(SFDC也认识到这一点)。我还希望能够查询“电子邮件退出”字段,并让VBA跳过它找到的任何条目1,以便将来不会收到电子邮件
希望我很清楚......我正在使用Access 2013和Outlook 2010。
这是'SendMail'模块背后的VBA代码,当我启动'SendEMail'宏时,它就会发生这一切:
Option Compare Database
Public Function SendEMail()
Dim db As DAO.Database
Dim MailList As DAO.Recordset
Dim MyOutlook As Outlook.Application
Dim MyMail As Outlook.MailItem
Dim Subjectline As String
Dim BodyFile As String
Dim fso As FileSystemObject
Dim MyBody As TextStream
Dim MyBodyText As String
Set fso = New FileSystemObject
' First, we need to know the subject.
Subjectline$ = InputBox$("Please enter the subject line for this mailing.", _
"We Need A Subject Line!")
' If there??s no subject, call it a day.
If Subjectline$ = "" Then
MsgBox "No subject line, no message." & vbNewLine & vbNewLine & _
"Quitting...", vbCritical, "E-Mail Merger"
Exit Function
End If
' Now we need to put something in our letter...
BodyFile$ = InputBox$("Please enter the filename of the body of the message.", _
"We Need A Body!")
' If there??s nothing to say, call it a day.
If BodyFile$ = "" Then
MsgBox "No body, no message." & vbNewLine & vbNewLine & _
"Quitting...", vbCritical, "I Ain??t Got No-Body!"
Exit Function
End If
' Check to make sure the file exists...
If fso.FileExists(BodyFile$) = False Then
MsgBox "The body file isn??t where you say it is. " & vbNewLine & vbNewLine & _
"Quitting...", vbCritical, "I Ain??t Got No-Body!"
Exit Function
End If
' Since we got a file, we can open it up.
Set MyBody = fso.OpenTextFile(BodyFile, ForReading, False, TristateUseDefault)
' and read it into a variable.
MyBodyText = MyBody.ReadAll
' and close the file.
MyBody.Close
' Now, we open Outlook for our own device..
Set MyOutlook = New Outlook.Application
' Set up the database and query connections
Set db = CurrentDb()
Set MailList = db.OpenRecordset("MyEmailAddresses")
' now, this is the meat and potatoes.
' this is where we loop through our list of addresses,
' adding them to e-mails and sending them.
Do Until MailList.EOF
' This creates the e-mail
Set MyMail = MyOutlook.CreateItem(olMailItem)
' This addresses it
MyMail.To = MailList("email")
'This gives it a subject
MyMail.Subject = Subjectline$
'This gives it the body
MyMail.HTMLBody = MyBodyText
'If you want to send an attachment
'uncomment the following line
'MyMail.Attachments.Add "c:myfile.txt", olByValue, 1, "My Displayname"
' To briefly describe:
' "c:myfile.txt" = the file you want to attach
' olByVaue = how to pass the file. olByValue attaches it, olByReference creates a shortcut.
' the shortcut only works if the file is available locally (via mapped or local drive)
' 1 = the position in the outlook message where to attachment goes. This is ignored by most
' other mailers, so you might want to ignore it too. Using 1 puts the attachment
' first in line.
' "My Displayname" = If you don??t want the attachment??s icon string to be "c:myfile.txt" you
' can use this property to change it to something useful, i.e. "4th Qtr Report"
'This sends it!
MyMail.Send
'Some people have asked how to see the e-mail
'instead of automaticially sending it.
'Uncomment the next line
'And comment the "MyMail.Send" line above this.
'MyMail.Display
'And on to the next one...
MailList.MoveNext
Loop
'Cleanup after ourselves
Set MyMail = Nothing
'Uncomment the next line if you want Outlook to shut down when its done.
'Otherwise, it will stay running.
'MyOutlook.Quit
Set MyOutlook = Nothing
MailList.Close
Set MailList = Nothing
db.Close
Set db = Nothing
End Function
答案 0 :(得分:1)
我会通过创建查询来删除不需要的记录,如下所示:
<强>替换强>
Set MailList = db.OpenRecordset("MyEmailAddresses")
。通过强>
Dim qd As DAO.QueryDef
Set qd = db.CreateQueryDef("")
qd.SQL = "SELECT * FROM MyEmailAddresses WHERE email IS NOT NULL And Len(Trim(email)) > 0 And OptOut <> 1"
Set MailList = qd.Openrecordset()
' adding them to e-mails and sending them.
Do Until MailList.EOF
... your emailing code ...
答案 1 :(得分:0)
我不是百分之百确定这是正确的,因为我在VBA中生锈了 您需要确保字段的名称正确,因为我猜了。
跳转到
If (MailList("email") <> "" AND MailList("opt_out") <> 1) Then
并确保它具有正确的字段。
其次,当你做
时db.OpenRecordset("MyEmailAddresses")
(该位在我粘贴的代码块之前)确保该查询包含opt out字段。
Do Until MailList.EOF
If (MailList("email") <> "" AND MailList("opt_out")) Then
' This creates the e-mail
Set MyMail = MyOutlook.CreateItem(olMailItem)
' This addresses it
MyMail.To = MailList("email")
'This gives it a subject
MyMail.Subject = Subjectline$
'This gives it the body
MyMail.HTMLBody = MyBodyText
'If you want to send an attachment
'uncomment the following line
'MyMail.Attachments.Add "c:myfile.txt", olByValue, 1, "My Displayname"
' To briefly describe:
' "c:myfile.txt" = the file you want to attach
' olByVaue = how to pass the file. olByValue attaches it, olByReference creates a shortcut.
' the shortcut only works if the file is available locally (via mapped or local drive)
' 1 = the position in the outlook message where to attachment goes. This is ignored by most
' other mailers, so you might want to ignore it too. Using 1 puts the attachment
' first in line.
' "My Displayname" = If you don??t want the attachment??s icon string to be "c:myfile.txt" you
' can use this property to change it to something useful, i.e. "4th Qtr Report"
'This sends it!
MyMail.Send
'Some people have asked how to see the e-mail
'instead of automaticially sending it.
'Uncomment the next line
'And comment the "MyMail.Send" line above this.
'MyMail.Display
'And on to the next one...
End If
MailList.MoveNext
Loop
答案 2 :(得分:0)
我可以建议以下内容不涉及任何代码,并且可以使您使用最初使用的相同代码,除非更改该代码中表的名称。我们将原始表格称为“X”。基于表X创建选择查询,确保所有字段都显示在查询中。同时确保电子邮件地址字段显示在OptOut字段之前(左侧)。
在“电子邮件”列类型的“条件”行中:不是空 在“标准”字段中,OptOut列类型:&lt;&gt; 1
将查询视图更改为“数据表”。所有没有电子邮件地址的记录(1)以及(2)具有电子邮件地址但在OptOut字段中具有1的记录将不会出现在数据表中。如果您对结果满意,请执行以下操作: 更改回设计视图,单击“设计”选项卡,然后单击“MakeTable”输入与原始表相同的名称,但为其添加“X”。请注意不要输入与原始表格相同的名称。如果出现此错误,则在运行make table查询时将替换原始表。在输入表名称的小窗口中单击“确定”。我们称这个表名为“Y”。要运行此查询,请单击“设计”,然后单击“运行”。您可能会收到警告消息。通过允许创建新表来响应。保存您的查询
最小化查询并打开它创建的表(表Y)此表将不包含您想要排除的那些记录。在最初为您工作的代码中,在最初使用的代码中将表名从X(原始表的名称)更改为Y(已创建的新表的名称)。
替代选择 如果您需要删除不需要的记录,则无需创建新表也不需要更改代码。要创建将删除不需要的记录的查询,请执行与Make Table查询相同的步骤,但不要单击“Make Table”图标,而是单击“Delete”图标。保存删除查询,关闭它并在此阶段不运行它。而是首先制作原始表的副本,为副本提供另一个名称,如果删除查询可能导致任何问题,则为您提供副本。完成复制后,双击删除查询并通过允许删除来响应警告。然后检查你的原始表。不需要的记录应该不再存在。同样被删除了。然后运行原始代码而不以任何方式改变它。
如果这对你有用,请告诉我。有一种方法可以通过命令按钮自动执行制作表或删除不需要的记录。