已发送/未发送电子邮件的VBA IF语句

时间:2020-06-27 19:28:51

标签: excel vba if-statement

我正在使用以下代码告诉我何时发送电子邮件,并显示文本“已发送”,所以我知道没有错误。但是我正在测试代码,添加供应商名称后,我使用vlookup显示电子邮件。我的目标是不让宏调试并继续进行下一个操作,但同时让我知道一行中有错误,因为该供应商没有列出电子邮件,我需要填写用电子邮件发送。列出供应商时,我将单元格留空以测试代码。即使我有有效的电子邮件,发送给VBA的那些电子邮件也会对已发送的电子邮件显示“未发送”。由于该宏由于一个单元格为空而找不到电子邮件,因此它已调试,并且在所有有效电子邮件旁边填充了“未发送”文本。我想念什么或做错什么?我只想避免调试来告诉我有错误,只告诉我“未发送”一行,而是继续发送其余的行,并用“已发送”文本填充那些发送的行。

Sub Send_Multiple_Emails()
dim sh as worksheet
set sh = thisworkbook.sheets("sheet1") <-- rename to what the tabs name is

dim OA as Object
Dim msg As object

set OA = createobject("Outlook.Application")

Dim i as integer
dim last_row As Integer

last_row = application.worksheetfunction.counta(sh.range("B:B"))

for i = 2 To last_row 
Set msg = OA.createitem(0)

msg.to = sh.Range("B" & i).Value
msg.cc = sh.Range("C" & i).Value
msg.subject = sh.range("D" & i ).Value
msg.body = sh.Range("E" & i).Value

if sh.Range("F" & i).Value <> "" Then
msg.attachments.add sh.range("F" & i).Value
End If

msg.send

**If Issent = True Then
Range("G" & i).Value = "Sent"
Else
Range("G" & i).Value = "Not Sent"
End If**

next i

msgbox "Mails Sent"

End Sub

1 个答案:

答案 0 :(得分:0)

请尝试替换此代码部分,

msg.send

If Issent = True Then
   Range("G" & i).Value = "Sent"
Else
   Range("G" & i).Value = "Not Sent"
End If

与此:

Dim Issent As Boolean
   
   On Error Resume Next
    msg.send
   If Err.Number <> 0 Then
      Err.Clear: On Error GoTo 0
      Issent = False
   Else
      On Error GoTo 0
      Issent = True
   End If
   
    If Issent = True Then
       Range("G" & i).Value = "Sent"
    Else
       Range("G" & i).Value = "Not Sent"
    End If

编辑: 我认为以上插入的行不会干扰您的代码平滑操作。即使不是这个问题,也请尝试下一个改编的代码。它还处理最终错误的要附加文档的路径:

  1. 添加对Microsoft Outlook ... Object Library的引用。在VBE(Visual Basic编辑器)中,转到:工具(菜单)->参考...向下滚动,直到找到上述参考。选中它,然后按OK

  2. 复制下一个代码而不是您的代码,或者复制下一个代码(我将更改Sub名称)并运行它:

    Sub Send_Multiple_Emails_bis()
     Dim sh As Worksheet, Issent As Boolean, i As Long, last_row As Long
     Dim OA As New Outlook.Application, msg As Outlook.MailItem
    
     Set sh = ActiveSheet ' ThisWorkbook.Sheets("sheet1")
    
     last_row = sh.Range("B" & Rows.count).End(xlUp).row
    
     For i = 2 To last_row
        Set msg = OA.CreateItem(0)
        With msg
            .To = sh.Range("B" & i).Value
            .cc = sh.Range("C" & i).Value
            .Subject = sh.Range("D" & i).Value
            .body = sh.Range("E" & i).Value
            '.display 'un-comment if you want to see each mail sending window
        End With
        
        If sh.Range("F" & i).Value <> "" Then
            If Dir(sh.Range("F" & i).Value) <> "" Then
                msg.Attachments.aDD sh.Range("F" & i).Value
            Else
                Range("G" & i).Value = "Wrong attachment path"
                GoTo NextMail
            End If
        End If
        
        On Error Resume Next
        msg.send
         If Err.Number <> 0 Then
            Err.Clear: On Error GoTo 0
            Issent = False
         Else
            On Error GoTo 0
            Issent = True
         End If
        
        If Issent = True Then
            Range("G" & i).Value = "Sent"
        Else
            Range("G" & i).Value = "Not Sent"
        End If
    NextMail:
        Set msg = Nothing
     Next i
    
     MsgBox "Mails Sent"
    End Sub

我想收到一些有关其行为的反馈...