A Microsoft Outlook email forum. Outlook Banter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » Outlook Banter forum » Microsoft Outlook Email Newsgroups » Outlook and VBA
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

Save attachment with filter by subject



 
 
Thread Tools Search this Thread Display Modes
  #1  
Old August 3rd 06, 04:25 AM posted to microsoft.public.outlook.program_vba
[email protected]
external usenet poster
 
Posts: 1
Default Save attachment with filter by subject

Hi all -

I am attempting to filter email inbox for incoming message with WOR
anywhere in the subject line.
If find WOR anywhere on the subject line, save the attachment to
specified location, remove the attachment, place text in body of e-mail
with message where file was saved to, move msg to .pst folder.

My code below cobbled together with a post I found at Outlookcode.com
and a post here.
No errors are raised (error handler) But the code does not give any
attachments to the specified folder

Can anyone point out what I did wrong?
Thanks
-goss

Sub SaveAttachment()
'Code via Outlookcode.com
'Filter bit via Dave Quaid Google Groups
'http://tinyurl.com/grv2y


'Declaration
Dim myItems, myItem, myAttachments, myAttachment, cdoFolder As
Object
Dim myOrt As String
Dim myOlApp As New Outlook.Application
Dim myOlExp As Outlook.Explorer
Dim myOlSel As Outlook.Selection
Dim myOlItems As Outlook.Items

'Destination folder
'Change the destination as needed
myOrt = "C:\Data\Reports\WORS"

On Error Resume Next

'work on selected items
cdoFolder = cdoMapiSession.GetDefaultFolder(cdoFolderID)
Set myOlExp = myOlApp.ActiveExplorer
Set myOlSel = myOlExp.Selection
Set myOlItems = cdoFolder.messages.Filter.Subject

'for all items do...
For Each myItem In myOlSel
If InStr(myOlItems, "WOR") Then

'point on attachments
Set myAttachments = myItem.Attachments

'if there are some...
If myAttachments.Count 0 Then

'add remark to message text
myItem.Body = myItem.Body & vbCrLf & _
"Removed Attachments:" & vbCrLf

'for all attachments do...
For i = 1 To myAttachments.Count

'save them to destination
myAttachments(i).SaveAsFile myOrt & _
myAttachments(i).DisplayName

'add name and destination to message text
myItem.Body = myItem.Body & _
"File: " & myOrt & _
myAttachments(i).DisplayName & vbCrLf

Next i

'for all attachments do...
While myAttachments.Count 0

'remove it (use this method in Outlook XP)
'myAttachments.Remove 1

'remove it (use this method in Outlook 2000)
myAttachments(1).Delete

Wend

'save item without attachments
myItem.Save
End If
End If
Next

'free variables
Set myItems = Nothing
Set myItem = Nothing
Set myAttachments = Nothing
Set myAttachment = Nothing
Set myOlApp = Nothing
Set myOlExp = Nothing
Set myOlSel = Nothing

End Sub

Ads
 




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
SaveAsFile - save as subject line instead of DisplayName iamjbunni Outlook and VBA 1 April 30th 06 09:54 AM
Rule to filter e-mails with a specific text string in an attachment [email protected] Outlook - General Queries 2 April 19th 06 10:38 AM
How can I save an attachment that is an email to my inbox? matt69 Outlook and VBA 5 April 14th 06 07:19 AM
How to filter email with blank subject, to, and message body ken4az Outlook - Installation 0 January 20th 06 06:27 PM
My calendar will not save events after typing in subject. scojen Outlook - Calandaring 0 January 16th 06 07:59 PM


All times are GMT +1. The time now is 08:43 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.Search Engine Friendly URLs by vBSEO 2.4.0
Copyright ©2004-2025 Outlook Banter.
The comments are property of their posters.