5

私たちはこの大きなイベントを開催しており、全員の名前、電子メール アドレス、旅程ファイル (2 つあります) を含む Excel シートがありCells(x, 3)ますCells(x, 4)。私がやろうとしているのは、列を下って、すべての情報を含む「パーソナライズされた」電子メールを全員に送信することです。

コードでは、forループは 3 にしか進みません。これは、自分自身にメールを送信してテストしているだけで、最終的に 1000 通のメールを受け取りたくないからです :P

添付ファイルを追加しようとした行で実行時エラー 440 (自動化エラー)が発生し続けます...何が起こっているのか、またはそれを修正する方法がわからないので、助けていただければ幸いです

コード

Sub CreateHTMLMail()
'Creates a new e-mail item and modifies its properties.

    Dim olApp As Object
    Dim objMail As Object
    Dim body, head, filePath, subject As String
    Dim x As Long
    Set olApp = CreateObject("Outlook.Application")
    'Create e-mail item
    Set objMail = olApp.CreateItem(0)

    filePath = "\\fileserver\homeshares\Tsee\My Documents\Metropolitan Sales\MNF"
    subject = "Important Travel Information for MNF Event this weekend"

    x = 1

    For x = 1 To 3
        head = "<HTML><BODY><P>Hi " & Cells(x, 1).Value & ",</P>"
        body = body & "<BR /><P>We are looking forward to having you at our <STRONG>Metropolitan Night Football Event</STRONG> this upcoming Sunday, <STRONG>11/17</STRONG>!  Note, that the Giants game time has changed from 8:30 PM to 4:25 PM.</P>"
        body = body & "<BR /><P>Please find attached your travel information packet that contains important addresses and confirmation numbers.  Please read through it and let me know if you have any questions.</P>"
        body = body & "<BR /><P>If you need to reach me this weekend, please call my cell phone <STRONG>(631) 793-9047</STRONG> or email me.</P>"
        body = body & "<BR /><P>Thanks,<BR />Liz</P></BODY></HTML>"

        With objMail
            .subject = subject
            .To = Cells(x, 2).Value
            .Attachments.Add = filePath & "/" & Cells(x, 3).Value
            .Attachments.Add = filePath & "/" & Cells(x, 4).Value
            .BodyFormat = olFormatHTML
            .HTMLBody = head & body
            .Send
        End With
    Next x

End Sub
4

1 に答える 1

6

上記のコメントに加えて、@bamie9l はすでにあなたの問題を 1 つ解決しています。

問題 2

@bamie9l すごい!それは機能しましたが、.BodyFormat = olFormatHTML 行で実行時エラー '5': 無効なプロシージャ コールまたは引数が発生します – metsales 13 分前

Excel から Outlook に遅延バインドしておりolFormatHTML、これは Outlook の定数であるため、Excel はそれを認識できません。Immediate WindowMS-Outlook で入力する?olFormatHTMLと、その定数の値が2

ここに画像の説明を入力

したがって、Excel でその定数を宣言する必要があります。前述したようConst olFormatHTML = 2に、コードの先頭に配置するか、次のように置き換えること.BodyFormat = olFormatHTMLができます.BodyFormat = 2

問題 3

@SiddharthRoutこれで機能しますが、今ではクレイジーな自動化エラーが発生します...ループを1回通過し、1通の電子メールを送信し、.subject = subjectに達すると実行時エラー '-2147221238 (8004010a )': 私が知る限り実行時エラー 440 と同じ自動化エラー – メタセールス

問題は、ループの外で Outlook アイテムを作成していることです。

Set objMail = olApp.CreateItem(0)

Outlook は既にそのメールを送信しており、次のメールのために再作成する必要があります。その行をループ内に移動します。

For x = 1 To 3
    Set objMail = olApp.CreateItem(0)

    head = "<HTML><BODY><P>Hi " & Cells(x, 1).Value & ",</P>"
    Body = "Blah Blah"

    With objMail

        .subject = subject
        .To = Cells(x, 2).Value
        .Attachments.Add = FilePath & "/" & Cells(x, 3).Value
        .Attachments.Add = FilePath & "/" & Cells(x, 4).Value
        .BodyFormat = olFormatHTML
        .HTMLBody = head & Body
        .Send
    End With
Next x
于 2013-11-11T17:15:14.187 に答える