【发布时间】:2016-02-05 05:54:09
【问题描述】:
我无法解决通过 Excel 发送电子邮件时出现的错误。目前它被设置为当下拉列表在“N”列中标题为“打开”时,它会向“M”列中出现电子邮件地址的特定人员发送电子邮件(我在“J”中选择电子邮件的名称" 并在 "M" 中创建地址)。我认为问题可能在于它检查了我在“N”中“打开”的每一行,因此当没有电子邮件地址时,它会抛出“运行时错误'13':类型不匹配”。我目前有以下代码:
在表 1(问题表)中:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("N3")) Is Nothing Then
Select Case Range("N3")
Case "Open": Macro1
End Select
End If
End Sub
我的模块是:
Option Explicit
子宏1() 将 OutApp 调暗为对象 将 OutMail 作为对象变暗 将单元格调暗为范围
Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")
On Error GoTo cleanup
For Each cell In Columns("M").Cells
If cell.Value Like "?*@xyz.com" And _
LCase(Cells(cell.Row, 14).Value) = "open" Then
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = cell.Value
.Subject = "Open Issue"
.Body = "Dear " & Cells(cell.Row, "J").Value _
& vbNewLine & _
"Issue raised: " & Cells(cell.Row, "C").Value _
& vbNewLine & _
"Regards"
'You can add files also like this
'.Attachments.Add ("C:\test.txt")
.Display 'Or use Display
End With
On Error GoTo 0
Set OutMail = Nothing
End If
Next cell
cleanup:
Set OutApp = Nothing
Application.ScreenUpdating = True
End Sub
基本上我想要它做的是能够只向地址发送电子邮件,在“打开”下拉列表中选择的行中,即在“N”列中选择“打开”,然后发送电子邮件仅到该行的“M”中的地址。我也需要它是可扩展的,这样我就可以向下走几行,只从那一行发送电子邮件。
【问题讨论】:
-
哪一行报错了?
-
如果 cell.Value 像 "?*@xyz.com" 和 _ LCase(Cells(cell.Row, "N").Value) = "open" 那么