View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
JP[_4_] JP[_4_] is offline
external usenet poster
 
Posts: 897
Default Grab Open Outlook Username from Excel

If you wanted to avoid the security prompt, a sneaky way to get the current
user name is to parse the text string in the name of the default mailbox. It
would always (usually) contain the name of the person who is setup for that
copy of Outlook. In this example, a Msgbox appears with the name of the
current user. You would need to use Instr and/or Mid to parse out the actual
person's name.

Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Application.StatusBar = "Checking For Existing Copy of Outlook..."
' test for instance of Outlook, exit if not able to start one
On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
If Err.Number < 0 Then
Set olApp = CreateObject("Outlook.Application")
End If
On Error GoTo 0

If olApp Is Nothing Then
Application.StatusBar = False
MsgBox "Cannot start Outlook. Please open Outlook and try again.", _
vbInformation
Exit Sub
End If

Set objNS = olApp.GetNamespace("MAPI")

Msgbox objNS.GetDefaultFolder(olFolderInbox).Parent

Set objNS = Nothing
Set olApp = Nothing
End Sub

On my system I would get a message box stating "Mailbox - Jimmy Pena" it
would then be a simple matter of extracting my name from this string.

This is "air code" from memory so you should proof it first before running.

HTH,
JP

"jayklmno" wrote:

Figured it out for myself...

Sub DisplayCurrentUser()
Dim myolApp As Outlook.Application
Dim myNamespace As Outlook.Namespace
Set myolApp = CreateObject("Outlook.Application")
Set myNamespace = myolApp.GetNamespace("MAPI")
MsgBox myNamespace.CurrentUser
End Sub

Only trouble is it prompts the Outlook Security pop box.

"jayklmno" wrote:

I want to grab the users Outlook Email address while an Excel Macro is
running (and assuming Outlook is also running on the machine). Is there a
quick and dirty way of doing this? If so, please point me in the right
direction.

Thanks!