【发布时间】:2017-05-22 21:35:46
【问题描述】:
我的最终目标是在 Outlook 或 MS Exchange 中提供有关联系人的任何信息,并在不遇到任何警告消息的情况下获取他们的姓名和电子邮件地址。
我开发了一个运行良好的功能,除了我从Outlook Object Model Guard (OMG) 收到弹出警告消息的部分,我需要绕过它而不使用任何付费加载项、CDP、兑换或更改设置在 Outlook 应用程序(信任中心)等中的程序访问中。
我的代码在 Excel VBA 中,我没有提前绑定到 Outlook 库。
我知道访问某些对象或方法会触发OMG 弹出警告并等待用户确认。我想知道有没有一种方法可以在 VBA 中以编程方式禁用OMG,然后再启用它?
Excel VBA 函数:
Public Function GetContactObject2(strInput As String) As Object
Dim chk As Boolean
Dim sEmailAddress As String
Dim olApp As Object
Dim olNS As Object 'NameSpcase OL identifiers
Dim olAL As Object 'AddressList An OL address list
Dim olRecip As Object 'Outlook Recipient Object
Dim olAddrEntry As Object 'AdressEntry An Address List entry
Dim olCont As Object 'ContactItem An Outlook contact item
Dim olExchUser As Object 'outlook Exchange User Object
Dim obj As Object
Dim oPA As Object
chk = True 'assume everything is running fine
Err.Clear
'On Error GoTo Handler
Set olApp = GetObject(, "Outlook.Application")
'If an instance of an existing Outlook object is not available, an error will occur (Err.Number = 0 means no error):
If Err.Number <> 0 Then
Set olApp = CreateObject("Outlook.Application")
End If
Set olNS = olApp.GetNamespace("MAPI")
'Set olAL = olNS.AddressLists("Global Address List")
Set olRecip = olNS.createrecipient(strInput)
olRecip.Resolve 'this line will cause Outlook Security Manager to pop up a message to allow or deny access to email
'Check if the entry was resolved
If olRecip.Resolved Then
Set olAddrEntry = olRecip.AddressEntry
Set olCont = olAddrEntry.GetContact
If Not (olCont Is Nothing) Then
'this is a contact
'olCont is ContactItem object
MsgBox olCont.FullName
Else
Set olExchUser = olAddrEntry.GetExchangeUser
If Not (olExchUser Is Nothing) Then
'olExchUser is ExchangeUser object
'MsgBox olExchUser.PrimarySmtpAddress
Set obj = olExchUser
Else
Set obj = Nothing
End If
End If
Else 'Recipient was not found at all in the Global Address List
Set obj = Nothing
End If
On Error GoTo 0
Set GetContactObject2 = obj
Exit Function
Handler:
MsgBox "Err #: " & Err.Number & vbNewLine & Err.Description
End Function
调用第一个函数的 Excel VBA 函数 2:
'=========================================
' Get Current User Email Address Function
'=========================================
' Gets current user's email address using outlook MAPI namespace
' RETURNS: user email if found, otherwise a zero-length string
Public Function GetCurrentUserEmailAddress2() As String
Dim chk As Boolean
Dim strInput As String 'any string that can be resolved by outlook to retrieve contact item
Dim sEmailAddress As String
Dim olApp As Object
Dim olNS As Object
Dim obj As Object 'object for contact
chk = True 'assume everything is running fine
Err.Clear
On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
'if an instance of an existing Outlook object is not available, an error will occur (Err.Number = 0 means no error):
If Err.Number <> 0 Then
Set olApp = CreateObject("Outlook.Application")
End If
'''' Set olNS = olApp.GetNamespace("MAPI")
'This line will cause Outlook to pop a warning window that a program wants to have access your email address
'''' sEmailAddress = olNS.Accounts.Item(1).SmtpAddress
'Get a contact object and then extract the email from there
'NOTE: some users' alias is their windows login, but some have different alias so it may fail. The best bet is finding the
'email address using some other way and using it as the input which will almost never fail
strInput = olApp.Session.CurrentUser.Address
Set obj = GetContactObject2(strInput)
If obj Is Nothing Then
'Try one more time with windows login
strInput = Environ("UserName")
Set obj = GetContactObject2(strInput)
If obj Is Nothing Then
chk = False
Else
sEmailAddress = obj.PrimarySmtpAddress
End If
Else
sEmailAddress = obj.PrimarySmtpAddress
End If
'Return a zero length string if by any chance email could not be retrieved, else validate it
If chk = True Then
chk = ValidateEmailAddress(sEmailAddress, bShowMessage:=False)
Else
sEmailAddress = ""
End If
On Error GoTo 0
'Assign string to function
GetCurrentUserEmailAddress2 = sEmailAddress
End Function
【问题讨论】:
-
让我们希望没有办法绕过该警告,否则黑客将有一个好日子!为什么不换一种方式工作 - 将代码放在 Outlook 中,然后通过“Excel.Application”将输出写入 Excel。
-
@YowE3K 我的用户太多,我做不到。 Sendkeys 等有一些技巧,但它们对我来说都不好看。有一些方法可以通过操作注册表来禁用它,所以这对黑客来说并不是一个真正的问题。 OMG 适用于一些基本病毒,这些病毒如今甚至可以通过免费的防病毒软件轻松捕获,所以我想应该有一些选项让 OMG 识别哪个应用程序是内部的并且应该被信任。微软停止了很多东西的进一步开发,包括这些东西!
-
你在哪个办公室?
-
@0m3r 办公室 2010
-
注意
email属性的名字是mail, stackoverflow.com/questions/785527/…。此外,您必须更改链接中的代码attr = "mail"和WScript.Echo rs.Fields("mail").Value。我不知道你尝试了什么,所以你可能已经尝试过了。