【发布时间】:2013-02-11 17:37:32
【问题描述】:
有点问题,希望有人能帮帮我。
(Outlook 2010 VBA)
这是我当前的代码,我需要的是当我点击邮件时(只有我点击的邮件,而不是文件夹/同一个地方的每封邮件) 它必须检查邮件的发件人是否已经在我的联系人或 地址簿“所有用户”, 如果还不是其中之一,请打开 AddContact 窗口并填写他/她的信息
还不行的是:
- 最重要的是,当我点击邮件时它不会运行脚本
- 当前检查联系人是否已存在不起作用 并带有一个 vbMsgBox (是或否和响应的东西),这不是我想要/需要的 如果联系人已经存在,则无需发生任何事情。
我希望我提供了足够的信息,有人可以在这里帮助我:)
Sub AddAddressesToContacts(objMail As Outlook.MailItem)
Dim folContacts As Outlook.MAPIFolder
Dim colItems As Outlook.Items
Dim oContact As Outlook.ContactItem
Dim oMail As Outlook.MailItem
Dim obj As Object
Dim oNS As Outlook.NameSpace
''don't want or need a vbBox/ask box, this is a part of the current contactcheck
''wich doesn't work and is totaly wrong :P
Dim response As VbMsgBoxResult
Dim bContinue As Boolean
Dim sSenderName As String
On Error Resume Next
Set oNS = Application.GetNamespace("MAPI")
Set folContacts = oNS.GetDefaultFolder(olFolderContacts)
Set colItems = folContacts.Items
''this selects the mail that is currently selected.
''what i want is that the sender of the new incoming mail gets added to contacts
''(ofcourse, if that contact doesn't exsist yet)
''so the new incoming mail gotta be selected.
For Each obj In Application.ActiveExplorer.Selection
If obj.Class = olMail Then
Set oContact = Nothing
bContinue = True
sSenderName = ""
Set oMail = obj
sSenderName = oMail.SentOnBehalfOfName
If sSenderName = ";" Then
sSenderName = oMail.SenderName
End If
Set oContact = colItems.Find("[FullName] = '" & sSenderName & "'")
''this part till the --- is wrong, i need someting to check if the contact (the sender)
''already exsists. Any ideas?
If Not (oContact Is Nothing) Then
response = vbAbort
If response = vbAbort Then
bContinue = False
End If
End If
''---------
If bContinue Then
Set oContact = colItems.Add(olContactItem)
With oContact
.Email1Address = oMail.SenderEmailAddress
.Email1DisplayName = sSenderName
.Email1AddressType = oMail.SenderEmailType
.FullName = oMail.SenderName
'.Save
oContact.Display
End With
End If
End If
Next
Set folContacts = Nothing
Set colItems = Nothing
Set oContact = Nothing
Set oMail = Nothing
Set obj = Nothing
Set oNS = Nothing
End Sub
嘿,我还有最后一个问题,
'sets the name of the contact
Set oContact = colItems.Find("[FullName] = '" & sSenderName & "'")
'checks if the contact exsist, if it does exit the for loop
If Not oContact Is Nothing Then
Exit For
End If
End If
这会检查姓名是否已在联系人中, 我需要它检查电子邮件地址是否在联系人中, 你能帮帮我吗?
我有这样的想法
set oSendermail = ?the e-mailaddress?
If Not oSendermail Is Nothing Then
Exit For
End If
End If
【问题讨论】:
-
定义一个规则,如果发件人包含在您的地址簿中,则将所有传入邮件移动到您的收件箱,然后停止规则处理。然后,仅对您的地址簿中不存在的发件人调用第二条规则。第二条规则应该调用一个 VBA 子例程,该子例程在将邮件移动到收件箱之前自动将发件人添加到地址簿。这里解释了如何定义规则:superuser.com/questions/174145/…
-
嘿,感谢您的快速反应,这是我从老板那里收到的任务,并且必须在整个公司运行,它确实必须检查发件人是否存在,如果不存在如果您单击邮件,而不是当您收到新邮件时,请不要打开 addContact 窗口。我希望你能进一步帮助我:)
-
好的。如果您的第一条规则具有发件人在地址簿中的前提条件,则这意味着发件人存在。在用户可以点击邮件之前执行这些规则。您还有其他顾虑吗?
-
我明白了,你能举例说明我是怎么做到的吗? ~谢谢
标签: vba ms-office outlook-2010