0

Im creating a form where all credentials are automatically populated by anyone who accesses it. However I am facing difficulties trying to get the user's email address.

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

UserEmail = olNS.CurrentUser.AddressEntry.GetExchangeUser.PrimarySmtpAddress

This one line could be taking >1 minute of some user's PC and few seconds on other's. Is there a way to skip this if its taking more than 5 seconds for example?

I'm thinking of applying application.ontime but dont know how to go from there.

Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
  • What you want to achieve is not trivial. Usually, all your VBA code runs in a single thread. That means whenever any function gets stuck for a while, your whole code execution halts. [This question is about asynchronous calls in VBA](https://stackoverflow.com/questions/24574884/excel-vba-make-a-script-asynchronous) and basically says: It's not possible. So you would either need to find a way to modify the function that returns the email address to return early. Or launch a second VBA host and have it retrieve the address for you. If it does not provide it in time, continue without it. – Inarion Apr 23 '18 at 09:55
  • Hey good idea to launch second vba host. I'll give it a try. – Aizat Kassim Apr 24 '18 at 16:08

1 Answers1

0

VBA is not designed for running multithreaded calls. If you want to optimize performance of your Outlook VBA solutions you may consider creating a COM add-in instead. See Chapter 12: Migrating VBA Solutions to VSTO for more information.

Before accessing the address entry object I'd suggest getting the Address property value. It returns a string representing the e-mail address of the Recipient. If it corresponds to an SMTP address then you may skip unnecessary calls.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45