我有以下循环,它从第二行开始为工作表中的每个用过的行调度电子邮件。
循环本身可以工作,并从第二行开始为每个使用过的行调度电子邮件。
但是,我的循环无法排除前一行内容并将它们累积到下一行的每一行中。电子邮件。我怀疑它与我的细胞参考有关。如果你可以帮助这个将是伟大的:)。
Sub TestEmail1()
Application.ScreenUpdating = False
Dim aOutlook As Object
Dim aEmail As Object
Dim rngeAddresses As Range, rngeCell As Range, strRecipients As String
Dim ccAddresses As Range, ccCell As Range, ccRecipients As String
Dim rngeSubject As Range, SubjectCell As Range, SubjectContent As Variant
Dim rngeBody As Range, bodyCell As Range, bodyContent As Variant
Dim Table1 As Range
Dim i As Integer
Set rng = ActiveSheet.UsedRange
LRow = rng.Rows.Count
For i = 2 To LRow
Set Table1 = Worksheets(1).Range("K1:R1")
Set Table2 = Worksheets(2).Range("K" & i & ":" & "R" & i)
Set aOutlook = CreateObject("Outlook.Application")
Set aEmail = aOutlook.CreateItem(0)
'set sheet to find address for e-mails as I have several people to
'mail to
Set rngeAddresses = ActiveSheet.Range("B" & i)
For Each rngeCell In rngeAddresses.Cells
strRecipients = strRecipients & ";" & rngeCell.Value
Next
Set ccAddresses = ActiveSheet.Range("C" & i)
For Each ccCell In ccAddresses.Cells
ccRecipients = ccRecipients & ";" & ccCell.Value
Next
Set rngeSubject = ActiveSheet.Range("D" & i)
For Each SubjectCell In rngeSubject.Cells
SubjectContent = SubjectContent & SubjectCell.Value
Next
Set rngeBody = ActiveSheet.Range("E" & i)
For Each bodyCell In rngeBody.Cells
bodyContent = bodyContent & bodyCell.Value
Next
'set Importance
'aEmail.Importance = 2
'Set Subject
aEmail.Subject = rngeSubject
'Set Body for mail
'aEmail.Body = bodyContent
aEmail.HTMLBody = bodyContent & "<br><br><br>" & RangetoHTML_ (Table1)
aEmail.To = strRecipients
aEmail.CC = ccRecipients
aEmail.Send
Next i
Exit Sub
End Sub
答案 0 :(得分:0)
是的,您正在SubjectContent,bodyContent和其他类似变量中累积更长和更长的字符串。每次通过For i = 2到lRow循环时,都会将当前行中指定单元格的值添加到关联的Content
变量中。
出于某种原因,您还为每个电子邮件区域设置了内循环,例如For Each bodyCell in rgneBody.Cells
。如果我正确地读取您的代码,则不需要这些循环。
因此,考虑到上述情况,我会改变:
Set rngeBody = ActiveSheet.Range("E" & i)
For Each bodyCell In rngeBody.Cells
bodyContent = bodyContent & bodyCell.Value
Next
为:
bodyContent = ActiveSheet.Range("E" & i)
如果您认为中间rngeBody
变量更具可读性,则可以使用它,但这并非必要。
对您的其他电子邮件区域变量重复上述步骤。