Jump to content

Outlook, Get UserName


Recommended Posts

I have been looking for probably 2 hours not to find something that I thought would be easy.

I want to find out if Outlook is installed, If so is it configured, If it's configured what UserName is it configured to use.

Why is this so hard!!!

Finding if outlook is installed was easy. Just call the Outlook Com object and ensure it was created. It's after that where I'm stuck.

MSDN Link to the Outlook UserName info

my failing attempt so far. I have been going round and round for too long...

$oOutlook = ObjCreate("Outlook.Application")
if IsObj($oOutlook) Then
    $user = $oOutlook.Application.GetNameSpace("MAPI");.CurrentUser
    $id1 = $user.Account.UserName
    MsgBox(0, "UserName", $id1);$oOutlook.Account.UserName)
EndIf

Thanks for any help you can provide.

Mike

Link to comment
Share on other sites

  • Moderators

Try this:

#include <IE.au3>

_IEErrorHandlerRegister()
$oOutlook = ObjCreate("Outlook.Application")
If Not IsObj($oOutlook) Then
    MsgBox(0, "", "Unable to create object!")
    Exit
EndIf

$oNameSpace = $oOutlook.GetNameSpace ("MAPI")
$oAccounts = $oNameSpace.Accounts
$iCount = $oAccounts.Count
$i = 1
$sProp = ""
For $oAccount In $oAccounts
    With $oAccount
        $sProp &= "Account #: " & $i & @CR
        $sProp &= "Account Type: " & .AccountType & @CR
        $sProp &= "Class: " & .Class & @CR
        $sProp &= "Display Name: " & .DisplayName & @CR
        $sProp &= "SMTP Address: " & .SmtpAddress & @CR
        $sProp &= "User Name: " & .UserName
        If $i <> $iCount Then $sProp &= @CR & @CR
    EndWith
    $i += 1
Next

ConsoleWrite($sProp & @CR)
Link to comment
Share on other sites

Big_Daddy,

Thanks for the attempt but I just get this in the scite Console

edit: updated the cosole data: I origionally had this pasted at the bottom of a test script so the line numbers were off.

--> COM Error Encountered in Testing.au3
----> $IEComErrorScriptline = 11
----> $IEComErrorNumberHex = 80020006
----> $IEComErrorNumber = -2147352570
----> $IEComErrorWinDescription = Unknown name.
----> $IEComErrorDescription = 
----> $IEComErrorSource = 
----> $IEComErrorHelpFile = 
----> $IEComErrorHelpContext = 
----> $IEComErrorLastDllError = 0

--> COM Error Encountered in Testing.au3
----> $IEComErrorScriptline = 12
----> $IEComErrorNumberHex = 000000A9
----> $IEComErrorNumber = 169
----> $IEComErrorWinDescription = Variable must be of type 'Object'.
----> $IEComErrorDescription = 
----> $IEComErrorSource = 
----> $IEComErrorHelpFile = 
----> $IEComErrorHelpContext = 
----> $IEComErrorLastDllError = 0

--> COM Error Encountered in Testing.au3
----> $IEComErrorScriptline = 15
----> $IEComErrorNumberHex = 000000A9
----> $IEComErrorNumber = 169
----> $IEComErrorWinDescription = Variable is not of type 'Object'.
----> $IEComErrorDescription = 
----> $IEComErrorSource = 
----> $IEComErrorHelpFile = 
----> $IEComErrorHelpContext = 
----> $IEComErrorLastDllError = 0

I have been working on this more this morning and I finally got some code that will retrieve the UserName but it throws an Outlook security box, so sadly I can't use it.

;Works but prompts the user with a security message
$oOutlook = ObjCreate("Outlook.Application")
$oSession = ObjCreate("Redemption.RDOSession")
$oAddress = $oOutlook.GetNamespace("MAPI").CurrentUser.Name
MsgBox(0, "", $oAddress)
Edited by MikeOsdx
Link to comment
Share on other sites

  • Moderators

Works perfectly for me, I get something like this.

Account #: 1
Account Type: 0
Class: 105
Display Name: Microsoft Exchange Server
SMTP Address: someone@somewhere.com
User Name: UserName

Account #: 2
Account Type: 2
Class: 105
Display Name: someone@somewhere.com
SMTP Address: someone@somewhere.com
User Name: UserName

Account #: 3
Account Type: 2
Class: 105
Display Name: someone@somewhere.com
SMTP Address: someone@somewhere.com
User Name: UserName

Account #: 4
Account Type: 2
Class: 105
Display Name: someone@somewhere.com
SMTP Address: someone@somewhere.com
User Name: UserName

What version of Outlook are you testing on?

Link to comment
Share on other sites

Works perfectly for me, I get something like this.

What version of Outlook are you testing on?

I am using Outlook 2003 with Office 2007, but the Office 2007 shouldn't make a difference.

edit: and I am connected directly to an Exchange server, not POP3

Mike

Edited by MikeOsdx
Link to comment
Share on other sites

  • Moderators

See if this works for you.

#include <IE.au3>

_IEErrorHandlerRegister()
$oOutlook = ObjCreate("Outlook.Application")
If Not IsObj($oOutlook) Then
    MsgBox(0, "", "Unable to create object!")
    Exit
EndIf

$oNameSpace = $oOutlook.GetNameSpace ("MAPI")
$oUser = $oNameSpace.CurrentUser
If Not IsObj($oUser) Then
    MsgBox(0, "Error", "There is no current user.")
    Exit
EndIf

ConsoleWrite($oUser.Name & @CR)
Link to comment
Share on other sites

See if this works for you.

#include <IE.au3>

_IEErrorHandlerRegister()
$oOutlook = ObjCreate("Outlook.Application")
If Not IsObj($oOutlook) Then
    MsgBox(0, "", "Unable to create object!")
    Exit
EndIf

$oNameSpace = $oOutlook.GetNameSpace ("MAPI")
$oUser = $oNameSpace.CurrentUser
If Not IsObj($oUser) Then
    MsgBox(0, "Error", "There is no current user.")
    Exit
EndIf

ConsoleWrite($oUser.Name & @CR)
That code pulls the user name but like the sample I put in post#3, I get a security warning that the user must hit OK to. So sadly I can't really use it.

I think I am going to send an e-mail to the Scripting Guys on Microsofts Website, then convert it to AutoIT of course. It's just stupid how difficult this is.

Thanks,

Mike

Edited by MikeOsdx
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...