1

Access にインポートした多数の Excel シートを持っており、Salesforce.com へのインポートの準備 (さまざまなフィールド名を統一し、SFDC が認識できるものにマッピングする) を行っているため、任意の 1 つのテーブル (Excel シート) から電子メールをバッチ処理できます。 VBA と Outlook 経由で。

ここで完璧に機能するコードを見つけました: http://www.jephens.com/2007/05/13/how-to-send-e-mail-from-ms-access-using-outlook/魅力。全員に BCC を送信して 1 つの大量のメッセージを送信するのではなく、メールを 1 つずつ送信する点が特に気に入っています。

私がやりたいことは、この機能をさらに活用するためにコードを変更することですが、VBA に関する私の知識は非常に限られています。

私の問題は2つあります。1 つ目は、リストのすべてのエントリに電子メール アドレスを持たないテーブルがある場合、クエリでエラーが発生します。私は、不足している電子メール アドレスをスキップして次のアドレスに移動するようにクエリに指示するコードを少し追加するだけの単純な問題であると想定しています。ただし、VBA についてはよくわからないので、これを実現するために何をどこにコードを配置すればよいか、誰かが助けてくれることを期待していました。

2 つ目は、メールを受信した受信者が返信メールで登録を解除することでオプトアウトできるように、メールを設定していることです。次に、メール送信元のテーブル (リスト) に戻り、作成した [メール オプトアウト] フィールドの下のエントリの横に「1」をドロップします (SFDC もこれを認識します)。また、「Email Opt Out」フィールドを照会し、VBA が 1 で見つかったエントリをスキップして、将来その人が電子メールを受け取らないようにしたいと考えています。

うまくいけば、私は明確です...私はAccess 2013とOutlook 2010を使用しています。

これは、「SendEMail」マクロを開始したときにすべてを実現する「SendMail」モジュールの背後にある VBA コードです。

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

コードを含まず、そのコード内のテーブルの名前を変更することを除いて、最初に使用したのとまったく同じコードを使用できるようにする次のことをお勧めします。元のテーブルを「X」と呼びましょう。テーブル X に基づいて選択クエリを作成し、すべてのフィールドがクエリに表示されるようにします。また、電子メール アドレス フィールドが OptOut フィールドの前 (左側) に表示されていることを確認します。

電子メール列タイプの「基準」行: Is Not Null 「基準」フィールドで、OptOut 列タイプ: <>1

クエリ ビューを「データシート」に変更します。(1) 電子メール アドレスを持たないレコード、および (2) 電子メール アドレスはあるが OptOut フィールドに 1 が含まれるレコードはすべて、データシートに表示されません。結果に満足したら、次の操作を行います: デザイン ビューに戻り、[デザイン] タブをクリックして、次に [MakeTable] をクリックします。元のテーブルと同じ名前を入力しますが、それに「X」を追加します。元のテーブルと同じ名前を入力しないように注意してください。この間違いを犯すと、テーブル作成クエリの実行時に元のテーブルが置き換えられます。テーブル名を入力した小さなウィンドウで [OK] をクリックします。このテーブル名を「Y」としましょう。このクエリを実行するには、[デザイン] をクリックしてから [実行] をクリックします。警告メッセージが表示される場合があります。新しいテーブルの作成を許可して応答します。クエリを保存します

クエリを最小化し、作成したテーブル (テーブル Y) を開きます。このテーブルには、除外したいレコードが含まれていません。最初に使用したコードで、テーブル名を X (元のテーブルの名前) から Y (作成された新しいテーブルの名前) に変更します。

代替オプション不要なレコードを削除する必要がある場合、新しいテーブルを作成したり、コードを変更したりする必要はありません。不要なレコードを削除するクエリを作成するには、テーブルの作成クエリと同じ手順に従いますが、[テーブルの作成] アイコンをクリックする代わりに、[削除] アイコンをクリックします。削除クエリを保存して閉じ、この段階では実行しないでください。代わりに、最初に元のテーブルのコピーを作成し、コピーに別の名前を付けます。このコピーは、削除クエリによって問題が発生した場合に役立ちます。コピーを作成したら、削除クエリをダブルクリックし、削除を許可して警告に応答します。次に、元のテーブルを調べます。不要なレコードはもう存在しないはずです。同じものは削除されました。次に、元のコードをまったく変更せずに実行します。

これがうまくいったかどうか教えてください。コマンド ボタンを使用して、テーブルの作成または不要なレコードの削除のプロセスを自動化する方法があります。

于 2014-07-23T01:07:43.027 に答える
0

私は VBA に慣れていないので、これが正しいと 100% 確信しているわけではありません。
私が推測したので、フィールドの名前が正しいことを確認する必要があります。

にジャンプ

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 に答える