首页 > 解决方案 > 在发送 Outlook 邮件之前接收消息提示,基于 Excel 文件中的数据

问题描述

我们与外包和自由职业者合作,很难跟踪谁不可用以及何时可用。

我拼凑了一个 Excel 文件,其中用户表单简化了输入日期,人员(通过他们的电子邮件地址输入)在 Excel 工作表中不可用。

下一步是将这个 Excel 文件“连接”到 Outlook,这样当我单击“发送”或输入电子邮件地址时,就会出现一个宏:

  1. 检查输入的电子邮件地址是否在 Excel 文件的工作表中
  2. 检查当前时刻是否在该名称旁边的不可用日期之间
  3. 两个条件都满足的时候给我提示信息告诉我这个人不可用,让我取消发送邮件。
  4. 可选:消息提示的字段中填写了该人相应的不可用时间段

是否可以通过这种方式将 Excel 连接到 Outlook,以便我们可以继续使用 Outlook 编写邮件?

简化后,它应该看起来像这样:

Click “Send” in Outlook email window
Before sending, call Excel file (does not need to be visible)
Check e-mail address column (column A)
 Matches “To” field in Outlook email window?
    If No, Send email
    If Yes, Check “From” date column (column C) next to corresponding email address
     Present date is equal to or later than “From” date?
        If No, Send Email
        If Yes, Check “Until” Date column (column D): present date is earlier than or equal to present date?
            If Yes, message prompt: “Name (column B) is not available from “From” until “Until”. Do you still want to send the e-mail?
            Buttons:
                Yes: Send Email
                No: Close prompt, do not send email, but keep email open.
            If No, Send Email

Excel 文件中用户窗体的代码:

Private Sub CommandDate1_Click()
    DatePicker1.Show
    AbsencePlannerUserForm.StartTextBox.SetFocus
End Sub

Private Sub CancelButton_Click()
    Unload Me
End Sub

Private Sub ClearButton_Click()
    Call AbsencePlannerUserForm_Initialize
End Sub

Private Sub CommandDate2_Click()
    DatePicker2.Show
    AbsencePlannerUserForm.EndTextBox.SetFocus
End Sub

Private Sub EndTextBox_Change()

End Sub

Private Sub ExtraInfoTextBox_Change()

End Sub

Private Sub OKButton_Click()
    Dim M_Date As Date
    Dim M_Item As String

    M_Date1 = StartTextBox
    M_Date2 = EndTextBox
    M_Item = EmailTextBox
    M_Info = ExtraInfoTextBox
    Application.ScreenUpdating = False
    LastRow = Abwesenheit1.Cells(Rows.Count, "D").End(xlUp).Row

    For rw = 2 To LastRow
        If Abwesenheit1.Cells(rw, "A") = M_Item And Cells(rw, "C") = M_Date1 _
          And Cells(rw, "D") = M_Date2 And Cells(rw, "E") = M_Info Then GoTo Passem
    Next rw
    GoTo NO_Dups
    
Passem:
    Application.ScreenUpdating = True
    
    MsgBox "Der Urlaub für " & M_Item & " vom " & M_Date1 & " bis zum " & _
      M_Date2 & " ist schon eingetragen."
    Exit Sub
    
NO_Dups:
    
    Dim emptyRow As Long
    
    'Make Abwesenheit1 active
    Abwesenheit1.Activate
    
    'Determine emptyRow
    emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1
    
    'Transfer information
    Cells(emptyRow, 1).Value = EmailTextBox.Value
    Cells(emptyRow, 3).Value = StartTextBox.Value
    Cells(emptyRow, 4).Value = EndTextBox.Value
    Cells(emptyRow, 5).Value = ExtraInfoTextBox.Value
    
    Application.Visible = True
    Me.Hide
    
End Sub
    
Private Sub AbsencePlannerUserForm_Click()
    
End Sub
    
Private Sub AbsencePlannerUserForm_Initialize()
    'Empty EmailTextBox
    EmailTextBox.Value = ""
    
    'Empty StartTextBox
    StartTextBox.Value = ""
    
    'Empty EndTextBox
    EndTextBox.Value = ""
    
    'Empty ExtraInfoTextBox
    ExtraInfoTextBox.Value = ""
    
    'Set Focus on EmailTextBox
    EmailTextBox.SetFocus
End Sub
    
Sub open_form()
    Application.Visible = False
    UserForm1.Show vbModeless
End Sub

Private Sub StartTextBox_Change()
    
End Sub
   
Private Sub UserForm_Click()
    
End Sub
    
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    Application.Visible = True
    Me.Hide
End Sub

标签: excelvbaemailoutlookprompt

解决方案


这是可以做到的。您需要在 VBA 环境中通过 excel 创建对 Outlook 的引用。要做到这一点:

从工具/参考中激活 Outlook 库

 (库的编号可能在您的计算机上有所不同)

然后调用前景:

https://docs.microsoft.com/en-us/office/vba/outlook/concepts/getting-started/automating-outlook-from-a-visual-basic-application

您可以设置一个条件,如果它匹配以显示包含所有不可用信息的用户表单,并添加按钮,当单击该按钮时退出子并关闭 Outlook 消息。

如果您发布代码,它会更容易处理它,但基本上,调用 Outlook,创建一个带有按钮的用户表单以在需要时关闭所有内容。


推荐阅读