Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Outlook automation? question 1

Status
Not open for further replies.

SBendBuckeye

Programmer
May 22, 2002
2,166
US
Does outlook expose an incoming mail object that you can process using VBA? I have done quite a bit with Access VBA but almost nothing with Outlook. I would like to do the following:

A. If an incoming message has an attachment, move the message to a specified Outlook folder

B. Save the attachment outside of Outlook in a specified folder

Thanks in advance for any help you can give me!

Have a great day!
 
A. can be done with a mail rule.
B. you will need to create a macro with VBA. In outlook's help search for automate tasks.
 
Thanks.

I created a rule to move selected mail messages with attachments to a subfolder named: AttachmentsFolder

I could not find anything in help under Automate Tasks in Outlook2000.

How do I set a reference to the individual mail messages in
that folder? I tried the following in create macro but am hung up.

Private Sub CopyAttachmentToDisk()
Dim fldr As Object
Dim str As String
str = "AttachmentsFolder"
Set fldr = _
Application.ActiveExplorer.CurrentFolder.Folders(str)
With fldr
'Where I want to do something, I think
End With
End Sub

Thanks again for any help!
 
Try:

Private Sub Application_NewMail()
Dim f As MAPIFolder, ns As NameSpace, mi As MailItem, i As Integer
Set ns = Application.GetNamespace("MAPI")
Set f = ns.GetDefaultFolder(olFolderInbox)
Set mi = f.Items(1)
For i = 1 To mi.Attachments.Count
With mi.Attachments(i)
.SaveAsFile MyFolderName & .FileName
Next i
End Sub

The only thing I'm not sure of is how to know whether items(1) is the most recently arrived item (it is for me), and how to deal with the situation where more than one mail item arrives at the same time. You probably need to work with the mailitem's .unread and .receivedtime properties.
Rob
[flowerface]
 
Thanks, Rob and SMAH. After adding the End With to your code along with a path variable, this is what I did. It worked like a charm the first time.

Where would I go to find things like you were kind enough to post? I spent some time poking around in Outlook help and didn't see anything remotely resembling your code.

Private Sub Application_NewMail()
Dim strPath As String
Dim mf As MAPIFolder
Dim ns As NameSpace
Dim mi As MailItem
Dim i As Integer
strPath = "F:\Windows\Temp\"
Set ns = Application.GetNamespace("MAPI")
Set mf = ns.GetDefaultFolder(olFolderInbox)
Set mi = mf.Items(1)
For i = 1 To mi.Attachments.Count
With mi.Attachments(i)
.SaveAsFile strPath & .FileName
End With
Next i
End Sub

Have a great day!


 
In outlook, hit Alt-F11, then F1 to get Outlook VBA help. It's not as good as Excel's VBA help, I think, but you should be able to pick things up from there.
Rob
[flowerface]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top