Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am pretty new with C# but now I have to write a software that reports the user currently logged in Outlook. To do that I'm using the following code:

public string AccountLogged
        {
            get
            {
                try
                {
                    return (Marshal.GetActiveObject("Outlook.Application") as Outlook.Application).Session.Accounts[1].DisplayName;
                }
                catch (Exception)
                {
                    return string.Empty;
                }
            }
        }


then in the Application.Idle event, I update several labels (some of them shown data from outlook and some other from othe sources) but only one is not updating on the form immediately:

lbUserAccount.Text = ComVar.otlConnector.AccountLogged;


Looking into the vars while debugging all the values are correct; even opening and closing Outlook several times data are updated correctly.
The problem is that the label on the form updates immediately only when I assign the value string.Empty otherwise it does only if I click on the form.

What I have tried:

-> Instead of updating this label from Application.Idle I tried System.Windows.Forms.Timer
-> Invalidate
-> Refresh
-> Update
Posted
Updated 5-Jul-17 23:06pm
Comments
Matteo Sala 6-Jul-17 10:21am    
I have tried to isolate as much as possible the cause of the malfunction finding that the issue is derived from the following code:

return (Marshal.GetActiveObject("Outlook.Application") as Outlook.Application).Session.Accounts[1].DisplayName;

1 solution

to refresh the label on your Form just use Control.Invoke Method like :
this.lbUserAccount.BeginInvoke((MethodInvoker) delegate() {this.lbUserAccount.Text = ComVar.otlConnector.AccountLogged; });


the follwoing solution is part of my code that working fine for my needs

using Outlook = Microsoft.Office.Interop.Outlook;

.
.
.
public string AccountLogged
{
    get
    {
		return GetDefaultOutlookAccount();
    }
}

private string GetDefaultOutlookAccount()
 {
	  Outlook.NameSpace session = null;
      Outlook.Application outlookApp = GetOutlookApplicationObject(out session);
	 
     if (session == null)
         return null;

     string account = string.Empty;
     try
     {
         Outlook.AddressEntry addrEntry = session.CurrentUser.AddressEntry;
         //Exchange server
         if (addrEntry.Type == "EX")
         {
             Outlook.ExchangeUser manager = session.CurrentUser.AddressEntry.GetExchangeUser();
             if (manager != null)
                 account = manager.PrimarySmtpAddress;
         }
         else
         {
             account = session.CurrentUser.Address;
         }
     }
     catch (Exception ex)
     {
         //do something or
	    //throw new Exception(ex);
     }

     return account.ToLower();
 }

private Outlook.Application GetOutlookApplicationObject(out Outlook.NameSpace session)
{
    Outlook.Application app = null;
    session = null;
    // Check whether there is an Outlook process running.           
    if (Process.GetProcessesByName("OUTLOOK").Length > 0)
    {
        try
        {
            //If so, use the GetActiveObject method to obtain the process and cast it to an Application object. 
            app = (Outlook.Application)Marshal.GetActiveObject("Outlook.Application");
            session = app.Session;
        }
        catch (Exception ex)
        {
            //do something or
			//throw new Exception(ex);
        }
    }

    //if  Outlook process not running or exception thrown
    //create a new instance of Outlook and log on to the default profile. 
    if (app == null)
    {
        try
        {
            app = new Outlook.Application();
            session = app.GetNamespace("MAPI");          
            session.Logon("", "", Missing.Value, Missing.Value);
        }
        catch (Exception ex)
        {
            //do something or
			//throw new Exception(ex);
        }
    }

	//now resolve current user
    if (app != null && app.Session != null && session.CurrentUser != null)
        session.CurrentUser.Resolve();

    return app;
}
 
Share this answer
 
Comments
Matteo Sala 6-Jul-17 5:27am    
Hi jekin77, thanks but in order to have the label updated on the form I still need to click on the form.
jekin77 6-Jul-17 5:42am    
do you try to use invoker in in the Application.Idle event ?

you cal also create new label on you form and set this value to eliminate problems with designer
Matteo Sala 6-Jul-17 5:52am    
yes, I tried the invoker and creating a new label but still not working. The content of this label updates only if I provide the focus to my form. The crazy thing is that all other labels are updating immediately without have to wait to receive focus.
jekin77 6-Jul-17 5:56am    
try to call Applicton.DoEvents() Method after setting the value

https://msdn.microsoft.com/de-de/library/system.windows.forms.application.doevents(v=vs.110).aspx

you can also save the value in variable temporary and than set label text :

string tmpValue = ComVar.otlConnector.AccountLogged;
lbUserAccount.Text = tmpValue;

Matteo Sala 6-Jul-17 6:03am    
Not working either. the label is still updating only when I start the application then behave as described above

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900