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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Outlook Save Attachment from Sender & Subject VBA 1

Status
Not open for further replies.

stormtrooperofdeath

Technical User
Jul 31, 2003
13
GB
Hi

I am very new to VB and have tried to write some VBA code for Outlook. Im slowly getting there through the online help and solutions on the web but I have hit some stumbling blocks on the way.

In Outlook, when a message is received, I want to it to trigger a script to be run from the rules wizard. If the subject matches my criteria, I want the attachment to be saved to the local HDD and the message moved to another Outlook folder. Ideally, I would like to be able to set the sender name as criteria as well, so that the message needs to have the correct subject from the correct sender to be true in order to be processed.

So what’s the problem? A run time error 13 – type mismatch.

This is my code:

Sub Detach_ASMS(item As Outlook.MailItem) ' Specifies that this is a script to be run from the rules wizard

Dim myOlApp
Dim myNameSpace
Dim j
Dim myItem
Dim myFolder
Dim MyAttachments
Dim myInboxFolder
Dim myDestFolder
Dim fname(200)
Dim fcount

Set myOlApp = CreateObject("Outlook.Application")
Set myNameSpace = myOlApp.GetNamespace("MAPI")
Set myFolder = myNameSpace.Folders("Mailbox - Luger, Lex")
Set myInboxFolder = myNameSpace.GetDefaultFolder(olFolderInbox)
Set myDestFolder = myFolder.Folders("ASMS History")

For Each item In myInboxFolder.Items
Set myItem = item
If myItem.Subject Like "*ASMS*" Then
Set MyAttachments = myItem.Attachments
fcount = MyAttachments.Count
For j = 1 To MyAttachments.Count
MyAttachments.item(j).SaveAsFile "C:\ASMS\" & MyAttachments.item(j).DisplayName
fname(j) = MyAttachments.item(j)
Next
myItem.Move myDestFolder
End If
Next item <<<--------------------------------------------- This is where the type mismatch error occurs!!!

Set myOlApp = Nothing
End Sub

I don’t really know what to do. I thought the program didn’t like ‘item’ being declared as a mail item so I tried to declare a different name but no luck……

This could be really really simple, but as a newbie it's making my head implode!!!!

Also, I tried to add the following to use sendername as criteria too:

If myItem.Find("[SenderName] = 'Luger, Lex'") Then

but at runtime i received this error:

runtime error 438: object doesnt support this property or method.

Please, if anyone could help me in any way with this, I would really appreciate it!!!

Thanks
 
The error occurs on the third line from the bottom:

Next item

It complains about the variable 'item'.

Thanks!
 
If VBScript, replace this:
Next item
By this:
Next

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Thanks for your help so far.

Unfortunately, when I made your suggested change, I still get the type mismatch error at the same line (even though the script does what it is meant to do, the error still pops up).

Any ideas?

Thanks!
 
Perhaps item is a reserved word.
Try something like this:
For Each myItem In myInboxFolder.Items
...
Next 'myItem

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
dont know if this is any help , but this is a bit of code to save the attachments in a folder i have called others then delete the message

Code:
Dim olOutlook As Outlook.Application
Dim ns As Outlook.NameSpace
Dim fld As Outlook.MAPIFolder
Dim itm As Object
Dim MyMail As Outlook.MailItem
dim intX as integer
'-----------

Application.ScreenUpdating = False

intx = 1

Set olOutlook = CreateObject("Outlook.Application")
Set ns = olOutlook.GetNamespace("MAPI")
Set itm = ns.GetDefaultFolder(olFolderInbox)
Set itm = itm.Folders("other")

For Each MyMail In itm.Items
    MyMail.Attachments.item(1).SaveAsFile "H:\CCTemp" & intX & " .xml"
intx = intx + 1 
Next

For Each MyMail In itm.Items
    MyMail.Delete
Next


Set olOutlook = Nothing
Set ns = Nothing
Set itms = Nothing
Set MyMail = Nothing

Filmmaker, gentleman and i have a new site 3/4 working

 
Thanks for your help guys

I had some other non related issues at work that i had to take care of, so this was pushed down the priority list

I thought item may have been a reserved word, so I did as PHV suggested, I also took the line Set myItem = item before the loop and it works fine now!

Chance, I took your code, when I get more time on this I may see if yours works better. thanks for the help everyone, I appreciate it!

One last question, I did all the tests on my PC, when I exported the script to another PC (the one that is supposed to the processing) it doesnt work... the only difference being its another outlook account - so i changed the mailbox reference in the script and made all the correct folders.. I dont know why its playing hardball with me... I noticed on my PC too that sometimes the script will disappear? I have it saved as both a module and in thisoutlooksession and occassionally the code disappears? I have Outlook XP and all 3 service packs... any ideas anyone?

Cheers once again for all your help!
 
it doesnt work...
Can you elaborate ?

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
It just doesnt run. At all. I have a rule set to run a script (this script) when an email arrives from a specified sender, yet when a message does arrive from this sender, the rule isnt fired. When i try to run the rule manually, it says it runs, and takes soem time to run as well, yet the messages arent moved and the attachments arent detached like they are supposed to. Nothing happens at all! The only difference being the mailbox name is different.... i dont know why its acting like this?
 
What is the security level for macros ?
Have you tried to set a breakpoint in your code for single step execution and debugging ?

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
This has helped sort out some automation that I was working on - had to make a fair few changes, but the basic principle is the star!!

If at first you don't succeed, try for the answer.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top