VBA发送带有特定列的电子邮件

时间:2018-05-05 06:38:48

标签: excel vba excel-vba

我有一个excel列表,我想创建一个按钮,使用模板在列表中发送电子邮件,目标电子邮件地址在列K中,但我只想在列A显示为YES时发送它。

我写了一个脚本来循环我想要的每一行,看看列A是否有"是"或不,如果是,则参考另一个宏。

但我陷入花药宏,我不能指定.to =列K

以下是查找A列是否为YES的循环脚本:

sub agreement2 ()
dim startrow as integer
startrow = 9
dim mylastrow3 as integer
lastrow3 = activesheet.cells(rows.count, 1).end(xlup).row
dim i as integer

for i = 9 to lastrow3
if (cells(i, 1).value = "YES") then
send_letter
end if
end sub 

这是我的send_letter脚本

Sub send_letter()

Dim Subject
Dim Body
Dim otlapp As Object
Dim olMail2 As Object
Dim ws As Object


Set otlapp = CreateObject("Outlook.Application")
Set olMail2 = otlapp.CreateItemFromTemplate("\\cpadm001.corp.ocalwa.com\clk\DEPT\CLKDEPT6\IMT\SAO\SSC\Team\Team1\New Joiner Script\agreement.oft")
Set doc2 = olMail2.GetInspector.WordEditor
Set ws = ThisWorkbook.Worksheets("Send Letters")

vTemplateBody2 = olMail2.HTMLBody
Subject2 = "Agreement Letter"
HTMLBody2 = vTemplateBody2


With olMail2
.To = ????????????????.Value
.Subject = Subject2
Set WrdRng = doc2.Range
WrdRng.Paste
.Send

End With

 End Sub

任何人都可以帮助K列。非常感谢你。

1 个答案:

答案 0 :(得分:0)

  1. send_letter更改为send_letter(r)
  2. 更改从send_lettersend_letter i
  3. 的来电
  4. 您的最终代码为:.To = Cells(r, "K").Value