Solved

Save Attachments of Outlook Message files  Getting Zero byte files

Posted on 2003-11-04
5
899 Views
Last Modified: 2010-03-05
All,

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?

Thanks...

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
    Next
End If
End With
Set msg = Nothing
sFile = Dir
Loop
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.Open
    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
0
Comment
Question by:Rog
  • 3
  • 2
5 Comments
 
LVL 24

Expert Comment

by:David Wilhoit
Comment Utility
does the account you're running it under have send as/receive as perms on the store? I assume this is E2K

D
0
 
LVL 8

Author Comment

by:Rog
Comment Utility
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.

Roger
0
 
LVL 8

Author Comment

by:Rog
Comment Utility
Updated Points, really need answer....


Thanks,

Rog
0
 
LVL 24

Expert Comment

by:David Wilhoit
Comment Utility
Out of my league then. My bad, should have looked closer at your script....

D

0
 
LVL 8

Accepted Solution

by:
Rog earned 0 total points
Comment Utility
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
        Next
    sFile = Dir()
loop
0

Featured Post

Why spend so long doing email signature updates?

Do you spend loads of your time carrying out email signature updates? Not very interesting are they? Don’t let signature updates get you down. Let Exclaimer Cloud - Signatures for Office 365 make managing email signatures a breeze.

Join & Write a Comment

Disabling the Directory Sync Service Account in Office 365 will stop directory synchronization from working.
Exchange server is not supported in any cloud-hosted platform (other than Azure with Azure Premium Storage).
In this video we show how to create a Shared Mailbox in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Recipients >> Sha…
In this Micro Video tutorial you will learn the basics about Database Availability Groups and How to configure one using a live Exchange Server Environment. The video tutorial explains the basics of the Exchange server Database Availability grou…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now