基于判断发送邮件VBA代码
时间: 2023-12-14 22:03:32 浏览: 148
Excel-VBA宏编程实例源代码-文本与图形的操作-员工签到表2.zip
以下是一个基于判断的发送邮件VBA代码示例:
```VBA
Sub SendEmail()
Dim OutlookApp As Object
Dim OutlookMail As Object
Dim MyEmail As String
Dim RecipientEmail As String
Dim SubjectLine As String
Dim EmailBody As String
' Set up Outlook application
Set OutlookApp = CreateObject("Outlook.Application")
Set OutlookMail = OutlookApp.CreateItem(0)
' Set email properties
MyEmail = "your@email.com"
RecipientEmail = Range("A1").Value 'recipient email address in cell A1
SubjectLine = "Subject Line Text"
' Check if recipient email is valid
If InStr(RecipientEmail, "@") > 0 And InStr(RecipientEmail, ".") > 0 Then
' Email address is valid
EmailBody = "Email Body Text"
With OutlookMail
.To = RecipientEmail
.CC = ""
.BCC = ""
.Subject = SubjectLine
.Body = EmailBody
.Send
End With
MsgBox "Email sent successfully!", vbInformation
Else
' Email address is not valid
MsgBox "Invalid email address!", vbCritical
End If
Set OutlookMail = Nothing
Set OutlookApp = Nothing
End Sub
```
该代码从单元格 A1 中读取收件人电子邮件地址,并检查该地址是否有效。如果有效,则发送邮件,如果无效,则弹出错误消息框。您可以根据需要修改电子邮件正文和其他属性。
阅读全文