Bypass 'Object Model Guard' when retrieving information from Outlook (XL2007)

AOB

Well-known Member
Joined
Dec 15, 2010
Messages
660
Office Version
  1. 365
  2. 2016
  3. 2013
Platform
  1. Windows
Hi everybody,

I have some script to connect to an Outlook mailbox / folder and read the information for each mail item which a) contains a pdf attachment and b) was sent within a date range (defined by two date-picker controls on the form) into an array, to subsequently populate a combobox on a user form in Excel.

But when running, I get prompted in Outlook :

A program is trying to access e-mail address information stored in Outlook. If this is unexpected, click Deny and verify your antivirus software is up-to-date

So the user would have to manually allow access (for 1-10 minutes) in order for the script to run. While I can certainly see the reasoning behind this prompt, I'd like to bypass it / prevent it from stalling the process (also, not sure how to apply error-handling in the event that the user were to click 'Deny')

Found an interesting article here which suggests using the Application object (as it is trusted in Outlook VBA and therefore should bypass the security prompt) but am still getting the prompt.

Here's my code :

Code:
Dim olApp As Outlook.Application
Dim objNameSpace As Outlook.Namespace
Dim objFolder As Outlook.MAPIFolder
Dim objItem As MailItem
Dim strID As String
Dim objMail As MailItem
Dim att As Variant
Dim arTempEMails() As String

Set olApp = CreateObject("Outlook.Application")
Set objNameSpace = olApp.GetNamespace("MAPI")

ReDim arTempEMails(1 To 5, 1 To 1)    
lgNoOfEmails = 0

For Each objItem In objFolder.Items

[INDENT]If objItem.SentOn >= Me.dtStartDate.Value And _
objItem.SentOn < (Me.dtEndDate.Value + 1) Then


[/INDENT]
[INDENT=2]If objItem.Attachments.Count > 0 Then


[/INDENT]
[INDENT=3]For Each att In objItem.Attachments


[/INDENT]
[INDENT=4]If Right(att.DisplayName, 4) = ".pdf" Then


[/INDENT]
[INDENT=5]lgNoOfEmails = lgNoOfEmails + 1
ReDim Preserve arTempEMails(1 To 5, 1 To lgNoOfEmails)
strID = objItem.EntryID
Set objMail = objNameSpace.GetItemFromID(strID)

arTempEMails(1, lgNoOfEmails) = objMail.EntryID
[COLOR=#ff0000]arTempEMails(2, lgNoOfEmails) = objMail.SenderName
[/COLOR]arTempEMails(3, lgNoOfEmails) = objMail.Subject
arTempEMails(4, lgNoOfEmails) = Format(objMail.SentOn, "dd-mmm-yyyy hh:mm:ss")
arTempEMails(5, lgNoOfEmails) = att.DisplayName

GoTo NextEMail[/INDENT]
[INDENT=4]
End If[/INDENT]
[INDENT=3]
Next att[/INDENT]
[INDENT=2]
End If


[/INDENT]
[INDENT]ElseIf objItem.SentOn < Me.dtStartDate.Value Then[/INDENT]
[INDENT=2]
Exit For[/INDENT]
[INDENT]
End If

NextEMail:


[/INDENT]
Next objItem

It's the line in red where the security prompt kicks in.

Does anybody know any way of restructuring this so that Outlook does not flag a security concern?

Thanks in advance!

AOB
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
That article refers to code running within Outlook (or a loaded COM addin) deriving from the Application object, not code in a different application.
 
Upvote 0
Found an interesting article here which suggests using the Application object (as it is trusted in Outlook VBA and therefore should bypass the security prompt) but am still getting the prompt.

Hi AOB

I think that is implying that you should use Outlook's VBA as the "trusted" source. In other words, calling procedures stored in Outlook to access mail details.

A bit of a pain but certainly a realistic option for you.
 
Upvote 0
Ahh, I get it - thanks guys for clarifying for me!

Would really rather not get into coding in VBA so took the lazy option and skipped the line to retrieve the Sender Name altogether (this is the only piece which breaches the security of Outlook) I was only retrieving it in the first place to make it easier for users to identify the appropriate mail but I don't think it will be essential for them.

Thanks again for the tips!
 
Upvote 0
** Would really rather not get into coding in <ACRONYM title="visual basic for applications">Outlook </ACRONYM>**
 
Upvote 0

Forum statistics

Threads
1,215,324
Messages
6,124,250
Members
449,149
Latest member
mwdbActuary

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top