Save OL Attachments Programatically

G

Guest

I was browsing through the MS Discussion forums and found an entry regarding
saving attachments from outlook via ACCESS vb code (code snippet from Joe
Fallon below). I have a similar issue where I receive an email daily that
contains an excel spreadsheet. I then have to save that attachement to any
random location, import it into my db, store the raw data, manipulate it,
run several comparisons against it, and then spit it back out into a
formatted spreadsheet that gets distributed to one of several users as a
daily work file.

This all works fine with the current coding that I've written. What I'm not
paricularly thrilled about, though, is having to manualy save that file from
the email attachment before running the code.
I used the code you provided the other user by Joe Fallon (and modified
particular areas to fit my needs) but I'm getting stuck.
I keep getting a break on the line:
Set MyInbox = ns.GetDefaultFolder(olFolderInbox).Items
with the error:
Method 'Items' of object 'MAPIfolder' failed

I've checked Tools,->References and I have an Outlook group checked, but I'm
lost for any other solutions.

If you have any suggestions, they would be greatly appreciated. My bosses
are breathing down my neck about this (they're rather picky and don't want
end-users to do much prep on their own).

Thanks.

The code listed in the forums was:
Public Sub SaveAttachment(strPath As String)
On Error GoTo Err_SaveAttachment

Dim ol As New Outlook.Application
Dim ns As Outlook.NameSpace
Dim MyInbox As Outlook.Items
Dim fldr As Outlook.MAPIFolder
Dim itm As Outlook.MailItem
Dim mFile As String, NumAttachments As Integer, i As Integer, NumEmails As
Integer, strTo As String

Set ns = ol.GetNamespace("MAPI")
Set MyInbox = ns.GetDefaultFolder(olFolderInbox).Items

'set a reference to a folder to move the items to
Set fldr = ns.Folders("Personal Folders").Folders("Saved
Messages").Folders("Bids")

For Each itm In MyInbox
'Debug.Print itm.Subject, itm.To, itm.SenderName
! If itm.Subject Like "*Bid*" Then
NumAttachments = itm.Attachments.Count
i = 1 'attachment number
Do While i <= NumAttachments
mFile = itm.Attachments.Item(i).filename
itm.Attachments.Item(i).SaveAsFile strPath & mFile
i = i + 1
Loop
Else
'Debug.Print "Not a Bid"
End If
Next
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top