Save Attachments of Outlook Message files Getting Zero byte files


I have a bunch of messages that were saved to files in a folder.  I would like to cycle through each message and save the attachments of each message to a folder.  The following code does just that except I get a zero byte file.  

What am I doing wrong?


Private Sub SaveAttachments()
Dim sFile As String
Dim msg As CDO.Message
'Dim oBodyPart As CDO.IBodyPart
Dim sPath As String
Dim sSavePath As String
Dim i As Integer
sPath = "C:\Roger\BundlePackage\"
sSavePath = "C:\Roger\BundlePackage\SavedAttachments\"
sFile = Dir(sPath & "*.msg")
Do While sFile <> ""
Set msg = LoadMessageFromFile(sPath & sFile)
With msg
If .Attachments.Count > 0 Then
    For i = 1 To .Attachments.Count
        Set oBodyPart = msg.Attachments(i)
        oBodyPart.SaveToFile sSavePath & oBodyPart.Filename
End If
End With
Set msg = Nothing
sFile = Dir
End Sub

' Reference to Microsoft ActiveX Data Objects 2.5 Library
' Reference to Microsoft CDO for Windows 2000 Library
Function LoadMessageFromFile(Path As String) As Message
    Dim Stm As New Stream
    Stm.LoadFromFile Path
    Dim iMsg As New CDO.Message
    Dim iDsrc As IDataSource
    Set iDsrc = iMsg
    iDsrc.OpenObject Stm, "_Stream"
    Set LoadMessageFromFile = iMsg
End Function
Rog DSQL Developer / Web Development / Business AnalysisAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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 WilhoitSenior Consultant, ExchangeCommented:
does the account you're running it under have send as/receive as perms on the store? I assume this is E2K

Rog DSQL Developer / Web Development / Business AnalysisAuthor Commented:
Yes.  I am running Office XP on Exchange 5.5.  The messages are in file format located in a folder, so no connection to exchange server will be needed.

Rog DSQL Developer / Web Development / Business AnalysisAuthor Commented:
Updated Points, really need answer....


David WilhoitSenior Consultant, ExchangeCommented:
Out of my league then. My bad, should have looked closer at your script....


Rog DSQL Developer / Web Development / Business AnalysisAuthor Commented:
Here is a solution that will work, but it is not in CDO...

It utilized Outlook objects....

Dim myOlapp As Outlook.Application
    Dim myoMsg As Outlook.MailItem
    Set myOlapp = CreateObject("Outlook.Application")
    Set myoMsg = myOlapp.CreateItem(olMailItem)
    'sPath = "C:\Roger\BundlePackage\"
    sPath = "\\eve\tmpoll\3114\HSIA Data\"
    'sSavePath = "C:\Roger\BundlePackage\SavedAttachments\"
    sSavePath = "\\eve\tmpoll\3114\HSIA Data\SavedAttachments\"
    sFile = Dir(sPath & "*.msg")
    Do While sFile <> ""
        Set myoMsg = myOlapp.CreateItemFromTemplate(sPath & sFile)
        For i = 1 To myoMsg.Attachments.Count
            myoMsg.Attachments.Item(i).SaveAsFile sSavePath & myoMsg.Attachments(i).Filename
    sFile = Dir()

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
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

From novice to tech pro — start learning today.