1

When I pass olMHTML it saves it with .msg extension, but if I change the extension to .mht it works as an mht file perfectly and if I don't I have trouble opening the file with .msg extension. If I save it as olMSGUnicode, it saves with no extension but adding the .msg extension makes it work perfectly as a .msg file.

What is the correct way to save as .mht as olMHTML seems to be .msg format although it seems wrong because as I said I have trouble opening the msg file but works fine when renamed to .mht, and olMSGUnicode while doesn't save with an extension by itself, opens file as an .msg file.

To be clear, why is olMHTML saving as msg instead of mht? olMSGUnicode works fine for msg files although they save with no extension adding .msg makes them work perfect. As does changing olMHTML to .mht from .msg.

EDIT: By doing a File.Move() after saving I can rename it to .mht and it works fine, but it would be much more convenient to just save int the correct format.

EDIT2: Code by commenter's request:

        if (!filename.EndsWith(".mht"))
        {
            filename = filename + ".mht";
        }
        try
        {
            message.SaveAs(path + filename, Microsoft.Office.Interop.Outlook.OlSaveAsType.olMHTML);
            MessageBox.Show(String.Format("Success saving file {0} at {1}", filename, path), "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            string newfilename = filename.Substring(0, filename.Length - 4) + ".mht";
            File.Move(path + filename, path + newfilename);
        }
        catch (System.Exception ex)
        {
            MessageBox.Show(String.Format("Error {0} while trying to save {1} at {2}", ex, filename, path), "Error",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

I realized I hadn't updated the if statement to not be .msg, it is working now. But still doesn't explain why when using olMHTML as save type, I get a file with no extension rather than with an .mht extension and I have to specify it while with other SaveAs types I do not. But that is a question for another day. I figure the answer out.

ss7
  • 2,902
  • 7
  • 41
  • 90
  • read the documentation https://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.olsaveastype.aspx perhaps you will understand why it's working with .mht – MethodMan May 02 '15 at 17:07
  • Still doesn't explain why olMHTML saves as msg – ss7 May 02 '15 at 17:09
  • does your internet work in regards to google ? anyway look at this resolved answer https://social.msdn.microsoft.com/forums/office/en-US/d27f9085-c067-4e63-ba18-87e0eb1e8a10/saveas-mht-format – MethodMan May 02 '15 at 17:16
  • I've seen that answer. That is not the question. olMHTML is mht format but saves as msg. It works if renamed. Msg already has two formats olMSG and olMSGUnicode which work fine. Why would olMHTML save as msg rather than mht like as is documented – ss7 May 02 '15 at 17:19
  • if it's microsoft.. then it's an error or bug.. Microsoft is know for that.. have you contacted microsoft support and put in a bug ticket.. – MethodMan May 02 '15 at 17:22
  • I am now. Also asking VSTO forums. Does seem to be a bug. – ss7 May 02 '15 at 17:29

2 Answers2

2

The OlSaveAsType enumeration page in MSDN provides the table of values and corresponding file extensions:

olTXT Text format (.txt)

olRTF Rich Text format (.rtf)

olTemplate Microsoft Outlook template (.oft)

olMSG Outlook message format (.msg)

olDoc Microsoft Office Word format (.doc)

olHTML HTML format (.html)

olVCard VCard format (.vcf)

olVCal VCal format (.vcs)

olICal iCal format (.ics)

olMSGUnicode Outlook Unicode message format (.msg)

olMHTML MIME HTML format (.mht)

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • I know, and as I've said, I use olMHTML for .mht format and it saves as .msg. If i specify the format extension in filename I wind up with .mht.msg. I think the docs are wrong for olMHTML – ss7 May 07 '15 at 20:48
2

I am not sure how it is possible to get a wrong extension if you are the one who specifies the file name (which includes the extension) when calling MailItem.SaveAs.

Make sure you specify fully qualified file name that includes the directory, filename, and extension.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • I dont specify the extension. All the SaveAs() types usually have no extension but a few. olMHTML has .msg when it should be .mht. olMSG and olMSGUnicode have no extension but should be .msg and if you rename it works perfect – ss7 May 02 '15 at 22:27
  • 1
    So why don't you specify the extension explicitly? I would argue that it would be a bug if Outlook changed the extension from an empty one (that you specify) to whatever it thinks is appropriate. – Dmitry Streblechenko May 03 '15 at 01:51
  • I do and it saves as .mht.msg oddly only this save as type makes outlook show a progress bar. Other times it just pauses and appears with no extension. For MHTML i see he saving progress bar and it shows the msg extension – ss7 May 03 '15 at 17:52
  • 1
    Can you be more specific? What is the exact file name you pass to SaveAs? Are you passing "mht.msg"? It has the MSG extension, does it not? Or are you saying that if you pass "c:\temp\myfile.mht", you and up with a file named "c:\temp\myfile"? – Dmitry Streblechenko May 03 '15 at 22:05
  • I told you in the quesiton. I pass olMHTML which is documented as .mht When I use the .mht extension in the filename i get a file .mht.msg. So C:\temp\myfile.mht ends up as C:\temp\myfile.mht.msg – ss7 May 07 '15 at 20:47
  • I did and when doing so found out that I still had an if statement adding .msg to the end that I hadn't noticed. It works now. Thanks – ss7 May 07 '15 at 20:57