Save Outlook message to disk

Add-in Express™ Support Service
That's what is more important than anything else

Save Outlook message to disk
 
william lui




Posts: 3
Joined: 2006-06-20
Hello there. I need some help with saving an outlook email to the hard disk in .msg format. I have created a button add-in, but im having problems with the code. I need to save the selected email to disk. Im using the code below.

Does anyone know what im missing?

Thanks, Will


private void adxCommandBarButton1_Click(object sender)
        {
            object selectedItem = null;
            Outlook.MailItem mail = null;

            try
            {
                mail = selectedItem as Outlook.MailItem;
                mail.SaveAs("c:/test/mail.msg"); //not sure about type param
            } 
            catch (Exception ex){
                
            }
        }   
    }
Posted 20 Jun, 2006 10:11:17 Top
Brad Smith




Posts: 49
Joined: 2005-11-22
I assume you have some other code in there? If selectedItem is null and you're not setting it to something else, then you're calling null.SaveAs(), which will throw an exception.

Try "c:\\test\\mail.msg" or @"c:\test\mail.msg". DOS/Windows wants backslashes in paths.

Instead of merely omitting the Type parameter, try specifying olMSG or at least vtMissing. Mind you, I'd expect a compile error if that was really a problem.

Brad.
Posted 20 Jun, 2006 10:22:05 Top
william lui




Posts: 3
Joined: 2006-06-20
Thanks for your reply Brad. I added the correct slashes, checks for null, and olMSG to the type parameter.


        private void adxCommandBarButton1_Click(object sender) {
            object selectedItem = null;
            Outlook.MailItem mail = null;

            try {
                mail = selectedItem as Outlook.MailItem;
                if (mail != null) {
                    mail.SaveAs("c:\test\mail.msg", olMSG);
                    MessageBox.Show("done");
                }
            }

            catch{}
        }   
      }


I get the error "The name 'olMSG' does not exist in the current context" when i compile. Ive never written an office add-in before. Id appreciate any help.

Thanks, Will
Posted 20 Jun, 2006 11:11:32 Top
Brad Smith




Posts: 49
Joined: 2005-11-22
Yeah, sometimes it can be a bit tricky looking these up. In this case, you're looking for Outlook.OlSaveAsType.olMSG.

Here's a general tip if you know part of the overall tag. In VS.NET, bring up the Object Browser (View|Object Browser). In their, you can then do a Search for "olMSG", and it'll pop up the whole tag.

Having said that, look at your posted code carefully. You've initialized a local variable called selectedItem to null. In your try you're now casting that as a MailItem:
mail = selectedItem as Outlook.MailItem;

So that means that mail will *always* be null, meaning your SaveAs call will never execute.

I'm assuming this button is on your Explorer CommandBar, so you want to deal with the selected item in the Explorer. Is that about right? In that case you need to deal with the Selection collection. So you'd need something like:


Outlook.Explorer currExplorer = HostApplication.ActiveExplorer();
if(null != currExplorer)
{
  Outlook.Selection selections = currExplorer.Selection;
  if(null != selections && selections.Count > 0)
  {
    for(int i = 1; i <= selections.Count; ++i)
    {
      object selectedItem = (object)selections[i];
      if(null != selectedItem )
      {
        if(selectedItem is Outlook.MailItem)
        {
          (selectedItem as Outlook._MailItem).SaveAs(@"c:	estmail.msg",
            Outlook.OlSaveAsType.olMSG);
        }
      }
      Marshal.ReleaseComObject(selectedItem);
    }
  }
}


Now, obviously the above code is somewhat restricted, since the Selection collection may have multiple selections. You can choose to loop through each, or only do your SaveAs when selections.Count == 1.
If you *do* keep the loop, then obviously you won't want to use the same file name each time.
Also, never assume your selection is always a MailItem. You definitely need to test each time lest you get an exception thrown.

Hope this points you in the right direction. There are almost certainly other ways to do what you want, but this should work.

Brad.
Posted 20 Jun, 2006 17:32:35 Top
william lui




Posts: 3
Joined: 2006-06-20
Thank you very much Brad:D, thats exactly what I needed. I dont think I would have figured it out myself.

Thanks for your advice, Will
Posted 21 Jun, 2006 04:37:24 Top