作者:丢失的面包树 | 来源:互联网 | 2023-07-23 07:25
我想使用vba发送前景电子邮件,允许用户在
.send或
。显示
关于一个单元格。
有办法吗?
如何实现此目的的示例如下,假设A列包含电子邮件地址,B列包含“显示”或“发送”:
Sub LoopThroughRows_SendEmail()
Dim OutApp As Object: Set OutApp = CreateObject("Outlook.Application")
Dim OutMail As Object: Set OutMail = OutApp.CreateItem(0)
Dim i As Long
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Sheet1")
'declare and set the worksheet you are working with
For i = 2 To ws.UsedRange.Rows.Count
'loop from Row 2 To Last Row in UsedRange
With OutMail
.To = ws.Cells(i,"A").Value 'get email address from Column A
.CC = ""
.BCC = ""
.Subject = "Subject"
.Body = "Please contact us to discuss blah blah blah......"
If ws.Cells(i,"B").Value = "Display" Then
'if column B contains "Display" then .Display,else .Send
.Display
ElseIf ws.Cells(i,"B").Value = "Send" Then
.Send
End If
End With
Next i
End Sub