none
the saveas method or property is not available becasue a document window is not active RRS feed

  • Question

  • Hello geeks,

    I have an outlook 2007 addin developed in c# (VS2008). I have a command that saves the email body as word file. Here is the code:

    var doc=mailitem.GetInspector.WordEditor;
    doc.SaveAs(ref filename);

    This code has been working fine for years. But, suddenly one of the users started receiving an error stating "The saveas method or property is not available becasue a document window is not active.".

    In this case, the MailItem is not open in the window. I just grabbed the mailitem from the list of emails from inbox folder.

    Can anyone please help me finding the cause of this error?

    Thanks in advance.

    Wednesday, December 7, 2016 3:52 PM

Answers

All replies

  • Why not simply call MailItem.SaveAs(..., olDoc)?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, December 7, 2016 4:59 PM
  • Because, it would save the mailitem as .msg format. But I need to have the mail body in word format that is being used in some other places.

    Thanks.

    Wednesday, December 7, 2016 5:55 PM
  • Hello Jahedur,

    What file path did you pass to the SaveAs method of the Document class? Did you try to specify the file format (second argument)?

    Anyway, you may consider using the SaveAs method of the MailItem class if you don't need to use any extra steps except saving the file to the disk or configure that action in depth. The second argument can be one of the following OlSaveAsType constants: olHTML, olMSG, olRTF, olTemplate, olDoc, olTXT, olVCal, olVCard, olICal, or olMSGUnicode. 


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Wednesday, December 7, 2016 5:57 PM
  • It will be MSG format only if you do not specify the type (second argument) or if the format is olMsg or olMsgUnicode.

    Do specify olDoc (=4).


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, December 7, 2016 6:12 PM
  • Actually I did supply the format. Sorry for not mentioning that. Here is the code where I pass the mailitem.GetInspector.WordEditor object:

     public static string GetBase64StringData(Word.Document vDoc)
            {
                object fileName = System.IO.Path.GetTempFileName();
                object missing = Type.Missing;
                object format = Word.WdSaveFormat.wdFormatDocument;
                try
                {
                    vDoc.SaveAs(ref fileName, ref format, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
    
                    return TPA_DotNet_Extension.clsFileOps.GetBase64StringDataFromFile(fileName.ToString());
    
                }
                catch (Exception ex)
                {
                    throw ex.MyException();
                }
                finally
                {
                    try
                    {
                        if (System.IO.File.Exists(fileName.ToString()))
                            System.IO.File.Delete(fileName.ToString());
                    }
                    catch (Exception)
                    {
                    }
                }
            }

    The error occurs at: vDoc.SaveAs(ref fileName, ref format....

    And, I could save the mailitem, but it won't give me the content in word format.

    Wednesday, December 7, 2016 6:31 PM
  • You are missing my point: do *not* call Document.SaveAs. Call MailItem.SaveAs.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, December 7, 2016 7:59 PM
  • Sorry about that Dmitry. I responded to Eugene Astafiev comment.

    And, thank you so much. It works. Now the only problem is, it includes some other information that I don't want. It includes like below:

    From:                                         Greg Snyder

    Sent:                                           Wednesday, December 07, 2016 3:05 PM

    To:                                               Jony Jahedur Rahman

    Subject:                                     RE: Service Agreement Windward Template

    Any option to exclude this? I just need the email body.

    Wednesday, December 7, 2016 9:21 PM
  • The headers is included for the olRtf, olTxt, olHTML formats.

    Read the MailItem.RtfBody property (returns a variant array of byte), then save the returned data to a file explicitlyin your code.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, December 7, 2016 9:28 PM
  • Thanks Dmitry for the suggestion. I have seen earlier, for some large emails, dealing with RTF sometimes gives an out of memory error as the size of this format is significantly high as compared to doc/docx. I think its because it keeps extra copy of all the images due to maintaining some compatibility issues, I don't remember exactly what it was.

    But, I will give it a try to see if converting from RTF to doc/docx will help me out.

    Thank you!


    Wednesday, December 7, 2016 10:03 PM
  • Where did you get an instance of the Document class?

    Anyway, make sure that a correct file name is used in the code (which corresponds to the file extension). For example:

    string filename = System.IO.Path.GetTempFileName().Replace(".tmp", ".doc");

    Does it help?


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Thursday, December 8, 2016 2:42 AM
  • Thanks Eugene. But it doesn't help.
    Friday, December 16, 2016 6:08 PM
  • Hi Jahedur,

    >>In this case, the MailItem is not open in the window. I just grabbed the mailitem from the list of emails from inbox folder.

    If MailItem is open in the window, will you get this error?

    >>doc.SaveAs(ref filename);

    For this code, I made a test under Outlook 2013, I will get error it does not have SaveAs.

    I made a test with below code to save as all mail body from list of inbox, it works, I suggest you make a try.

    Outlook.Application _application = Globals.ThisAddIn.Application;
                string rootFolderPath = _application.Session.DefaultStore.GetRootFolder().FolderPath;
                Outlook.NameSpace outlookNameSpace = _application.GetNamespace("MAPI");
                Outlook.MAPIFolder sentfolder = _application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) as Outlook.MAPIFolder;
                int i = 0;
                foreach (Outlook.MailItem mailItem in sentfolder.Items)
                {
                    if (mailItem != null)
                    {
                        Word.Document doc = mailItem.GetInspector.WordEditor as Word.Document;
                        doc.SaveAs(@"D:\Edward\"+"test"+i.ToString()+".docx");
                        i++;
                    }
                }            

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, December 19, 2016 4:21 AM
  • Thanks Edward. This was the reason. And, sorry for the delayed response.

    Thursday, July 6, 2017 2:30 PM