VBOffice

Security Model, Part 2

With this little trick you can send emails via Outlook from, for instance, Excel without raising the security prompt in Outlook.

Last modified: 2007/12/19 | Accessed: 28.893  | #31
◀ Previous sample Next sample ▶

Content

Reporter Reporter
VBOffice Reporter is an easy to use tool for data analysis and reporting in Outlook. A single click, for instance, allows you to see the number of hours planned for meetings the next month.

Publish the Application Objekt Variable

Some properties of the Outlook object model are blocked due to security reasons. For instance, if try to send an email from Excel via Outlook, the user gets a prompt and must explicitly allow the sending. Since Outlook 2003 you can workaround the issue: All you need to do is derive all objects from the instrinsic Application object.

Many VBA samples try to create a new instance of the own host application. That is unneccessary for all VBA projects, in Outlook 2003 it could even lead to a disabled VBA, and in any case it leads to that Outlook doesn' trust your code.

So the question is, how can you use the intrinsic Outlook Application object from within, say, Excel so that it trusts your code? The solution is simple: Make the property public. As this isn't officially supported, Microsoft could stop this method from working anytime.

Add the following code to the ThisOutlookSession module. It's important to declare it as public and the returned type as object.

Important: I strongly recommend that you use your own name for the public property, a name that nobody else knows.


tip  How to add macros to Outlook
Public Property Get MySecretPropertyName() As Object
  Set MySecretPropertyName = Application
End Property

The Problem

The following code demonstrates the issue: With the usualy call from Excel to Outlook the security prompt will be raised when you try to read, for instance, the address of an email. (It's assumed that Outlook is already running.)

Private Sub Test()
  Dim Outlook As Outlook.Application
  Dim Mail as Outlook.MailItem
  Dim Folder As Outlook.MAPIFolder

  Set Outlook = GetObject(, "Outlook.Application")
  Set Folder = Outlook.Session.GetDefaultFolder(olFolderInbox)
  Set Mail = Folder.Items(1)
  Debug.Print Mail.Recipients(1).Address
End Sub
SAM SAM
Determine the "identity" of your emails. Set with SAM the sender and the folder folder for sent items with the help of rules.

The Solution

Instead of deriving the email from the Application object that is returned by the GetObject function, we derive it from the property that we added to ThisOutlookSession. This way you won't see the security prompt.

Private Sub Test()
  Dim LockedOutlook As Outlook.Application
  Dim Outlook As Object
  Dim Mail as Outlook.MailItem
  Dim Folder As Outlook.MAPIFolder

  Set LockedOutlook = GetObject(, "Outlook.Application")

  Set Outlook = LockedOutlook.MySecretPropertyName

  Set Folder = Outlook.Session.GetDefaultFolder(olFolderInbox)
  Set Mail = Folder.Items(1)
  Debug.Print Mail.Recipients(1).Address
End Sub
ReplyAll ReplyAll
ReplyAll alerts you before unintentionally replying all, or if you are a confidential BCC recipient of the e-mail.
email  Send a message