Results 1 to 11 of 11
  1. #1
    Lounger
    Join Date
    Sep 2003
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    VBA: My Outlook VBA rule code does't work :( (Office 2003)

    <P ID="edit" class=small>(Edited by HansV on 24-Nov-03 13:16. Removed some superfluous empty lines; inserted [tab] tags to preserve indentation in code.)</P>Hi!!!

    I write some VBA code that doesn't work good.
    what the code SHOULD [img]/forums/images/smilies/wink.gif[/img] 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?

    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 = "GotDotNet_Community@microsoft.com" Then
    Call MoveMail(currentMailItem, currentMAPIFolder.Folders.Item("Forum").Folders.It em("GotDotNet").EntryID)
    'newsalerts-noreply@google.com
    ElseIf currentMailItem.SenderEmailAddress = "newsalerts-noreply@google.com" Then
    Call MoveMail(currentMailItem, currentMAPIFolder.Folders.Item("News").Folders.Ite m("Google.com").EntryID)
    'newsmail@derStandard.at
    ElseIf currentMailItem.SenderEmailAddress = "newsmail@derStandard.at" Then
    Call MoveMail(currentMailItem, currentMAPIFolder.Folders.Item("Newsletter").Folde rs.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(strTarg FldrID)
    currentMailItem.Delete
    FINISH:
    MoveMail = CBool(Err.Number)
    End Function

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: VBA: My Outlook VBA rule code does't work :( (Office 2003)

    I haven't tested your code, but in general, For Each is dangerous if you modify or delete items in the loop. It is better to loop backwards:

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

  3. #3
    Lounger
    Join Date
    Sep 2003
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA: My Outlook VBA rule code does't work :( (Office 2003)

    or is this code better 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)

    'GotDotNet_Community@microsoft.com
    If currentMailItem.SenderEmailAddress = "GotDotNet_Community@microsoft.com" Then
    Call MoveMail(currentMailItem, currentMAPIFolder.Folders.Item("Forum").Folders.It em("GotDotNet").EntryID)
    'newsalerts-noreply@google.com
    ElseIf currentMailItem.SenderEmailAddress = "newsalerts-noreply@google.com" Then
    Call MoveMail(currentMailItem, currentMAPIFolder.Folders.Item("News").Folders.Ite m("Google.com").EntryID)
    'nachricht@mail.pressetext.com
    ElseIf currentMailItem.SenderEmailAddress = "nachricht@mail.pressetext.com" Then
    Call MoveMail(currentMailItem, currentMAPIFolder.Folders.Item("News").Folders.Ite m("Pressetext").EntryID)
    'noreply@tutorialforums.com
    ElseIf currentMailItem.SenderEmailAddress = "noreply@tutorialforums.com" Then
    Call MoveMail(currentMailItem, currentMAPIFolder.Folders.Item("Forum").Folders.It em("Tutorial Forums").EntryID)
    'newsmail@derStandard.at
    ElseIf currentMailItem.SenderEmailAddress = "newsmail@derStandard.at" Then
    Call MoveMail(currentMailItem, currentMAPIFolder.Folders.Item("Newsletter").Folde rs.Item("DerStandard.at").EntryID)
    'system@mail.pressetext.com
    ElseIf currentMailItem.SenderEmailAddress = "system@mail.pressetext.com" Then
    Call MoveMail(currentMailItem, currentMAPIFolder.Folders.Item("News").Folders.Ite m("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(strTarg FldrID)
    FINISH:
    MoveMail = CBool(Err.Number)
    End Function

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: VBA: My Outlook VBA rule code does't work :( (Office 2003)

    Well, have you tried it? Do you still get the error?

    You have removed the line to delete CurrentMailItem, so I would say that this code will only copy items to other folders, not move them. Why not move the item itself, instead of copying it and moving the copy?

  5. #5
    Lounger
    Join Date
    Sep 2003
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA: My Outlook VBA rule code does't work :( (Office 2003)

    Sorry I'm new to VBA. Can you give me a sample code?
    thanks!


    regards,


    gicio

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: VBA: My Outlook VBA rule code does't work :( (Office 2003)

    This is what I would make of it. I can't test it, however, since I don't have Outlook 2003. SenderEmailAddress is not available in Outlook 2002.

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

    On Error GoTo ErrHandler

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

    For intIndex = currentMAPIFolder.Items.Count To 1 Step -1
    Set currentMailItem = currentMAPIFolder.Items(intIndex)
    Select Case currentMailItem.SenderEmailAddress
    Case "GotDotNet_Community@microsoft.com"
    currentMailItem.Move currentMAPIFolder.Folders("Forum").Folders("GotDot Net")
    Case "newsalerts-noreply@google.com"
    currentMailItem.Move currentMAPIFolder.Folders("News").Folders("Google. com")
    Case "nachricht@mail.pressetext.com"
    currentMailItem.Move currentMAPIFolder.Folders("News").Folders("Presset ext")
    Case "noreply@tutorialforums.com"
    currentMailItem.Move currentMAPIFolder.Folders("Forum").Folders("Tutori al Forums")
    Case "newsmail@derStandard.at"
    currentMailItem.Move currentMAPIFolder.Folders("Newsletter").Folders("D erStandard.at")
    Case "system@mail.pressetext.com"
    currentMailItem.Move currentMAPIFolder.Folders("News").Folders("Presset ext.com")
    End Select
    Next intIndex

    ExitHandler:
    Set currentMailItem = Nothing
    Set currentMAPIFolder = Nothing
    Set currentNameSpace = Nothing
    Exit Sub

    ErrHandler:
    MsgBox Err.Description, vbExclamation
    Resume ExitHandler
    End Sub

  7. #7
    Lounger
    Join Date
    Sep 2003
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA: My Outlook VBA rule code does't work :( (Office 2003)

    thanks!!
    1. question:

    are meet request and email tracking notifications also currentMailItem ????


    regards,


    gicio

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: VBA: My Outlook VBA rule code does't work :( (Office 2003)

    You're looping through all items in the Inbox folder; meeting requests and tracking notifications are Inbox items too, so my guess is that they will be processed too.

  9. #9
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: VBA: My Outlook VBA rule code does't work :( (Office 2003)

    If you are getting type mismatches for those, then Outlook is saying that it does not consider them to be a MailItem.

    Even though you get the best Intellihelp (whatever it's called) when you use the more specific type of item, you might need to convert your code to make the currentItem a variable of type Object and then interrogate its type before you process it. I can't look up whether the Type or TypeName or which method lets you do this, but I think it's something like that.

  10. #10
    Lounger
    Join Date
    Sep 2003
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA: My Outlook VBA rule code does't work :( (Office 2003)

    Thx jscher2000 !!!

    can someone give me some sample codes to do that what jscher2000 say?


    regards,


    gicio

  11. #11
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: VBA: My Outlook VBA rule code does't work :( (Office 2003)

    What I proposed would result in less efficient code, so before you go down that road, you ought to diagnose the cause of the problem. When you get the type mismatch, do you have the option to Debug? If so, try that, and find out what kind of item is selected.

    For example, when you get to VBA, press Ctrl+G to open the Immediate window and query the active item:

    ?Activeexplorer.Items(intindex).class

    You should get a number which you can look up in the object browser (not directly, but it shows in the bottom pane when you select a constant in the OlObjectClass enumeration). For example 43 is mail, 53, 54, 55, 56, 57 deal with meeting requests, and so forth.

    You could check the class in your loop before dealing with the message and skip non-mail items, or you could write the code more flexibly to deal with all types of items. But do the diagnosis first.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •