Saving Attachments in Outlook via. VBA

C

Curtis C

I have some code to save attachments in my inbox, and it works OK,
except that the code loops inside the "Date Today" subgroup within the
inbox. If I do a "myFolder.Items.Count" I receive the correct number of
items within the entire inbox however, using the
"myFolder.Items.GetNext" loops within the "Date Today" group. My code
is:
___________________________________________________________
Dim objOL As Outlook.Application
Dim objMsg As Outlook.MailItem 'Object
Dim objAttachments As Outlook.Attachments
Dim objSelection As Outlook.Selection
Dim i As Long
Dim lngCount As Long
Dim strFile As String
Dim strFolderpath As String
Dim strDeletedFiles As String
Dim mail_count As Integer

strFolderpath = CreateObject("WScript.Shell").SpecialFolders(16)

On Error Resume Next
Set objOL = CreateObject("Outlook.Application")
Set myNms = objOL.GetNamespace("MAPI")
Set myFolder = myNms.GetDefaultFolder(olFolderInbox)
Set myExplorer = objOL.ActiveExplorer
Set myExplorer.CurrentFolder = myFolder

Set objMsg = myFolder.Items.GetFirst

mail_count = myFolder.Items.Count
Do While mail_count > 0
Set objSelection = objOL.ActiveExplorer.objMsg
strFolderpath = "C:\mail_attachments\"

For Each objMsg In objSelection

Set objAttachments = objMsg.Attachments
lngCount = objAttachments.Count

If lngCount > 0 Then
For i = lngCount To 1 Step -1
strFile = objAttachments.Item(i).FileName
strFile = strFolderpath & strFile
objAttachments.Item(i).SaveAsFile strFile
Next i

objMsg.Save

End If
Next
mail_count = mail_count - 1
Set objMsg = myFolder.Items.GetNext
Loop

ExitSub:

Set objAttachments = Nothing
Set objMsg = Nothing
Set objSelection = Nothing
Set objOL = Nothing
End Sub
___________________________________________________________
Any help would be greatly appreciated.

*** Sent via Developersdex http://www.developersdex.com ***
 
H

Hollis D. Paul

I have some code to save attachments in my inbox, and it works OK,
except that the code loops inside the "Date Today" subgroup within the
inbox. If I do a "myFolder.Items.Count" I receive the correct number of
items within the entire inbox however, using the
"myFolder.Items.GetNext" loops within the "Date Today" group. My code
is:
_______
I personally am not into doing this kind of code, so I have no ideas of
my own about the code. I do know that Eric Legault has an addin wizard
that does this and more. I couldn't find his site URL, so I asked in a
private list and got this information in return. But my initial thought
is that you should quit wasting your time and get Eric's Wizard.

Eric Legault (MVP - Outlook, MCDBA, old school WOSA MCSD, B.A.)
Blog: http://blogs.officezealot.com/legault
Try Picture Attachments Wizard for Outlook!
http://www.collaborativeinnovations.ca


I also got an analysis of your code from Ken Slovak, another Outlook MVP
who does a lot of Outlook coding, and addins, for his clients. To whit,
and remember he is speaking to me, a colleague and not a client:

**********************

Hollis,

That code is all wrong. You can't set selection in code and I have no
idea why he's trying to anyway. He can just loop the Items collection of
Inbox using a loop or GetFirst followed by GetNext until there are no
more items to evaluate. As he gets each item he can evaluate it for
attachments and then if he's removing them he can use a down counting
loop (For 1 = Attachments.Count To 1 Step -1) to get at the attachments
and remove them.

As it is his code will never work.

Ken
*********************

In short, go visit Eric's site.


Hollis D. Paul [MVP - Outlook]
(e-mail address removed)
Mukilteo, WA USA
 

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