none
i have a mapi retreive sent mail question if anyone can help RRS feed

  • Question

  • I'm trying to retrieve the count of items in my sent mail folder. I've tried two different methods but only one is retrieving the correct number. The VBA route is correct but using interop isn't working correctly. Was wondering if anyone has any idea. The code I'm using is below.

    Here is the VBA from within outlook itself:

    Sub CountSentMail()

        Dim objOutlook As Object, objnSpace As Object, objFolder As MAPIFolder

        Dim EmailCount As Integer

       

        Set objOutlook = CreateObject("Outlook.Application")

        Set objnSpace = objOutlook.GetNamespace("MAPI")

            On Error Resume Next

            Set objFolder = objnSpace.GetDefaultFolder(olFolderSentMail)

            If Err.Number <> 0 Then

            Err.Clear

            MsgBox "No such folder."

            Exit Sub

            End If

        EmailCount = objFolder.Items.Count

        MsgBox "Number of emails in the folder: " & EmailCount, , "email count"

        Set objFolder = Nothing

        Set objnSpace = Nothing

        Set objOutlook = Nothing

    End Sub

     

    The count returned is 901 (IPM.NOTE) messages

     

     

    Here is the VB.net code

    Imports Microsoft.Office.Interop.Outlook

    Public Class frmFBUMailboxReceiptReport

    Private Sub frmFBUMailboxReceiptReport_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim Outlook As New Microsoft.Office.Interop.Outlook.Application

        Dim NS As Microsoft.Office.Interop.Outlook.NameSpace = Outlook.GetNamespace("MAPI")

        Dim Sentbox As Microsoft.Office.Interop.Outlook.MAPIFolder = Nothing

        Sentbox = NS.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderSentMail)

        MsgBox(Sentbox.Items.Count)

     

    The Count returned is 888 (IPM.NOTE) messages

    Tuesday, October 31, 2017 5:11 PM

All replies

  • Is this running on the same machine? Are you sure you end up using the same profile?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Tuesday, October 31, 2017 5:38 PM
  • yes, both running on the same machine and same profile.
    Tuesday, October 31, 2017 5:50 PM
  • Hi Juan Stevens,

    I try to test your both code example on my machine.

    I got the same result with both code.

    you can see the result below.

    Result with VBA:

    Result with VB.Net:

    I made this test with Outlook 2016. which version you are using?

    I suggest you to again make a test and post your result like I posted above.

    it will help us to understand the issue in a better way.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 1, 2017 3:20 AM
    Moderator
  • Hi Juan,

    What is the status of this thread?

    As my test, I also get the same value with different code.

    To check whether it is related with your environment or account, I would suggest you make a test with new account, and sent some emails, then make a test with both code, will it return the different results?

    If it did, I suggest you output the email one by one to check which email missed in VB.NET Code.

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 8, 2017 7:47 AM