尝试使用包含嵌入图像的VBA代码从Excel创建批量电子邮件工作簿。我无法申请" For i
"对于代码,无法弄清楚如何使用ListObject table
从整个列表发送电子邮件。对于下面的脚本,引用的工作表是"消息生成器。"我试图向列表中的每个人发送单独的电子邮件,直到列B中的行中的值= 0.但是,我为循环设置的整数似乎返回值0,好像没有值在行和列中。
任何人都知道如何发送创建工作簿来发送批量电子邮件?请参阅下面的脚本。谢谢!
Dim MainWB As Workbook
Dim olApp As Outlook.Application
Dim olemail As Outlook.MailItem
Dim fso As Scripting.FileSystemObject
Dim ts As Scripting.TextStream
Dim SigPath As String, SigText As String
SigPath = Environ("AppData") & "\Microsoft\Signatures\New.htm"
Set fso = New Scripting.FileSystemObject
Set ts = fso.OpenTextFile(SigPath)
SigText = ts.ReadAll
ts.Close
Set fso = Nothing
Set MainWB = ActiveWorkbook
Dim Subject As String
Dim Body As String
Dim i As Integer
Dim l As Integer
l = NumberOfNonBlankRowsInColumn(2) - 2 'subtract 2 header rows
Set olApp = New Outlook.Application
For i = 0 To l
Set olemail = olApp.CreateItem(olMailItem)
Subject = MainWB.Sheets("Message Generator").Range("B3").Offset(i, 0).Value
Body = MainWB.Sheets("Message Generator").Range("AB3").Offset(i, 0).Value
With olemail
.BodyFormat = olFormatHTML
.To = "UTOAI@outlook.com"
.Subject = Subject
.Body = Body
.Attachments.Add "C:\Users\Jacka\Documents\Test\logo.jpg"
.HTMLBody = "<img src='cid:logo.jpg'" & "width='309.5' height='39.5'><br>" & _vbanewline & .HTMLBody & SigText
.Display
End With
Set olemail = Nothing
Next i
Set olApp = Nothing
End Sub
函数NumberOfNonBlankRowsInColumn(souceCol As Integer)As Integer
Dim NumberOfRowsInColumn As Integer, j As Integer
Dim CurrentRowValue As String
NumberOfRowsInColumn = Cells(Rows.Count, sourceCol).End(xlUp).row
For j = 1 To NumberOfRowsInColumn
CurrentRowValue = Cells(j, sourceCol).Value
If IsEmpty(CurrentRowValue) Or CurrentRowValue = "" Then
Exit For
End If
Next j
NumberOfNonBlankRowsInColumn = (j - 1)
End Function
答案 0 :(得分:0)
尽量不要犯这个错误。它有时工作而不是其他工作的原因是因为我在上面的行中有一个值,后来删除了。因此,我将NonBlankRowsInColumn设置为2(值开始的地方)并完成。请参阅下面的编辑功能。
函数NumberOfNonBlankRowsInColumn(souceCol As Integer)As Integer
Dim NumberOfRowsInColumn As Integer, j As Integer
Dim CurrentRowValue As String
NumberOfRowsInColumn = Cells(Rows.Count, sourceCol).End(xlUp).row
For j = 2 To NumberOfRowsInColumn
CurrentRowValue = Cells(j, sourceCol).Value
If IsEmpty(CurrentRowValue) Or CurrentRowValue = "" Then
Exit For
End If
Next j
NumberOfNonBlankRowsInColumn = (j - 1)
End Function