none
C# start Outlook as Administrator programatically RRS feed

  • Question

  • Hello,

    I try to generate a Outlook TaskItem out of a WordTemplate. It is no problem, all fine. But it just works when outlook is already running in Administrator mode.

    When I try to start the code to create a task item in Outlook when Outlook isn't running (outlookApp = new Outlook.ApplicationClass();) then I get the error:HRESULT: 0x80004004 (E_ABORT)

    Here is my code:

            private void ThisDocument_Startup(object sender, System.EventArgs e)
            {
    
                try
                {
                    outlookApp = (Outlook.ApplicationClass)Marshal.GetActiveObject("Outlook.Application");
                }
                catch (COMException ex)
                {
                    outlookApp = new Outlook.ApplicationClass();
                }
    
                cbPriority.SelectedIndex = 1;
            }


            private void createTaskItem()
            {
                Outlook.TaskItem olTaskItem = (Outlook.TaskItem)
                                        outlookApp.CreateItem(Outlook.OlItemType.olTaskItem);
    
                olTaskItem.Body = txtBody.Text;
                olTaskItem.Subject = txtSubject.Text;
                olTaskItem.StartDate = dtpStartDate.Value;
                olTaskItem.DueDate = dtpDueDate.Value;
                olTaskItem.Status = Outlook.OlTaskStatus.olTaskNotStarted;
                olTaskItem.SchedulePlusPriority = cbPriority.SelectedItem.ToString();
    
                string eMails = txtRecipients.Text;
                int count = eMails.Split(';').Length - 1;
    
                if (eMails.IndexOf(";") > -1)
                {
                    for (int i = 0; i <= count; i++)
                    {
                        if (i != count)
                        {
                            string eMail = eMails.Substring(0, eMails.IndexOf(";"));
                            olTaskItem.Recipients.Add(eMail);
                        }
                        else
                        {
                            olTaskItem.Recipients.Add(eMails);
                        }
    
                        eMails = eMails.Remove(0, eMails.IndexOf(";") +1);
                    }
                }
                else
                {
                    olTaskItem.Recipients.Add(txtRecipients.Text); //Error when Outlook isn't running in Administrator mode
                }
    
                olTaskItem.Recipients.ResolveAll();
                olTaskItem.Assign();
    
                olTaskItem.Send();
            }

    Now my question: How can I start Outlook in Administrator mode?

    Thanks for all helpfull answers.

    Alexander

    Thursday, April 5, 2012 11:32 AM

