如何在 excel 中使用 vba 在 outlook 中发送邮件
How to send mail in outlook with vba in excel
我有一个 excel sheet,其中包含个人信息,例如姓名、电子邮件地址等,我还有一个 VBA 代码,用于选择特定范围内的单元格(在本例中为范围 R)然后调用 VBA 宏来发送邮件。
但是如何将特定人员的电子邮件地址分配给我的 VBA 代码?
例如:
如果我点击单元格 R5,那么 VBA 宏应该开始 运行 发送邮件到单元格 M5 和单元格 O5 中的电子邮件地址,或者如果我点击单元格 R10,那么它应该通过电子邮件发送到电子邮件单元格 M10 和单元格 O10 中的地址。
请参阅下面我目前拥有的代码:
当我单击范围 R 中的任何单元格时,将触发以下 VBA 宏
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("R6:R1000000")) Is Nothing Then
Call Send_Email
End If
End If
End Sub
宏Send_Email:
Sub Send_Email()
Dim EmailApp As Outlook.Application
Dim NewEmailItem As Outlook.MailItem
Dim Scr As String
Set EmailApp = New Outlook.Application
Set NewEmailItem = EmailApp.CreateItem(olMailItem)
NewEmailItem.To = ****** here should be the cell reference ******
'NewEmailItem.CC = ****** here should be the cell reference ******
NewEmailItem.Subject = "abcd"
With NewEmailItem
.HTMLBody = "Hello abcd" etc.
End With
End Sub
这是完全相同的示例代码 - 显示如何根据存储在工作簿中的数据向收件人列表发送电子邮件。收件人电子邮件地址必须在 A 列,电子邮件正文必须在活动 sheet:
的第一个文本框中
Sub Sample()
'Setting up the Excel variables.
Dim olApp As Object
Dim olMailItm As Object
Dim iCounter As Integer
Dim Dest As Variant
Dim SDest As String
'Create the Outlook application and the empty email.
Set olApp = CreateObject("Outlook.Application")
Set olMailItm = olApp.CreateItem(0)
'Using the email, add multiple recipients, using a list of addresses in column A.
With olMailItm
SDest = ""
For iCounter = 1 To WorksheetFunction.CountA(Columns(1))
If SDest = "" Then
SDest = Cells(iCounter, 1).Value
Else
SDest = SDest & ";" & Cells(iCounter, 1).Value
End If
Next iCounter
'Do additional formatting on the BCC and Subject lines, add the body text from the spreadsheet, and send.
.BCC = SDest
.Subject = "FYI"
.Body = ActiveSheet.TextBoxes(1).Text
.Send
End With
'Clean up the Outlook application.
Set olMailItm = Nothing
Set olApp = Nothing
End Sub
在您的场景中,您可以使用单独的功能来发送电子邮件,您可以通过参数传递所需的数据。
我有一个 excel sheet,其中包含个人信息,例如姓名、电子邮件地址等,我还有一个 VBA 代码,用于选择特定范围内的单元格(在本例中为范围 R)然后调用 VBA 宏来发送邮件。
但是如何将特定人员的电子邮件地址分配给我的 VBA 代码?
例如: 如果我点击单元格 R5,那么 VBA 宏应该开始 运行 发送邮件到单元格 M5 和单元格 O5 中的电子邮件地址,或者如果我点击单元格 R10,那么它应该通过电子邮件发送到电子邮件单元格 M10 和单元格 O10 中的地址。
请参阅下面我目前拥有的代码:
当我单击范围 R 中的任何单元格时,将触发以下 VBA 宏
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("R6:R1000000")) Is Nothing Then
Call Send_Email
End If
End If
End Sub
宏Send_Email:
Sub Send_Email()
Dim EmailApp As Outlook.Application
Dim NewEmailItem As Outlook.MailItem
Dim Scr As String
Set EmailApp = New Outlook.Application
Set NewEmailItem = EmailApp.CreateItem(olMailItem)
NewEmailItem.To = ****** here should be the cell reference ******
'NewEmailItem.CC = ****** here should be the cell reference ******
NewEmailItem.Subject = "abcd"
With NewEmailItem
.HTMLBody = "Hello abcd" etc.
End With
End Sub
这是完全相同的示例代码 - 显示如何根据存储在工作簿中的数据向收件人列表发送电子邮件。收件人电子邮件地址必须在 A 列,电子邮件正文必须在活动 sheet:
的第一个文本框中Sub Sample()
'Setting up the Excel variables.
Dim olApp As Object
Dim olMailItm As Object
Dim iCounter As Integer
Dim Dest As Variant
Dim SDest As String
'Create the Outlook application and the empty email.
Set olApp = CreateObject("Outlook.Application")
Set olMailItm = olApp.CreateItem(0)
'Using the email, add multiple recipients, using a list of addresses in column A.
With olMailItm
SDest = ""
For iCounter = 1 To WorksheetFunction.CountA(Columns(1))
If SDest = "" Then
SDest = Cells(iCounter, 1).Value
Else
SDest = SDest & ";" & Cells(iCounter, 1).Value
End If
Next iCounter
'Do additional formatting on the BCC and Subject lines, add the body text from the spreadsheet, and send.
.BCC = SDest
.Subject = "FYI"
.Body = ActiveSheet.TextBoxes(1).Text
.Send
End With
'Clean up the Outlook application.
Set olMailItm = Nothing
Set olApp = Nothing
End Sub
在您的场景中,您可以使用单独的功能来发送电子邮件,您可以通过参数传递所需的数据。