2

I am developing a mail macro that a number of users will run in Outlook. Currently I use the following approach to search my Inbox...

Set objNamespace = Application.GetNamespace("MAPI")
'Replace line below with code that sets olShareName equal to the current 'outlook user's primary e-mail account.
Set olShareName = objNamespace.CreateRecipient("rpullman@dogs.com")
Set objFolder = objNamespace.GetSharedDefaultFolder(olShareName, olFolderInbox)
Set DestFolder = objNamespace.GetSharedDefaultFolder(olShareName, olFolderToDo)

strFilter = "@SQL=" & Chr(34) & "urn:schemas:httpmail:subject" & Chr(34) & " like '%Dogs Secretly Control the World%'"

Set filteredItems = objFolder.Items.Restrict(strFilter)

I want to make this code portable in the sense that the olShareName variable will be set to whomever is running the macro's primary e-mail address.

If, for instance, my colleagues were to use this macro, and they had the following e-mail addresses (that are accessed through outlook): bailey@dogs.com and molly@dogs.com, they should be able to run it without having to key in any modification to the code.

Obviously, I could use an input-box and have them type in their e-mail address, but I want to avoid that.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
PocketLoan
  • 534
  • 4
  • 13
  • 28
  • Come on, somebody take a stab. – PocketLoan Jun 16 '16 at 18:09
  • 1
    I'll take a stab with... a google for `vba outlook get account email address`, click the first link http://stackoverflow.com/questions/26519325/how-to-get-the-email-address-of-the-current-logged-in-user and the second answer describes getting `oExchUser.PrimarySmtpAddress` through Outlook.application :P – TessellatingHeckler Jun 17 '16 at 05:28
  • 1
    Can we see the rest of your code? – 0m3r Jun 17 '16 at 05:39

1 Answers1

2

Use the NameSpace.CurrentUser property to get a Recipient object for the current user. This will return the address associated with the default account in the active Outlook profile. If this is an Exchange account you will need to access Recipient.AddressEntry.GetExchangeUser() to get ExchangeUser.PrimarySMTPAddress. Although you really only need the Recipient object to use as the parameter for NameSpace.GetSharedDefaultFolder.

If you need the addresses of that user's aliases then I believe you need to use MAPI directly (or Redemption). But if you want to access the email address of the other accounts configured in the profile then you can loop through NameSpace.Accounts and check Account.SmtpAddress.

Eric Legault
  • 5,706
  • 2
  • 22
  • 38
  • Thanks, Another way to do it is to avoid the development of an Outlook macro and use an Excel Macro that opens Outlook... `Dim OutApp As Object Dim OutMail As Object Set OutApp = CreateObject("Outlook.Application") Set OutMail = OutApp.CreateItem(0) With OutMail .Display` – PocketLoan Jun 22 '16 at 14:28