【发布时间】:2019-10-12 02:52:29
【问题描述】:
我是 VB 的新手,希望能提供任何帮助。提前谢谢你。
我正在尝试在特定单元格具有特定值时发送电子邮件。例如,当单元格 E3 的值为 1 时,我想发送电子邮件 1,当它的值为 2 时,我想发送电子邮件 2。这部分工作正常。我还希望主题行是左侧3个单元格的内容,即当单元格E3被触发发送电子邮件时,我希望主题行是B3的内容,当E4被触发时,我想要以B4的内容为准。这是我失误的地方,我尝试了各种偏移量和范围选项,但我只是在电子邮件中得到一个空白主题行。这就是我所拥有的:
Dim xRg As Range
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Target.Cells.Count > 1 Then Exit Sub
Set xRg = Intersect(Range("E3:E5"), Target)
If xRg Is Nothing Then Exit Sub
If IsNumeric(Target.Value) And Target.Value = 1 Then
Call Mail_small_Text_Outlook
ElseIf IsNumeric(Target.Value) And Target.Value = 2 Then
Call Mail_small_Text_Outlook2
End If
End Sub
Sub Mail_small_Text_Outlook()
Dim xOutApp As Object
Dim xOutMail As Object
Dim xMailBody As String
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)
xMailBody = "Good Day" & vbNewLine & _
"Content Mail 1"
On Error Resume Next
With xOutMail
.To = "xxx@mci.co.za"
.CC = ""
.BCC = ""
.Subject = ActiveCell.Offset(0, -3).Value
.Body = xMailBody
.Display 'or use .Send
End With
On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub
Sub Mail_small_Text_Outlook2()
Dim xOutApp As Object
Dim xOutMail As Object
Dim xMailBody As String
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)
xMailBody = "Good Day" & vbNewLine & _
"Content Mail 2"
On Error Resume Next
With xOutMail
.To = "xxx@mci.co.za"
.CC = ""
.BCC = ""
.Subject = ActiveCell.Offset(0, -3).Value
.Body = xMailBody
.Display 'or use .Send
End With
On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub
【问题讨论】:
-
查看是否可以在 With.. End With 构造之前捕获 ActiveCell.Offset(0, -3).Value 作为临时字符串。如果字符串为空,则可能是 ActiveCell 连接问题,或者数据类型/excelsheet/工作簿引用可能有误。