1

我有许多 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/魅力。我特别喜欢它一一发送电子邮件,而不是作为一封大量信息发送给每个人都密件抄送。

我想做的是更改代码以充分利用此功能,但我对 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
4

3 回答 3

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 ...
于 2013-03-12T02:09:26.633 回答
0

我不是 100% 确定这是正确的,因为我对 VBA 很生疏。
您需要确保字段名称正确,因为我猜了一下。

跳到

If (MailList("email") <> "" AND MailList("opt_out") <> 1) Then

并确保它具有正确的字段。

第二,当你做

db.OpenRecordset("MyEmailAddresses") 

(该位在我粘贴的代码块之前)确保查询包含选择退出字段。

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
于 2013-03-12T01:50:20.840 回答
0

我可以建议以下不涉及任何代码并且可以使您使用与最初使用的代码完全相同的代码,除了更改该代码中的表名称。让我们将您的原始表称为“X”。创建基于表 X 的选择查询,确保所有字段都出现在查询中。还要确保电子邮件地址字段出现在选择退出字段之前(左侧)。

在电子邮件列类型的“条件”行中:不为空 在“条件”字段中,选择退出列类型:<>1

将查询视图更改为“数据表”。所有没有电子邮件地址的记录 (1) 以及 (2) 有电子邮件地址但在 OptOut 字段中有 1 的记录都不会出现在数据表中。如果您对结果感到满意,请执行以下操作: 更改回设计视图,单击“设计”选项卡,然后单击“生成表” 输入与原始表相同的名称,但添加一个“X”。请注意不要输入与原始表格相同的名称。如果您犯了这个错误,那么在运行 make table 查询时,您的原始表将被替换。在您输入表名称的小窗口中单击确定。让我们将此表名称称为“Y”。要运行此查询,请单击“设计”,然后单击“运行”。您可能会收到一条警告消息。通过允许创建新表来响应。保存您的查询

最小化查询并打开它创建的表(表 Y) 该表将没有您想要排除的记录。在最初为您工作的代码中,将您最初使用的代码中的表名从 X(原始表的名称)更改为 Y(创建的新表的名称)。

替代选项如果您需要删除不需要的记录,则无需创建新表或更改您的代码。要创建将删除不需要的记录的查询,请按照与制作表查询相同的步骤操作,但不要单击“制作表”图标,而是单击“删除”图标。保存您的删除查询,关闭它并且在此阶段不要运行它。而是首先复制您的原始表,给该副本另一个名称,如果删除查询可能导致任何问题,该副本将为您服务。复制后,双击删除查询并通过允许删除来响应警告。然后检查您的原始表格。不需要的记录应该不再存在。同样已被删除。然后运行您的原始代码而不以任何方式更改它。

请让我知道这是否对您有用。有一种方法可以通过命令按钮自动执行制作表格或删除不需要的记录的过程。

于 2014-07-23T01:07:43.027 回答