将文件附加到与正确客户对应的Outlook电子邮件中

时间:2019-02-21 23:58:29

标签: excel vba outlook outlook-vba

您好,我正在为我的会计团队创建一个电子邮件生成器,它将向客户发送一封电子邮件,其中包含与该客户相对应的附件。当前,文件位于文件夹中,文件名每个月都不会更改。基本上,我只需要将正确的附件发送给正确的客户即可。

我尝试过的操作:如果附件名称是静态的,则能够使生成器正常工作。我能够在全部3个邮件中发送3个具有相同附件的独特电子邮件。

当我将附件名称更新为包括“&row_number”以遍历客户时,我开始收到错误消息:“ 438-对象不支持此属性或方法”

我将与下面的附件有关的所有代码行加粗了。我真的快完成了,因此我们将不胜感激。

这是我的代码:

 'CREATE EmailGenerator2 FUNCTION that COMBINES custom email, custom subjectline, standard message, rate attachment and hidden logo

  **Sub EmailGenerator2(what_address As String, subject_line As String, attachment As String, mail_body As String)**

'ENABLE OUTLOOK APP
Dim olApp As Outlook.Application
Set olApp = CreateObject("Outlook.Application")

Dim olMail As Outlook.MailItem
Set olMail = olApp.CreateItem(olMailItem)

'SET MAIL PARAMETERS
olMail.SentOnBehalfOfName = "pricing@companyname.com"
olMail.To = what_address
olMail.CC = ""
olMail.BCC = ""
olMail.Subject = subject_line
**olMail.attachment = attachment**
olMail.Attachments.Add "R:\Revenue Assurance\10. Databases\Email Generators\International_AZ\logo.png", 1, 0
olMail.BodyFormat = olFormatHTML
olMail.HTMLBody = mail_body
olMail.Send

End Sub

Sub SendMassEmail()

row_number = 1

Do
DoEvents
'CREATE LOOP EVENT
row_number = row_number + 1


**Dim custom_attachment As String**
Dim mail_body_message As String
Dim custom_subject_line As String


**'ADD CUSTOM ATTACHMENT
custom_attachment = Sheets("Sheet2").Range("A8").Value & "\" & Sheets("Sheet1").Range("D" & row_number).Value**

'INSERT BODY FROM SECOND TAB
mail_body_message = Sheet2.Range("A1")
partner_name2 = Sheet1.Range("B" & row_number)
notice_date2 = Sheet1.Range("F2")
payment_date = Sheet1.Range("F5")

mail_body_message = Replace(mail_body_message, "replace_partner_name2", partner_name2)
mail_body_message = Replace(mail_body_message, "replace_notice_date2", notice_date2)
mail_body_message = Replace(mail_body_message, "replace_payment_date", payment_date)

'CREATE CUSTOM SUBJECTLINE
custom_subject_line = Sheet2.Range("A5")
notice_date = Sheet1.Range("F5")
partner_name = Sheet1.Range("B" & row_number)
custom_subject_line = Replace(custom_subject_line, "replace_notice_date", notice_date)
custom_subject_line = Replace(custom_subject_line, "replace_partner_name", partner_name)
    'MsgBox custom_subject_line

'GENERATE and SEND EMAIL
**Call EmailGenerator2(Sheet1.Range("C" & row_number), custom_subject_line, custom_attachment, mail_body_message)**

'FINISH DO LOOP event on LAST ROW of COLUMN A
Loop Until row_number = Sheet1.Range("A99999").End(xlUp).Row


End Sub

0 个答案:

没有答案
相关问题