Consulting

Results 1 to 6 of 6

Thread: Solved: CDO Method to delete items programmatically

  1. #1
    Just A Dude VBAX Tutor Scottie P's Avatar
    Joined
    May 2004
    Location
    Remote from 18901 USA
    Posts
    263
    Location

    Solved: CDO Method to delete items programmatically

    Hi All.

    I was just wondering around the internet minding my own business when this code just jumped onto my clip board. Obviously curious, I tried it out - and it works great...for one item per click/run of the code.
    I have no knowledge in Outlook VBA so I would like to know this:

    Is there a way to have this work on a selection of multiple items?
    For instance, I select 5 items in the folder [items that I want to see go away for good] and hit the little 'Wipe 'em out' button and all 5 go...not just one each press of the button/run of the code.

    The code in question:

    [VBA]
    Sub DeleteSelectedItem()
    Dim objApp As Outlook.Application
    Dim objItem As Object
    Dim objCDO As MAPI.Message
    Dim objCDOSession As MAPI.Session
    Dim strEntryID As String
    Dim strStoreID As String

    Set objApp = CreateObject("Outlook.Application")
    Set objItem = objApp.ActiveExplorer.Selection.Item(1)
    If Not objItem Is Nothing Then
    Set objCDOSession = CreateObject("MAPI.Session")
    objCDOSession.Logon "", "", False, False
    strEntryID = objItem.EntryID
    strStoreID = objItem.Parent.StoreID
    Set objCDO = objCDOSession.GetMessage(strEntryID, strStoreID)
    If Not objCDO Is Nothing Then
    objCDO.Delete
    End If
    End If

    objCDOSession.Logoff
    Set objCDOSession = Nothing
    Set objItem = Nothing
    Set objCDO = Nothing
    Set objApp = Nothing
    End Sub
    [/VBA]

    Any takers?

    X
    Last edited by Scottie P; 06-12-2004 at 09:54 AM.
    Life is Visual: Presence is Perception...
    How we see the world is how we respond to it. ~* Peace *~

  2. #2
    BoardCoder
    Licensed Coder
    VBAX Expert mark007's Avatar
    Joined
    May 2004
    Location
    Leeds, UK
    Posts
    622
    Location
    Try changing this:

    Set objItem = objApp.ActiveExplorer.Selection.Item(1)

    to

    for each objItem in objApp.ActiveExplorer.Selection

    then add a 'next' below the end if.

    That should do it to all selected messages.

    "Computers are useless. They can only give you answers." - Pablo Picasso
    Mark Rowlinson FIA | The Code Net

  3. #3
    Just A Dude VBAX Tutor Scottie P's Avatar
    Joined
    May 2004
    Location
    Remote from 18901 USA
    Posts
    263
    Location

    wow - yes!

    exactly, Mark!
    performs wonderfully!

    Regards,

    Scott
    Life is Visual: Presence is Perception...
    How we see the world is how we respond to it. ~* Peace *~

  4. #4
    BoardCoder
    Licensed Coder VBAX Expert mark007's Avatar
    Joined
    May 2004
    Location
    Leeds, UK
    Posts
    622
    Location
    Good, good, good
    "Computers are useless. They can only give you answers." - Pablo Picasso
    Mark Rowlinson FIA | The Code Net

  5. #5
    Knowledge Base Approver VBAX Expert brettdj's Avatar
    Joined
    May 2004
    Location
    Melbourne
    Posts
    649
    Location
    Weird - the code is using early binding dimensioning with late binding

    [vba]
    Dim objApp As Outlook.Application
    Dim objCDO As MAPI.Message
    Dim objCDOSession As MAPI.Session


    Set objApp = CreateObject("Outlook.Application")
    Set objCDOSession = CreateObject("MAPI.Session")
    [/vba]

    Cheers

    Dave

  6. #6
    BoardCoder
    Licensed Coder VBAX Expert mark007's Avatar
    Joined
    May 2004
    Location
    Leeds, UK
    Posts
    622
    Location
    Yes this is odd:

    [vba]
    Set objApp = CreateObject("Outlook.Application")[/vba]

    Could understand the use of getobject but not createobject. Should use:

    [vba]Set objApp = New Outlook.Application[/vba]
    "Computers are useless. They can only give you answers." - Pablo Picasso
    Mark Rowlinson FIA | The Code Net

Posting Permissions

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