Answers

  • You should not start Outlook as admin, your code all should be running asInvoker. See http://support.microsoft.com/default.aspx?scid=kb;EN-US;2265515

    --
    Ken Slovak
    [MVP - Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    Reminder Manager, Extended Reminders, Attachment Options
    http://www.slovaktech.com/products.htm
     
     
    "ALEBO" <=?utf-8?B?QUxFQk8=?=> wrote in message news:3904edb6-a04f-4e08-a831-1bcb0e511746...

    Hello,

    I try to generate a Outlook TaskItem out of a WordTemplate. It is no problem, all fine. But it just works when outlook is already running in Administrator mode.

    When I try to start the code to create a task item in Outlook when Outlook isn't running (outlookApp = new Outlook.ApplicationClass();) then I get the error:HRESULT: 0x80004004 (E_ABORT)

    Here is my code:

            private void ThisDocument_Startup(object sender, System.EventArgs e)
            {
    
                try
                {
                    outlookApp = (Outlook.ApplicationClass)Marshal.GetActiveObject("Outlook.Application");
                }
                catch (COMException ex)
                {
                    outlookApp = new Outlook.ApplicationClass();
                }
    
                cbPriority.SelectedIndex = 1;
            }


            private void createTaskItem()
            {
                Outlook.TaskItem olTaskItem = (Outlook.TaskItem)
                                        outlookApp.CreateItem(Outlook.OlItemType.olTaskItem);
    
                olTaskItem.Body = txtBody.Text;
                olTaskItem.Subject = txtSubject.Text;
                olTaskItem.StartDate = dtpStartDate.Value;
                olTaskItem.DueDate = dtpDueDate.Value;
                olTaskItem.Status = Outlook.OlTaskStatus.olTaskNotStarted;
                olTaskItem.SchedulePlusPriority = cbPriority.SelectedItem.ToString();
    
                string eMails = txtRecipients.Text;
                int count = eMails.Split(';').Length - 1;
    
                if (eMails.IndexOf(";") > -1)
                {
                    for (int i = 0; i <= count; i++)
                    {
                        if (i != count)
                        {
                            string eMail = eMails.Substring(0, eMails.IndexOf(";"));
                            olTaskItem.Recipients.Add(eMail);
                        }
                        else
                        {
                            olTaskItem.Recipients.Add(eMails);
                        }
    
                        eMails = eMails.Remove(0, eMails.IndexOf(";") +1);
                    }
                }
                else
                {
                    olTaskItem.Recipients.Add(txtRecipients.Text); //Error when Outlook isn't running in Administrator mode
                }
    
                olTaskItem.Recipients.ResolveAll();
                olTaskItem.Assign();
    
                olTaskItem.Send();
            }

    Now my question: How can I start Outlook in Administrator mode?

    Thanks for all helpfull answers.

    Alexander


    Ken Slovak MVP - Outlook
    Thursday, April 5, 2012 4:28 PM
    Moderator

All replies

  • You should not start Outlook as admin, your code all should be running asInvoker. See http://support.microsoft.com/default.aspx?scid=kb;EN-US;2265515

    --
    Ken Slovak
    [MVP - Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    Reminder Manager, Extended Reminders, Attachment Options
    http://www.slovaktech.com/products.htm
     
     
    "ALEBO" <=?utf-8?B?QUxFQk8=?=> wrote in message news:3904edb6-a04f-4e08-a831-1bcb0e511746...

    Hello,

    I try to generate a Outlook TaskItem out of a WordTemplate. It is no problem, all fine. But it just works when outlook is already running in Administrator mode.

    When I try to start the code to create a task item in Outlook when Outlook isn't running (outlookApp = new Outlook.ApplicationClass();) then I get the error:HRESULT: 0x80004004 (E_ABORT)

    Here is my code:

            private void ThisDocument_Startup(object sender, System.EventArgs e)
            {
    
                try
                {
                    outlookApp = (Outlook.ApplicationClass)Marshal.GetActiveObject("Outlook.Application");
                }
                catch (COMException ex)
                {
                    outlookApp = new Outlook.ApplicationClass();
                }
    
                cbPriority.SelectedIndex = 1;
            }


            private void createTaskItem()
            {
                Outlook.TaskItem olTaskItem = (Outlook.TaskItem)
                                        outlookApp.CreateItem(Outlook.OlItemType.olTaskItem);
    
                olTaskItem.Body = txtBody.Text;
                olTaskItem.Subject = txtSubject.Text;
                olTaskItem.StartDate = dtpStartDate.Value;
                olTaskItem.DueDate = dtpDueDate.Value;
                olTaskItem.Status = Outlook.OlTaskStatus.olTaskNotStarted;
                olTaskItem.SchedulePlusPriority = cbPriority.SelectedItem.ToString();
    
                string eMails = txtRecipients.Text;
                int count = eMails.Split(';').Length - 1;
    
                if (eMails.IndexOf(";") > -1)
                {
                    for (int i = 0; i <= count; i++)
                    {
                        if (i != count)
                        {
                            string eMail = eMails.Substring(0, eMails.IndexOf(";"));
                            olTaskItem.Recipients.Add(eMail);
                        }
                        else
                        {
                            olTaskItem.Recipients.Add(eMails);
                        }
    
                        eMails = eMails.Remove(0, eMails.IndexOf(";") +1);
                    }
                }
                else
                {
                    olTaskItem.Recipients.Add(txtRecipients.Text); //Error when Outlook isn't running in Administrator mode
                }
    
                olTaskItem.Recipients.ResolveAll();
                olTaskItem.Assign();
    
                olTaskItem.Send();
            }

    Now my question: How can I start Outlook in Administrator mode?

    Thanks for all helpfull answers.

    Alexander


    Ken Slovak MVP - Outlook
    Thursday, April 5, 2012 4:28 PM
    Moderator
  • Hi Ken, thanks for your Answer. I can't find how to run outllok with asInvoker in your link and in google. I just find a link with a manifest to start my own application with asInvoker. Can you give me a code example oder anonther link with a good description?

    Tuesday, April 10, 2012 7:17 AM
  • when manipulating outlook (office in general) app you must make sure that both your app and office one as in the same security context, so verify if your app is run as an admin or as a normal user. Of course latter scenario is preferred. You will not be able to communicate between different security levels, this is by design.
    Tuesday, April 10, 2012 9:34 AM
  • There is no code for that, the security context is invoked as part of the manifest file for the application or addin. This is how you set the security context in a manifest file:
     

    <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">

    <security>

    <requestedPrivileges>

    <requestedExecutionLevel

    level="asInvoker"

    uiAccess="false" />

    </requestedPrivileges>

    </security>

    </trustInfo>


    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "ALEBO" <=?utf-8?B?QUxFQk8=?=> wrote in message news:070160ca-266e-48c5-9e4d-3ab6ceed121b...

    Hi Ken, thanks for your Answer. I can't find how to run outllok with asInvoker in your link and in google. I just find a link with a manifest to start my own application with asInvoker. Can you give me a code example oder anonther link with a good description?


    Ken Slovak MVP - Outlook
    Tuesday, April 10, 2012 7:02 PM
    Moderator