excel 的 Outlook 邮件:正文和变量有问题
Outlook mail with excel: Issue with body text and variable
我在使用 .body 文本时遇到问题。
我可以得到它来在 outlook 中创建项目。 "To:" 和 "Subject" 正确,但正文不正确。
我可以让它包含从 "Hello" 到 "following service lines:" 或包含 "For y = 2 to LR1" 到 Next y。我希望它包括两者。我如何连接这两个部分? - 解决了!
If 匹配函数应匹配 sht1.range("B" & y) 与 sht3.range("C" & i)。在 "y" 循环中,y 从 2 变为 49,而变量 i 应该保持不变,直到循环完成。然后它应该移动到下一个 i 并重新做一遍。这是行不通的,因为正文包含所有行 i 变量 y。 - 已解决!
Sub Test()
Dim OutApp As Object
Dim OutMail As Object
Dim Sht1 As Worksheet
Dim Sht2 As Worksheet
Dim Sht3 As Worksheet
Dim i As Long
Dim y As Long
Dim x As Long
Dim LR1 As Long
Dim LR2 As Long
Set Sht1 = ThisWorkbook.Sheets("Sheet1")
Set Sht2 = ThisWorkbook.Sheets("Sheet2")
Set Sht3 = ThisWorkbook.Sheets("Sheet3")
LR1 = Sht1.Cells(Sht1.Rows.Count, "B").End(xlUp).Row
LR2 = Sht3.Cells(Sht3.Rows.Count, "C").End(xlUp).Row
Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")
On Error GoTo Cleanup
For i = 2 To LR2
If Not IsEmpty(Sht3.Range("C" & i)) Then
Sht3.Activate
Sht3.Range("C" & i).Select
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = Sht3.Range("C" & i).Offset(ColumnOffset:=1)
.Subject = "OE input sheet " & Sht3.Range("C" & i) & ": Service Delivered = NO"
.body = "Hello " & ActiveCell.Offset(ColumnOffset:=-1).Value & "." _
& vbNewLine & vbNewLine & _
"We have noticed, that you have indicated Service Deliverd = NO, while the service line still contains a value. " _
& vbNewLine & vbNewLine & _
"We are referring to the following service lines:" _
For y = 2 To LR1
Sht1.Activate
Sht1.Range("B" & y).Select
If Sht1.Range("B" & y) = Sht3.Range("C" & i) Then
.body = .body & vbNewLine & ActiveCell.Offset(ColumnOffset:=1).Value
End If
Next y
.Save
End With
On Error GoTo 0
Set OutMail = Nothing
End If
Next i
Cleanup:
Set OutApp = Nothing
Application.ScreenUpdating = True
End Sub
提前致谢
mailItem 的 .body 属性 没有将附加信息附加到正文中,它完全替换了正文中的信息。
因此,当您第一次使用 "Hello" 部分进行设置时,电子邮件正文包含此信息。当您在循环中再次设置它时,它现在只包含您在循环中设置的信息。
将循环中的行更新为:
.body = .body & vbNewLine & ActiveCell.Offset(ColumnOffset:=1).Value
它会将新信息附加到电子邮件正文中。
我在使用 .body 文本时遇到问题。
我可以得到它来在 outlook 中创建项目。 "To:" 和 "Subject" 正确,但正文不正确。
我可以让它包含从 "Hello" 到 "following service lines:" 或包含 "For y = 2 to LR1" 到 Next y。我希望它包括两者。我如何连接这两个部分? - 解决了!
If 匹配函数应匹配 sht1.range("B" & y) 与 sht3.range("C" & i)。在 "y" 循环中,y 从 2 变为 49,而变量 i 应该保持不变,直到循环完成。然后它应该移动到下一个 i 并重新做一遍。这是行不通的,因为正文包含所有行 i 变量 y。 - 已解决!
Sub Test()
Dim OutApp As Object
Dim OutMail As Object
Dim Sht1 As Worksheet
Dim Sht2 As Worksheet
Dim Sht3 As Worksheet
Dim i As Long
Dim y As Long
Dim x As Long
Dim LR1 As Long
Dim LR2 As Long
Set Sht1 = ThisWorkbook.Sheets("Sheet1")
Set Sht2 = ThisWorkbook.Sheets("Sheet2")
Set Sht3 = ThisWorkbook.Sheets("Sheet3")
LR1 = Sht1.Cells(Sht1.Rows.Count, "B").End(xlUp).Row
LR2 = Sht3.Cells(Sht3.Rows.Count, "C").End(xlUp).Row
Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")
On Error GoTo Cleanup
For i = 2 To LR2
If Not IsEmpty(Sht3.Range("C" & i)) Then
Sht3.Activate
Sht3.Range("C" & i).Select
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = Sht3.Range("C" & i).Offset(ColumnOffset:=1)
.Subject = "OE input sheet " & Sht3.Range("C" & i) & ": Service Delivered = NO"
.body = "Hello " & ActiveCell.Offset(ColumnOffset:=-1).Value & "." _
& vbNewLine & vbNewLine & _
"We have noticed, that you have indicated Service Deliverd = NO, while the service line still contains a value. " _
& vbNewLine & vbNewLine & _
"We are referring to the following service lines:" _
For y = 2 To LR1
Sht1.Activate
Sht1.Range("B" & y).Select
If Sht1.Range("B" & y) = Sht3.Range("C" & i) Then
.body = .body & vbNewLine & ActiveCell.Offset(ColumnOffset:=1).Value
End If
Next y
.Save
End With
On Error GoTo 0
Set OutMail = Nothing
End If
Next i
Cleanup:
Set OutApp = Nothing
Application.ScreenUpdating = True
End Sub
提前致谢
mailItem 的 .body 属性 没有将附加信息附加到正文中,它完全替换了正文中的信息。
因此,当您第一次使用 "Hello" 部分进行设置时,电子邮件正文包含此信息。当您在循环中再次设置它时,它现在只包含您在循环中设置的信息。
将循环中的行更新为:
.body = .body & vbNewLine & ActiveCell.Offset(ColumnOffset:=1).Value
它会将新信息附加到电子邮件正文中。