Outlook vba cannot save a large Msg file to disk. error -2147024882 There is not enough free memory

I'll bet there is no easy solution to this problem, but I thought I would try anyhow.

I cannot do a Saveas of a particular email message.

 The email's total size is only 6 MB, but the email has an odd structure which contains 3 levels:

Level 1 is the email with subject "look at this"

Level 2 is an attachment named "Master.msg". it  contains 100 sub attachments named  "Att1.msg to "Att100.msg"

level 3 are the attachments.  It is possible that some of them have Level 4 sub attachments, but I don't think so.

The failing code is very simple:
Sub test()
   Dim Item As Outlook.mailItem
   Set Item = ActiveExplorer.Selection(1)
   On Error Resume Next
   Item.SaveAs "c:\aaatmp\test451.uni", olMsg
   Debug.Print InputBox("", , Err & " " & Error)

   End Sub

Open in new window

The error message is
"-2147024882 There is not enough free memory to run this program. Quit one or more programs, and then try again.

This SaveAs fails in both Outlook 2003 and Outlook 2010.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David LeeCommented:
Hi, rberke.

Can you save the item manually?
rberkeConsultantAuthor Commented:
Yes I can save it manually, and I already used that technique to manually workaround the problem caused by this particular email.

This is not a high priority. Myprogram has been running for over two years and this is the first time the .SaveAs failed.

Nonetheless, I posted this question in hopes of a long term solution because the error might happen on future emails. I already implemented a workaround which slaps a big MSGBOX  onto my screen saying "Bob  fix the problem manually!".

But, that workaround is ugly, so I had hoped someone might have an alternative way of saving the message. I might try using Redemption when I get some spare time, but I prefer to not use that tool.
Are you trying to save the individual attachments or the entire (selected) item?
Price Your IT Services for Profit

Managed service contracts are great - when they're making you money. Yes, you’re getting paid monthly, but is it actually profitable? Learn to calculate your hourly overhead burden so you can master your IT services pricing strategy.

David LeeCommented:
How big is the message?

Would there be a size other than the 6MB already stated?
Or are you referring to the body text size?
David LeeCommented:

Doh!  No, I missed that he'd given the message size in his original post.  Thanks for pointing that out.


I'm not aware of any size limit on saving messages via VBA.  Just to make though I just ran your code on a 6mb message I have and it saved without a problem.  Do you have another message of similar size?  If so, can you save it via the macro?

I have a hypothesis but no way to test it.  Since he has attachments that contain attachments, the .SaveAs method might be doing some (internal) recursive calls and running out of stack space.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
David LeeCommented:

I don't know of any way to test that theory either.  I found this conversation on a Microsoft forum where a person ran into the same problem.  Pay attention to the responses from Dmitry Streblechenko, the creator of Outlook Redemption and Outlook Spy,
Dmitry recommended trying a unicode format save: olmsgunicode
One other possible reason might be the number of application objects that are started to handle the diverse attachment file types.  Not sure there is a solution, other than the add-in suggested by Dmitry.
David LeeCommented:
I'm not convinced that it's an inherent flaw in Outlook.  Dmitry notes that he tested by saving a message with 312 attachments and didn't encounter the same issue.  He also notes that if you can save the message from the GUI, then you ought to be able to save it via VBA.  I'd be interested in seeing if the message will save on other systems or not.
What if the Outlook GUI does some specific iteration and does not use the SaveAs method.
David LeeCommented:
That's certainly possible.  I'd be surprised though if the developers wrote two different routines to perform the same function.
rberkeConsultantAuthor Commented:
New informantion:

saveas olmsgUnicode gets the same error
BlueDevilFan asked about sizes.  the answers are posted above.
We have now received these 3 emails that have level 3 attachments:
6 MB Email #1 had 100 level 3 attachments and gets erro
r -2147024882 when processed by the the 8 line program in my first post.
2 mb Email #2 had 250 level 3 attachments and gets error -2147024882
1 mb Email #3 had 34 level 3 attachments and worked fine.

the platform does not seem to matter. I have tested all 3 messages with Outlook 2010 under Windows 7, and with Outlook 2003 on Windows small business server 2003. the problem is totally repeatable on both platforms.

aikimark said:
<<internal recursion might be running out of stack space>>
<<What if the Outlook GUI does some specific iteration and does not use the SaveAs method.>>

aikimark appears to be right on both counts.

Manual save works.
Redemption works.
Vba saveAs does not work.

aimark also posted a useful link.

Dmitry's thread discussed several problems that were very similar to mine, and eventually reached the conclusion that Redemption worked. and native vba failed.

I also tried workaround #2 that almost worked, but not quite.
1. Save level 2 attachment (which was named 'master2013.msg') to a disk file named OpenAttachmentManually.txt.
2. Delete original .msg atachment
3. Reattach OpenAttachmentManually.txt to level 1 Mailitem.
4. SaveAS MailItem

My hope was that changing the extension from .msg to .xxx would force Outlook to treat the attachment as a large text file.

Unfortunately, Outlook is too smart for me. At step 3, OUtlook examined the content and noticed that the OpenAttachmentManually.txt was really an email.  Outlook therefore imported "OpenAttachmentManually.txt" and changed the name back to 'Master2013.msg'. (I have noticed this kind of behaviour many times in the past, so it did not surprise me.)

So, I seem to be out of luck.  I could switch to Redemption, or I could just live with my first workaround which is to slap a Msgbox onto the screen warning me of the problem.


P.S. I thought of a workaround #3 but it is too hard and ugly to implement.

1. Save level 2 attachment (which was named 'master2013.msg') to \\server\share\broken attachments\Master2013.msg
2. Delete original .msg atachment
3. Create file named "seeAttachments.html" which contained hyperlinks to the \\server\share file.
3. Reattach seeAttachments.html to level 1 mailitem.

In case anybody is interested, here is my workaround #2 that failed
Sub test128()
   Dim Item As Outlook.mailItem
   Set Item = ActiveExplorer.Selection(1)
   Dim varAtt, filename
   filename = "c:\aaatmp\OpenAttachmentManually.xxx"
   For Each varAtt In Item.Attachments
        On Error Resume Next
        Kill filename
        On Error GoTo 0
        varAtt.SaveAsFile filename         ' <====this works !!!============== save attachment to temporary folder
        Item.Attachments.Add (filename)    ' <====this almost works, but attachment name reverts to Master2013.msg    Next
    Item.SaveAs "c:\aaatmp\final.msg"
   End Sub

Open in new window

you might try file extensions other than txt
rberkeConsultantAuthor Commented:
both .txt, .html, .xxx and .bin  fail.  I don't think any others are likely to work. I also added an it.save at the bottom of the attach/reattach loop, but that did not help.
if .bin fails, then we can abandon that work-around possibility.
rberkeConsultantAuthor Commented:
I give up.  Points go to aikimark for having good insight into a difficult problem.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Office

From novice to tech pro — start learning today.