简体   繁体   English

从 Excel 发送 Outlook 电子邮件

[英]Send Outlook email from Excel

In my Excel file I have a reminder column, when the assigned date has passed then "Send Reminder" pops up in the column.在我的 Excel 文件中,我有一个提醒列,当指定的日期过去时,列中会弹出“发送提醒”。

I am trying to send a reminder email.我正在尝试发送提醒电子邮件。

I ran into trouble with "Sub or function not defined" but I fixed it by adding Solver into my references.我遇到了“未定义子或函数”的问题,但我通过将求解器添加到我的引用中来修复它。 Now when I click on macro > run , no email is sent.现在,当我单击 macro > run 时,不会发送电子邮件。

Sub SendEmail()
    Dim OutLookApp As Object
    Dim OutLookMailItem As Object
    Dim iCounter As Integer
    Dim MailDest As String
    
    Set OutLookApp = CreateObject("OutLook.application")
    Set OutLookMailItem = OutLookApp.CreateItem(0)
    
    With OutLookMailItem
        MailDest = ""
        For iCounter = 1 To WorksheetFunction.CountA(Column(4))
            If MailDest = "" And Cells(iCounter, 4).Offset(0, -1) = "Send Reminder" Then
                MailDest = Cells(iCounter, 4).Value
            ElseIf MailDest <> "" And Cells(iCounter, 4).Offset(0, -1) = "Send Reminder" Then
                MailDest = MailDest & ":" & Cells(iCounter, 4)
            End If
        Next iCounter
    
        .BCC = MailDest
        .Subject = "FYI"
        .Body = "Reminder"
        .Send
    End With
    
    Set OutLookMailItem = Nothing
    Set OutLookApp = Nothing
End Sub

The columns are Name - Date - Reminder - Email (1, 2, 3, 4) and I am using Excel 2010.这些列是名称 - 日期 - 提醒 - 电子邮件(1、2、3、4),我使用的是 Excel 2010。

First select the outlook library from Tools--> References--> Microsoft outlook 12.0 library or any other versions of outlook library you have .首先从工具--> 参考--> Microsoft Outlook 12.0 库或您拥有的任何其他版本的 Outlook 库中选择 Outlook 库。

Sub Email()
'Dim OutlookApp As Outlook.Application
Dim OutlookApp
Dim objMail
Dim mydate1 As Date
Dim mydate2 As Long
Dim datetoday1 As Date
Dim datetoday2 As Long
Dim x As Long
lastrow = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
For x = 2 To lastrow

mydate1 = Cells(x, 6).Value
mydate2 = mydate1

Cells(x, 9).Value = mydate2

datetoday1 = Date
datetoday2 = datetoday1

Cells(x, 10).Value = datetoday2

If mydate2 - datetoday2 = 1 Then

'Set OutlookApp = New Outlook.Application
Set OutlookApp = CreateObject("Outlook.Application")
Set objMail = OutlookApp.CreateItem(olMailItem)
objMail.To = Cells(x, 5).Value
k
With objMail
.Subject = "Payment Reminder"
.Body = "Your payment is due." & vbCrLf & "Kindly ignore if already paid." & vbCrLf & "Hari"
'.Display
.send
End With
Cells(x, 7) = "Yes"
Cells(x, 7).Interior.ColorIndex = 3
Cells(x, 7).Font.ColorIndex = 2
Cells(x, 7).Font.Bold = True
Cells(x, 8).Value = mydate2 - datetoday2
End If
Next
Set OutlookApp = Nothing
Set objMail = Nothing

End Sub

This will update your workbook with remainder Yes after sending emails这将在发送电子邮件后更新您的工作簿剩余部分是

Title   F.Name  L.Name  Mob.No  Email    Date   Remainder   Days Diff   Date No Today as No
Mr  trolls  t   9787687644  xxx@gmail.com   9/5/2015    Yes 1   42252   42251.

Hope it helps you希望对你有帮助

Setup for a subroutine to send mail based on selection criteria.设置子程序以根据选择标准发送邮件。

Set up your Workbook as follows:按如下方式设置您的工作簿:

In VB Editor under Tools|References find 'Microsoft Outlook xx.x Object Library', where xx.x represents the version of Outlook that you are working with.在工具|参考下的 VB 编辑器中,找到“Microsoft Outlook xx.x 对象库”,其中 xx.x 代表您正在使用的 Outlook 版本。 (see also: https://msdn.microsoft.com/en-us/library/office/ff865816.aspx ) This will make for easier coding as you get intellisense suggestions for your objects. (另请参阅: https : //msdn.microsoft.com/en-us/library/office/ff865816.aspx )这将使您在获得对象的智能感知建议时更容易编码。

Declare OutlookApp as public, above all other subs/functions etc.OutlookApp声明为公共,高于所有其他子/功能等。
(ie at the top of your 'coding'window) (即在“编码”窗口的顶部)

Public OutlookApp As Outlook.Application

your sendReminderMail() sub你的 sendReminderMail() 子

Sub SendReminderMail()
    Dim iCounter As Integer
    Dim MailDest As String

    On Error GoTo doOutlookErr:
    Set OutlookApp = New Outlook.Application
    
    For iCounter = 1 To WorksheetFunction.CountA(Columns(4))
        MailDest = Cells(iCounter, 4).Value
        
        If Not MailDest = vbNullString And Cells(iCounter, 4).Offset(0, -1) = "Send Reminder" Then
          sendMail MailDest
          MailDest = vbNullString
        End If
        
    Next iCounter

'basic errorhandling to prevent Outlook instances to remain open in case of an error.
doOutlookErrExit:
    If Not OutlookApp Is Nothing Then
        OutlookApp.Quit
    End If
    Exit Sub

doOutlookErr:
    MsgBox Err.Description, vbOKOnly, Err.Source & ":" & Err.Number
    Resume doOutlookErrExit
    
End Sub

added sendMail Function:添加了 sendMail 功能:

Function sendMail(sendAddress As String) As Boolean
    
    'Initiate function return value
    sendMail = False
    On Error GoTo doEmailErr:
    
    'Initiate variables
    Dim OutLookMailItem As Outlook.MailItem
    Dim htmlBody As String
    
    'Create the mail item
    Set OutLookMailItem = OutlookApp.CreateItem(olMailItem)
    
    'Create the concatenated body of the mail
    htmlBody = "<html><body>Mail reminder text.<br></body></html>"
    
    'Chuck 'm together and send
    With OutLookMailItem
    
        .BCC = sendAddress
        .Subject = "Mail Subject"
        .HTMLBody = htmlBody
        .Send
      
    End With
    
    sendMail = True

doEmailErrExit:
    Exit Function

doEmailErr:
    MsgBox Err.Description, vbOKOnly, Err.Source & ":" & Err.Number
    Resume doEmailErrExit
    
End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM