2015-10-18 52 views
-2
Sub SendReminderMail() 
    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(Columns(34)) 
     If MailDest = "" And Cells(iCounter, 34).Offset(0, -1) = "Send Reminder" Then 
     MailDest = Cells(iCounter, 34).Value 
     ElseIf MailDest <> "" And Cells(iCounter, 34).Offset(0, -1) = "Send Reminder" Then 
     MailDest = MailDest & ";" & Cells(iCounter, 34).Value 
     End If 
    Next iCounter 

    .BCC = MailDest 
    .Subject = "ECR Notification" 
    .HTMLBody = "Reminder: This is a test for an automatic ECR email notification. Please complete your tasks for ECR#" 
    .Send 
    End With 

    Set OutLookMailItem = Nothing 
    Set OutlookApp = Nothing 
End Sub 

需要编写代码以AE电子邮件在列中的值与 “设置提醒” 文本如何从代码发送电子邮件提醒

enter image description here

+1

GD MJac,请更改您的问题,以便代码实际显示为代码并提供您迄今尝试过的内容,哪些内容不起作用。你收到错误消息吗?给出一个解释你的代码样本应该做什么。你的问题没有包含足够的信息,并且在问题中缺乏精确性。 – mtholen

回答

0

GD mjac,

你是仍然害怕你的信息...?

您提供的代码收集所有地址并随后发送一条消息?我希望根据您的示例表/数据,您希望为每个“打开”的ECR代码发送电子邮件给每个收件人?

因此,假设如下:

  • 你想发送一封电子邮件,每行,其中列“发送提醒”是 真
  • 电子邮件地址“AH”将为每行有什么不同?

在你的代码使用Outlook.Application对象Set OutlookApp = CreateObject("Outlook.application"),小心打开的应用程序类型的对象,一定要确保他们将在代码完成的事件或当触发一个错误,否则可能会关闭可能最终会产生许多使用有价值的资源“运行”的Outlook实例。下面的代码具有一些基本的错误处理,以确保OutlookApp对象在不再需要时关闭。

设置您的工作簿,如下所示:

在VB编辑器下工具|引用找到“Microsoft Outlook中XX.X对象库”,其中XX.X表示您正在使用的Outlook版本。 (另请参阅:https://msdn.microsoft.com/en-us/library/office/ff865816.aspx)当您为对象获得智能感知建议时,这将使编码更轻松。

声明OutlookApp为公共,上述所有其它潜艇/功能等等,等等 (即,在您的 '编码' 窗口的顶部)

Public OutlookApp As Outlook.Application 

您sendReminderMail()子

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

    On Error GoTo doOutlookErr: 
    Set OutlookApp = New Outlook.Application 

    For iCounter = 1 To WorksheetFunction.CountA(Columns(34)) 
     MailDest = Cells(iCounter, 34).Value 
     ecr = Cells(iCounter, 34).Offset(0, -3).Value 

     If Not MailDest = vbNullString And Cells(iCounter, 34).Offset(0, -1) = "Send Reminder" Then 
      sendMail MailDest, ecr 
      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(sendAddress As String, ecr As Long) 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>Reminder: This is a test for an automatic ECR email notification.<br>" & _ 
       "Please complete your tasks for ECR#" & CStr(ecr) & "</body></html>" 

    'Chuck 'm together and send 
    With OutLookMailItem 

     .BCC = sendAddress 
     .Subject = "ECR Notification" 
     .HTMLBody = htmlBody 
     .Send 

    End With 

    sendMail = True 

doEmailErrExit: 
    Exit Function 

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


End Function 
+0

mtholen,这是令人印象深刻的。你的假设是现货。由于我是整个编程环境的新手,因此我没有正确表达我的问题。我剩下的唯一问题是如何使ecr值的字体变成粗体?是否带有语法.Font.Bold = True – mjac

+0

GD Mjac,最简单的方法是在“&Cstr(ecr)&”之后加上,之后再加。即“请完成任务ECR#”&CStr的(ECR)“”如果这个答案再回答你的问题通过点击问题上的投票箭头下接受图标接受的答案。 – mtholen