We help IT Professionals succeed at work.

Open Outlook item

japhethdleung
on
3,260 Views
Last Modified: 2008-08-11
I am trying to open an msg file in C# and wanted to get the type of Outlook Item.
I have tried using the following code but i am getting an error when i'm leaving the 2nd parameter of the .CreateItemFromTemplate() method.


Microsoft.Office.Interop.Outlook.Application o = new Microsoft.Office.Interop.Outlook.Application();
           MailItem x = (MailItem)o.CreateItemFromTemplate("C:\\SharePoint.msg");

Open in new window

Comment
Watch Question

Commented:
Use "Type.Missing" for the second parameter:
Microsoft.Office.Interop.Outlook.Application o = new Microsoft.Office.Interop.Outlook.Application();
           MailItem x = (MailItem)o.CreateItemFromTemplate("C:\\SharePoint.msg", Type.Missing);

Open in new window

Author

Commented:
Still not working...

Commented:
What kind of error do you receive? Please post more details.

Author

Commented:
I want to determine the kind of Outlook item it is...  like (TASK,NOTE,CALENDAR ... etc)


thanks

Commented:
Use the IS operator to compare its type before casting to the specific item type.

You cast directly to MailItem but the underlying Object could also be a TaskItem or a ContactItem etc.

Try the following code:
            String itemMessage = "Item is unknown.";
 
            Microsoft.Office.Interop.Outlook.Application o = new Microsoft.Office.Interop.Outlook.Application();
            object myItem = o.CreateItemFromTemplate("C:\\SharePoint.msg", Type.Missing);
            
            if (myItem is MailItem)
            {
                MailItem mailItem =
                    (myItem as MailItem);
                itemMessage = "The item is an e-mail message." +
                    " The subject is " + mailItem.Subject + ".";
                mailItem.Display(false);
            }
            else if (myItem is ContactItem)
            {
                ContactItem contactItem =
                    (myItem as ContactItem);
                itemMessage = "The item is a contact." +
                    " The full name is " + contactItem.Subject + ".";
                contactItem.Display(false);
            }
            else if (myItem is AppointmentItem)
            {
                AppointmentItem apptItem =
                    (myItem as AppointmentItem);
                itemMessage = "The item is an appointment." +
                    " The subject is " + apptItem.Subject + ".";
            }
            else if (myItem is TaskItem)
            {
                TaskItem taskItem =
                    (myItem as TaskItem);
                itemMessage = "The item is a task. The body is "
                    + taskItem.Body + ".";
            }
            else if (myItem is MeetingItem)
            {
                MeetingItem meetingItem =
                    (myItem as MeetingItem);
                itemMessage = "The item is a meeting item. " +
                     "The subject is " + meetingItem.Subject + ".";
            }
 
            MessageBox.Show(itemMessage);
        }

Open in new window

Author

Commented:
I already try this one,this will not work if there are NO Outlook install in the machine.

Is there any other way to determine the Outlook Item without the relying in the Outlook object.

Like  looking to the BINARY VALUE or Signature

Thanks

Commented:
Why would you want to know the type of Outlook Item without having Outlook to work with them?

Wouldn't it be rather suited to work with another format type then? Could you explain the background of your task to me?

Commented:
Take a look at this information to get more insight in the internal structure of .msg files:
http://www.fileformat.info/format/outlookmsg/

Author

Commented:
We are building an application that retrieves all MSG files from a drive and identifies what type of Outlook Items they are. We tried using Interop but it requires Outlook installed on the computer.

Author

Commented:
001A means Message Class. It can contain one of the following:
      IPM.Note            <- a regular e-mail
      IPM.Contact            <- a contact (name, address, phone etc.)
      IPM.Post            <- a post-it note
      IPM.Activity            <- a calendar event
      IPM.Task            <- a task
      
      or special cases like:
      IPM.Note.Rules.OofTemplate.Microsoft
      IPM.TaskRequest.Accept

This article determine only the MESSAGE CLASS but dont have a way to identiy the item
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Commented:
Simplest way to identify them if you keep them in a drive would be the filename - that one you implicitly got, since you know which file you have open.
If you need additional information you can take it by the flags above.

Best regards

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.