stormtrooperofdeath
Technical User
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
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