Solved

Save Attachments of Outlook Message files  Getting Zero byte files

Posted on 2003-11-04
5
927 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 24

Expert Comment

by:David Wilhoit
ID: 9683485
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
ID: 9684308
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
ID: 9687340
Updated Points, really need answer....


Thanks,

Rog
0
 
LVL 24

Expert Comment

by:David Wilhoit
ID: 9688423
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
ID: 9695178
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

MS Outlook is a world-class email client application that is mainly used for e-communication globally.  In this article, we will discuss the basic idea about MS Outlook, its advanced features, and types of MS Outlook File formats.
A couple of months ago we ran into an issue that necessitated re-creating our Edge Subscriptions. However, when we attempted to execute the command: New-EdgeSubscription -filename C:\NewEdgeSub_01.xml we received an error indicating that the LDAP se…
In this video we show how to create an email address policy 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 Mail Flow…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Suggested Courses
Course of the Month11 days, 18 hours left to enroll

623 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