[英]VBA outlook send to email saved in cell
I have the below VBA code to automatically send outlook email.我有以下 VBA 代码可以自动发送 Outlook 电子邮件。 the email will be send based on the condition applied on column "D" , so if the condition is true on Column "D" , The email will be automatically created and send it To email in Column "C" where its cell within the same row of the condition on column "D" .
电子邮件将根据应用于列“D”的条件发送,因此如果“D”列的条件为真,则将自动创建电子邮件并将其发送到“C”列中的电子邮件,其单元格在同一列列 "D" 上的条件行。
I have written all the codes except the code related to the part " send it To email in Column "C" where its cell within the same row of the condition on column "D" " , so please revise my code and help me除了与“将其发送到“C”列中的电子邮件,其单元格位于“D”列条件的同一行中的代码外,我已经编写了所有代码,因此请修改我的代码并帮助我
Dim xRg As Range
'Update by Extendoffice 2018/3/7
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Target.Cells.Count > 1 Then Exit Sub
Set xRg = Intersect(Range("D2:D1000"), Target)
If xRg Is Nothing Then Exit Sub
If IsNumeric(Target.Value) And Target.Value > 2 Then
Call Mail_small_Text_Outlook
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 = "Hi there" & vbNewLine & vbNewLine & _
"You have pending quotation which its number"
On Error Resume Next
With xOutMail
.To = "Email Address"
.CC = ""
.BCC = ""
.Subject = "send by cell value test"
.Body = xMailBody
.Display 'or use .Send
End With
On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub
It seems you need to send the email to a recipient specified in the Excel sheet.您似乎需要将电子邮件发送给 Excel 工作表中指定的收件人。 To get this working you need to extract a value from the cell and pass it to the
Mail_small_Text_Outlook
method:要使其正常工作,您需要从单元格中提取一个值并将其传递给
Mail_small_Text_Outlook
方法:
Dim xRg As Range
'Update by Extendoffice 2018/3/7
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Target.Cells.Count > 1 Then Exit Sub
Set xRg = Intersect(Range("D2:D1000"), Target)
If xRg Is Nothing Then Exit Sub
If IsNumeric(Target.Value) And Target.Value > 2 Then
Mail_small_Text_Outlook(Target.Cells("C2"))
End If
End Sub
Sub Mail_small_Text_Outlook(Dim value as string)
Dim xOutApp As Object
Dim xOutMail As Object
Dim xMailBody As String
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)
xMailBody = "Hi there" & vbNewLine & vbNewLine & _
"You have pending quotation which its number"
On Error Resume Next
With xOutMail
.To = value
.CC = ""
.BCC = ""
.Subject = "send by cell value test"
.Body = xMailBody
.Display 'or use .Send
End With
On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub
You may find a lot of samples on the Mail from Excel with Outlook (Windows) page.您可能会在带有 Outlook 的 Excel 邮件 (Windows)页面上找到大量示例。 Pay special attention to the Mail a different file(s) to each person in a range article.
请特别注意将不同的文件邮寄给范围文章中的每个人。
To reference another cell based on a target cell.根据目标单元格引用另一个单元格。
Target.Offset(0, -1)
Option Explicit ' Consider this mandatory
' Tools | Options | Editor tab
' Require Variable Declaration
' If desperate declare as Variant
Dim xRg As Range
Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
Set xRg = Intersect(Range("D2:D1000"), Target)
If xRg Is Nothing Then Exit Sub
If IsNumeric(Target.Value) And Target.Value > 2 Then
Call Mail_small_Text_Outlook(Target.Offset(0, -1).Value)
End If
End Sub
Sub Mail_small_Text_Outlook(valueColC As String)
Dim xOutApp As Object
Dim xOutMail As Object
Dim xMailBody As String
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)
xMailBody = "Hi there" & vbNewLine & vbNewLine & _
"You have pending quotation which its number"
' feel free to remove when it immediately precedes With xOutMail
' https://excelmacromastery.com/vba-error-handling/#On_Error_Resume_Next
With xOutMail
.To = valueColC
.CC = ""
.BCC = ""
.Subject = "send by cell value test"
.Body = xMailBody
.Display 'or use .Send
End With
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.