提问人:user19600963 提问时间:11/2/2023 最后编辑:user19600963 更新时间:11/2/2023 访问量:44
群集电子邮件跳过不包含电子邮件的行,而不是收到错误消息
Cluster Email Skip Rows with no Email included instead of getting an error message
问:
我想根据包含电子邮件地址的表格使用VBA发送自定义电子邮件。
当有一行没有电子邮件时,我收到一条错误消息,只会发送第一行。strEmail
如何跳过没有电子邮件的行,直到有一行是空的。strTour_Nr
有什么想法吗?
Sub sendCustEmails()
Dim objOutlook As Object
Set objOutlook = CreateObject("Outlook.Application")
Dim objEmail As Object
Set objEmail = objOutlook.CreateItem(olMailItem)
intRow = 2
strTour_Nr = ThisWorkbook.Sheets("2. Ansprechpartner für Touren").Range("F" & intRow).Text
While (strTour_Nr <> " ")
Set objEmail = objOutlook.CreateItem(olMailItem)
'Subject and Body templates are in cells A2 and B2
strMailSubject = ThisWorkbook.Sheets("Tabelle1").Range("A2").Text
strMailBody = ThisWorkbook.Sheets("Tabelle1").Range("B2").Text
strTour_Nr = ThisWorkbook.Sheets("2. Ansprechpartner für Touren").Range("F" & intRow).Text
strUntName = ThisWorkbook.Sheets("2. Ansprechpartner für Touren").Range("C" & intRow).Text
strEmail = ThisWorkbook.Sheets("2. Ansprechpartner für Touren").Range("P" & intRow).Text
strVon_Ladedatum = ThisWorkbook.Sheets("2. Ansprechpartner für Touren").Range("K" & intRow).Text
strVon_Ladezeit = ThisWorkbook.Sheets("2. Ansprechpartner für Touren").Range("M" & intRow).Text
strBis_Ladezeit = ThisWorkbook.Sheets("2. Ansprechpartner für Touren").Range("N" & intRow).Text
strPOD_fehlt = ThisWorkbook.Sheets("2. Ansprechpartner für Touren").Range("Q" & intRow).Text
strIOP_IOD_fehlt = ThisWorkbook.Sheets("2. Ansprechpartner für Touren").Range("R" & intRow).Text
strPOD_missing = ThisWorkbook.Sheets("2. Ansprechpartner für Touren").Range("S" & intRow).Text
strIOD_IOP_missing = ThisWorkbook.Sheets("2. Ansprechpartner für Touren").Range("T" & intRow).Text
strMailSubject = Replace(strMailSubject, "<TourNr>", strTour_Nr)
strMailBody = Replace(strMailBody, "<TourNr>", strTour_Nr)
strMailBody = Replace(strMailBody, "<Unt_Name>", strUntName)
strMailBody = Replace(strMailBody, "<Von_Ladedatum>", strVon_Ladedatum)
strMailBody = Replace(strMailBody, "<Von_Ladezeit>", strVon_Ladezeit)
strMailBody = Replace(strMailBody, "<Bis_Ladezeit>", strBis_Ladezeit)
strMailBody = Replace(strMailBody, "<POD_fehlt>", strPOD_fehlt)
strMailBody = Replace(strMailBody, "<IOP_IOD_fehlt>", strIOP_IOD_fehlt)
strMailBody = Replace(strMailBody, "<POD_missing>", strPOD_missing)
strMailBody = Replace(strMailBody, "<IOP_IOD_missing>", strIOD_IOP_missing)
With objEmail
.To = CStr(strEmail)
.Subject = strMailSubject
.Body = strMailBody
.Send
End With
intRow = intRow + 1
strTour_Nr = ThisWorkbook.Sheets("2. Ansprechpartner für Touren").Range("F" & intRow).Text
Wend
End Sub
On Error Resume Next
似乎有效,但我不知道如何/在哪里阻止它。
答:
0赞
Shai Rado
11/2/2023
#1
尝试设置对象,然后通过使用语句可以使代码更短且更易于阅读。Worksheet
With
我用常规的 .While
For
请参阅下面代码说明中的注释:
Sub sendCustEmails()
Dim objOutlook As Object
Dim objEmail As Object
Dim ws As Worksheet
Dim LastRow As Long, intRow As Long
Set objOutlook = CreateObject("Outlook.Application")
' set worksheet object
Set ws = ThisWorkbook.Sheets("2. Ansprechpartner für Touren")
LastRow = ws.Range("F2").End(xlDown).Row ' get last row with data in worksheet
With ws
' loop over rows with data in column F
For intRow = 2 To LastRow
If Trim(.Range("F" & intRow).Value) <> "" Then ' check that column F contains text in it
Set objEmail = objOutlook.CreateItem(olMailItem)
'Subject and Body templates are in cells A2 and B2
strMailSubject = ThisWorkbook.Sheets("Tabelle1").Range("A2").Value
strMailBody = ThisWorkbook.Sheets("Tabelle1").Range("B2").Value
strTour_Nr = .Range("F" & intRow).Value
strUntName = .Range("C" & intRow).Value
strEmail = .Range("P" & intRow).Value
strVon_Ladedatum = .Range("K" & intRow).Value
strVon_Ladezeit = .Range("M" & intRow).Value
strBis_Ladezeit = .Range("N" & intRow).Value
strPOD_fehlt = .Range("Q" & intRow).Value
strIOP_IOD_fehlt = .Range("R" & intRow).Value
strPOD_missing = .Range("S" & intRow).Value
strIOD_IOP_missing = .Range("T" & intRow).Value
strMailSubject = Replace(strMailSubject, "<TourNr>", strTour_Nr)
strMailBody = Replace(strMailBody, "<TourNr>", strTour_Nr)
strMailBody = Replace(strMailBody, "<Unt_Name>", strUntName)
strMailBody = Replace(strMailBody, "<Von_Ladedatum>", strVon_Ladedatum)
strMailBody = Replace(strMailBody, "<Von_Ladezeit>", strVon_Ladezeit)
strMailBody = Replace(strMailBody, "<Bis_Ladezeit>", strBis_Ladezeit)
strMailBody = Replace(strMailBody, "<POD_fehlt>", strPOD_fehlt)
strMailBody = Replace(strMailBody, "<IOP_IOD_fehlt>", strIOP_IOD_fehlt)
strMailBody = Replace(strMailBody, "<POD_missing>", strPOD_missing)
strMailBody = Replace(strMailBody, "<IOP_IOD_missing>", strIOD_IOP_missing)
With objEmail
.To = CStr(strEmail)
.Subject = strMailSubject
.Body = strMailBody
.Send
End With
Set objEmail = Nothing ' clear object
End If
Next intRow
End With
End Sub
评论
0赞
user19600963
11/2/2023
我收到以下错误消息:“运行时错误'-2147467259 (80004005)':必须指定收件人。确保至少输入一个名称。
0赞
Shai Rado
11/2/2023
您@user19600963有效的电子邮件地址?您可以分享工作表和 F 列的屏幕截图吗?您可以使用虚假的电子邮件地址。
0赞
user19600963
11/2/2023
嗨,我在帖子中添加了屏幕截图。我在 P 列中有一个公式,用于检查电子邮件是否有效。如果有效,则电子邮件将插入到 P 列中。这是个问题吗?
0赞
Shai Rado
11/3/2023
@user19600963根据您的屏幕截图,F列中没有电子邮件地址,但是P列?您要使用的电子邮件地址保存在哪里?
0赞
user19600963
11/3/2023
这现在对我有用:imgur.com/a/VGijLan
评论