Results 1 to 2 of 2

Thread: [RESOLVED] ** MailItem in Outlook

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2002
    Posts
    46

    [RESOLVED] ** MailItem in Outlook

    I have the following code that will move all messages from the Inbox to a folder the user selects from the "Select Folder" dialog box:

    Code:
    Sub LAC_MoveMail()
    
    Dim ns As NameSpace
    Dim Inbox As MAPIFolder
    Dim Item As Object
    Dim ca As MailItem
    Dim myNewFolder As MAPIFolder
    
    Set ns = GetNamespace("MAPI")
    Set Inbox = ns.GetDefaultFolder(olFolderInbox)
    
    Set myNewFolder = ns.PickFolder
    Set ca = Inbox.Items.GetFirst
    
    While TypeName(ca) <> "Nothing"
        ca.Move myNewFolder
        Set ca = Inbox.Items.GetNext
    Wend
    
    Set ca = Inbox.Items.GetLast
    ca.Move myNewFolder
    
    End Sub
    Problem? When there is a meeting request in the users inbox, they get a run-time error - Type Mismatch. It's occuring on the
    Code:
    set ca = inbox.items.getnext
    because ca is a MailItem Object. My question? How do I determine what type of objects are in the Inbox?
    Last edited by charlie79; Apr 16th, 2004 at 07:58 AM.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Try this. It will move only MailItems. You can add more to it to
    handel other types.
    VB Code:
    1. Private Sub LAC_MoveMail_Click()
    2.  
    3.     Dim ns As NameSpace
    4.     Dim Inbox As MAPIFolder
    5.     Dim Item As Object
    6.     Dim ca As MailItem
    7.     Dim myNewFolder As MAPIFolder
    8.     Dim i As Integer
    9.    
    10.     MousePointer = vbHourglass
    11.     Set ns = GetNamespace("MAPI")
    12.     Set Inbox = ns.GetDefaultFolder(olFolderInbox)
    13.    
    14. ReSelect:
    15.     Set myNewFolder = ns.PickFolder
    16.     If TypeName(myNewFolder) = "Nothing" Then
    17.         MousePointer = vbNormal
    18.         GoTo No_Bugs_Exit
    19.     End If
    20.     If myNewFolder.DefaultItemType <> olMailItem Then
    21.         If MsgBox("Select Outlook MailItem type only!", vbOKCancel + vbInformation, App.ProductName) = vbOK Then
    22.             GoTo ReSelect
    23.         Else
    24.             GoTo No_Bugs_Exit
    25.         End If
    26.     End If
    27.     For i = Inbox.Items.Count To 1 Step -1
    28.         If Inbox.Items.Item(i).Class = olMail Then
    29.             Set ca = Inbox.Items.Item(i)
    30.             ca.Move myNewFolder
    31.         End If
    32.         Set ca = Nothing
    33.     Next
    34.     MousePointer = vbNormal
    35.  
    36. No_Bugs_Exit:
    37.     Set ns = Nothing
    38.     Set myNewFolder = Nothing
    39.  
    40. End Sub
    Last edited by RobDog888; Apr 15th, 2004 at 11:20 PM.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

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



Click Here to Expand Forum to Full Width