Excel和Outlook VBA:根据Excel文件中的数据,在Outlook中发送邮件之前先接收消息提示

时间:2019-01-03 12:23:10

标签: excel vba email outlook prompt

我对使用VBA完全陌生,一直在寻找和使用试错法来提出自己必须帮助我和我的同事的想法。但是,由于我现在再也找不到在线页面可以为我提供进一步的帮助,因此,我将自己开始,希望您能够为我提供帮助!

上下文:我们主要与外包和自由职业者合作,有时很难跟踪谁不可用以及直到何时。因此,我整理了一个Excel文件,其中的用户表单简化了Excel工作表中无法输入人员(通过其电子邮件地址输入)的输入日期。那是我的第一个VBA经验。

现在,下一步是我正在努力寻找解决方案的那一步。我想将此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

期待您有更多经验的输入!

1 个答案:

答案 0 :(得分:0)

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

从“工具/参考”中激活Outlook库

 (您的计算机上的库号可能会有所不同

然后致电Outlook:

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

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

如果您发布代码,则可以更轻松地进行处理,但基本上,请调用Outlook,创建一个带有按钮的用户表单,以在需要时关闭所有内容。