私はRon de BruinのOutlook電子メールジェネレータを.To、.CC、.Subject、.Bodyフィールドに対していくつかの変更を加えて使用しました。私は、必要に応じて電子メールに請求書ファイルを添付するためのコードもいくつか含んでいました。各ループについて、配列の最初の項目をスキップします
添付ファイルリストの最初のグループはスキップされます。
Sub Create_Email()
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
'Don't forget to copy the function RangetoHTML in the module.
'Working in Excel 2000-2016
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
Dim StrBody As String
Dim Bundle As Variant, Group As Variant
Bundle = Split(Worksheets("Extra").Range("G2").Value, ",")
StrBody = Range("D5").Value & "<br>" & _
Range("D6").Value & "<br>" & _
Range("D7").Value & "<br>" & _
Range("D8").Value & "<br>" & _
Range("D9").Value & "<br><br><br><br>"
mola = Range("B2").Value
maybe = Format(mola, "mm")
real = Format(mola, "mmmm yyyy")
nope = Format(mola, "yyyy")
InvPath = ("U:\BILLREC\M & R EG Billing\Invoices\" & nope & "\" & maybe & " " & real & "\" & "Electronic Invoices" & "\")
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = Range("C2").Value
.CC = Range("D2").Value
.Subject = Range("C5").Value
.HTMLBody = StrBody
For Each Group In Bundle
pdfFile = "Group" & Group & ".pdf"
.Attachments.Add InvPath & pdfFile
Next Group
For Each Group In Bundle
xlsFile = "Group" & Group & ".xlsx"
.Attachments.Add InvPath & xlsFile
Next Group
.Display
End With
On Error GoTo 0
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
「バンドル」アレイ
は####、####、##、####形式で、グループ番号のオフに基づいて、ブック自体にVLOOKUPから供給されます。最終的には、標準命名規則でファイル名が付けられていないか、リスト内の最初のグループ番号です。
単一の「グループ」番号がある場合、ファイルはまったく添付されません。
「On Error Resume Next」をコメントアウトして、エラーが発生していないかどうか確認しましたか?例えば。あなたのセルの値に先行するスペースがある場合(おそらく '' 1234,3423,12,3131 "'の代わりに '' 1234,3423,12,3131 "'を持つ)、あなたの '.Add'は' "Group 1234.pdf"は存在しない可能性があります。 – YowE3K
私はその可能性を考えていました。配列を含むデータセットは、Vlookupから取得されます。私は手動でvlookupが返す可能性のある複数の結果を変更し、マクロによって引き込まれます。どちらの場合でも、順序やその他の値にかかわらず、配列にリストされている最初のグループのファイルは添付されません。 – JSmalls
InvPathが作成された直後に[GetAttr](https://msdn.microsoft.com/VBA/Language-Reference-VBA/articles/getattr-function)を使用してクイックサニティチェックを追加できます。GetAttrはいくつかのファイル属性を返しますファイルが存在しない場合はエラー53をスローします。 "MsgBox InvPath&" Group "&Bundle(0)&" .pdf "&": "&GetAttr(InvPath&" Group "&Bundle(0)&" .pdf ")'エラーが出たら、少なくともファイルが存在すると言うことができます。 – barrowc