2015-05-28 126 views
0

美好的一天 下面的代碼基於其他列生成唯一的電子郵件+附件到列A中的每一行。使用excel發送電子郵件從OFT模板vba

這意味着如果同一封電子郵件存在多次,他們將收到多個電子郵件。我想要做的是檢查電子郵件是否存在於多行(已排序)中,並僅向其發送一封電子郵件(包含所有附件)。這可能嗎?

這是我的代碼:

Dim objOutlook As Outlook.Application 
Dim objMail As Outlook.MailItem 

Dim rngTo As Range 
Dim rngSubject As Range 
Dim rngBody As Range 
Dim rngAttach As Range 

ActiveSheet.Range("A2").Select 

Do Until IsEmpty(ActiveCell) 

    Set objOutlook = CreateObject("Outlook.Application") 
    Set objMail = objOutlook.CreateItemFromTemplate("C:\Desktop\EBILL\template.oft") 
    With objMail 
    .To = ActiveCell.Offset(0, 4).Value 
    .Subject = "Invoice For: " & " " & Month & " - " & Year 
    .Attachments.Add ActiveCell.Offset(0, 5).Value 
    ActiveCell.Offset(1, 0).Select 
    .Display 'Instead of .Display, you can use .Send to send the email or .Save to save a copy in the drafts folder 
    End With 

Loop 


    Set objOutlook = Nothing 
    Set objMail = Nothing 
    Set rngTo = Nothing 
    Set rngSubject = Nothing 
    Set rngBody = Nothing 
    Set rngAttach = Nothing 
+0

是A2單元下的所有電子郵件地址? – 0m3r

+0

所有電子郵件都是vlookup的結果,在列(E)中列出。所以基本上我會有例如5個用戶,其中3個是獨特的,1個有2個條目。這主要用於向客戶發送賬單。 – Nadz

回答

0

找到一個工作的答案。希望這可以幫助有類似情況的人

With objMail 
.To = ActiveCell.Offset(0, 4).Value 
.Subject = "Invoice For: " & " " & Month & " - " & Year 
.Attachments.Add ActiveCell.Offset(0, 5).Value 
ActiveCell.Offset(1, 0).Select 

Do Until ActiveCell.Offset(0, 4).Value <> .To 
    .Attachments.Add ActiveCell.Offset(0, 5).Value 
    ActiveCell.Offset(1, 0).Select 
Loop 
.Display 'Instead of .Display, you can use .Send to send the email or .Save to save a copy in the drafts folder 
End With 
相關問題