VBA: My Outlook VBA rule code does't work :(

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi!!!

I write some VBA code that doesn't work good.
what the code SHOULD ;) do:

After the send/receive proces the code loop through all messages in the
inbox
and move the messages in the right folders (depend on the sender email
address).

the problem is that after 3 loops I got a :

Run-time error '13': Type mismatch.


can someone tell me why I get this error?






Option Explicit


Private Sub Application_NewMail()
Dim currentNameSpace As NameSpace
Dim currentMAPIFolder As MAPIFolder
Dim currentMailItem As MailItem

Set currentNameSpace = Application.GetNamespace("MAPI")
Set currentMAPIFolder =
currentNameSpace.GetDefaultFolder(olFolderInbox)

For Each currentMailItem In currentMAPIFolder.Items

'GotDotNet_Community@ microsoft.com
If currentMailItem.SenderEmailAddress =
"(e-mail address removed)" Then
Call MoveMail(currentMailItem,
currentMAPIFolder.Folders.Item("Forum").Folders.Item("GotDotNet").EntryID)
'(e-mail address removed)
ElseIf currentMailItem.SenderEmailAddress =
"(e-mail address removed)" Then
Call MoveMail(currentMailItem,
currentMAPIFolder.Folders.Item("News").Folders.Item("Google.com").EntryID)
'(e-mail address removed)
ElseIf currentMailItem.SenderEmailAddress =
"(e-mail address removed)" Then
Call MoveMail(currentMailItem,
currentMAPIFolder.Folders.Item("Newsletter").Folders.Item("DerStandard.at").
EntryID)

Else

End If

Next currentMailItem

Set currentMAPIFolder = Nothing
Set currentNameSpace = Nothing
End Sub


Private Function MoveMail(currentMailItem As MailItem, strTargFldrID As
String) As Boolean
Dim currentNameSpace As NameSpace
Dim currentMoveMailItem As MailItem

Set currentNameSpace = Application.GetNamespace("MAPI")

On Error GoTo FINISH:
Set currentMoveMailItem = currentMailItem.Copy
currentMoveMailItem.Move
Destfldr:=currentNameSpace.GetFolderFromID(strTargFldrID)
currentMailItem.Delete
FINISH:
MoveMail = CBool(Err.Number)
End Function
 
The first thing you should do is set a breakpoint so you can find out what statement returns the error. My guess is that you're sending not just email messages but also meeting requests or other items that are not MailItem objects.
--
Sue Mosher, Outlook MVP
Outlook and Exchange solutions at http://www.slipstick.com
Author of
Microsoft Outlook Programming: Jumpstart
for Administrators, Power Users, and Developers
 
Thx Sue!

you are right! it was a meeting request ;)

how should a VBA code looks like to get no errors also with meeting
requests...tracking infos.....???


regards,


gicio



The first thing you should do is set a breakpoint so you can find out what
statement returns the error. My guess is that you're sending not just email
messages but also meeting requests or other items that are not MailItem
objects.
--
Sue Mosher, Outlook MVP
Outlook and Exchange solutions at http://www.slipstick.com
Author of
Microsoft Outlook Programming: Jumpstart
for Administrators, Power Users, and Developers
 
can you give me some code samples? because I'm new to VBA.


regard,


gicio


Dim your object as Object and use the Class property to determine what type
of item it is before using any item-specific properties or methods.
--
Sue Mosher, Outlook MVP
Outlook and Exchange solutions at http://www.slipstick.com
Author of
Microsoft Outlook Programming: Jumpstart
for Administrators, Power Users, and Developers
 
Dim your object as Object and use the Class property to determine what type of item it is before using any item-specific properties or methods.
--
Sue Mosher, Outlook MVP
Outlook and Exchange solutions at http://www.slipstick.com
Author of
Microsoft Outlook Programming: Jumpstart
for Administrators, Power Users, and Developers
 
Dim objItem as Object

' your code to return objItem

If objItem.Class = olMail Then
' your code to use MailItem properties and methods with objItem
End If
--
Sue Mosher, Outlook MVP
Outlook and Exchange solutions at http://www.slipstick.com
Author of
Microsoft Outlook Programming: Jumpstart
for Administrators, Power Users, and Developers
 
is this ok now ??:

Option Explicit


Private Sub Application_NewMail()
Dim currentNameSpace As NameSpace
Dim currentMAPIFolder As MAPIFolder
Dim currentMailItem As MailItem

Set currentNameSpace = Application.GetNamespace("MAPI")
Set currentMAPIFolder =
currentNameSpace.GetDefaultFolder(olFolderInbox)

Dim intIndex As Integer
For intIndex = currentMAPIFolder.Items.Count To 1 Step -1
Set currentMailItem = currentMAPIFolder.Items(intIndex)

'(e-mail address removed)
If currentMailItem.SenderEmailAddress =
"(e-mail address removed)" Then
Call MoveMail(currentMailItem,
currentMAPIFolder.Folders.Item("Forum").Folders.Item("GotDotNet").EntryID)
'(e-mail address removed)
ElseIf currentMailItem.SenderEmailAddress =
"(e-mail address removed)" Then
Call MoveMail(currentMailItem,
currentMAPIFolder.Folders.Item("News").Folders.Item("Google.com").EntryID)
'(e-mail address removed)
ElseIf currentMailItem.SenderEmailAddress =
"(e-mail address removed)" Then
Call MoveMail(currentMailItem,
currentMAPIFolder.Folders.Item("News").Folders.Item("Pressetext").EntryID)
'(e-mail address removed)
ElseIf currentMailItem.SenderEmailAddress =
"(e-mail address removed)" Then
Call MoveMail(currentMailItem,
currentMAPIFolder.Folders.Item("Forum").Folders.Item("Tutorial
Forums").EntryID)
'(e-mail address removed)
ElseIf currentMailItem.SenderEmailAddress =
"(e-mail address removed)" Then
Call MoveMail(currentMailItem,
currentMAPIFolder.Folders.Item("Newsletter").Folders.Item("DerStandard.at").
EntryID)
'(e-mail address removed)
ElseIf currentMailItem.SenderEmailAddress =
"(e-mail address removed)" Then
Call MoveMail(currentMailItem, currentMAPIFolder.Folders.Ite
m("News").Folders.Item("Pressetext.com").EntryID)

Else

End If

Next intIndex

Set currentMAPIFolder = Nothing
Set currentNameSpace = Nothing
End Sub


Private Function MoveMail(currentMailItem As MailItem, strTargFldrID As
String) As Boolean
Dim currentNameSpace As NameSpace
Dim currentMoveMailItem As MailItem

Set currentNameSpace = Application.GetNamespace("MAPI")

On Error GoTo FINISH:
Set currentMoveMailItem = currentMailItem.Copy
currentMoveMailItem.Move
Destfldr:=currentNameSpace.GetFolderFromID(strTargFldrID)
'currentMailItem.Delete
FINISH:
MoveMail = CBool(Err.Number)
End Function
 
No. You're still declaring currentMailItem as MailItem, even though you have no reason to assume that every item in the Inbox is a mail message.
 
can yo give me some more sample code?

Thanks!!!!!!!!!!!!!!


regards,


gicio




Dim objItem as Object

' your code to return objItem

If objItem.Class = olMail Then
' your code to use MailItem properties and methods with objItem
End If
--
Sue Mosher, Outlook MVP
Outlook and Exchange solutions at http://www.slipstick.com
Author of
Microsoft Outlook Programming: Jumpstart
for Administrators, Power Users, and Developers
 
Back
Top