none
Help on the Outlook VBA code RRS feed

  • Question

  • Hi, 

    I am creating the following code for exporting all the IMAP folder items to Excel as well as to folder using Visual Studio 2010 and Office 2010 via citrix)

    I am getting a Error after exporting 246 Mails, Says that "Your Administrator has limited the No of sessions...)

    Please Help me to sort this out...

    Imports Microsoft.Office.Interop.Excel
    Imports Microsoft.Office.Interop.Outlook
    Imports Microsoft.Office.Interop
    Imports System.IO
    Imports System.Threading
    Imports System.Runtime.InteropServices


    Module Module1

        <MTAThread()> Sub Main()
            Dim Folder As Outlook.MAPIFolder
            Dim FirstRw As Integer, SecondRw As Integer
            Dim objOutlook As Outlook._Application
            objOutlook = New Outlook.Application()
            Dim ObjExcel As Excel.Application
            ObjExcel = New Excel.Application()
            ObjExcel.Visible = False
            Dim objNS As Outlook._NameSpace
            objNS = objOutlook.GetNamespace("MAPI")
            Dim Wbook As Excel.Workbook
            Dim NewSheet As Excel.Worksheet
            Dim Wsheet As Excel.Worksheet
            Dim oItem As Outlook.MailItem
            Dim oInspector As Outlook.Inspector
            Dim TodayDate As String
            Dim Strname As String
            Dim Odate As Date
            Dim TotalItems As Integer
            TodayDate = Format(System.DateTime.Now.ToString("MM-dd-yyyy"))
            Wbook = ObjExcel.Workbooks.Open("H:\Outlook Exported\Outlook Mail Details.xlsx")
            NewSheet = ObjExcel.Worksheets.Add(After:=ObjExcel.Worksheets(ObjExcel.Worksheets.Count))
            NewSheet.Name = TodayDate & " Inbox"
            Wsheet = Wbook.Sheets(TodayDate & " Inbox")
            Wsheet.Activate()
            Wsheet.Cells(1, 1) = "Sender"
            Wsheet.Cells(1, 2) = "Subject"
            Wsheet.Cells(1, 3) = "Date"
            Wsheet.Cells(1, 4) = "Body"


            TotalItems = objNS.GetDefaultFolder(OlDefaultFolders.olFolderInbox).Items.Count


            For FirstRw = 1 To TotalItems


                SecondRw = FirstRw + 1

                oItem = objNS.GetDefaultFolder(OlDefaultFolders.olFolderInbox).Items(FirstRw)
                Odate = oItem.ReceivedTime
                oInspector = objOutlook.ActiveInspector
                Wsheet.Cells(SecondRw, 1).Select()
                Wsheet.Cells(SecondRw, 1) = oItem.SenderName
                Wsheet.Cells(SecondRw, 2) = oItem.Subject
                Wsheet.Cells(SecondRw, 3) = Format(Odate, "MMddyyyy") & Format(Odate, "hhmmss")
                Wsheet.Cells(SecondRw, 4) = oItem.Body


                Strname = Format(Odate, "MMddyyyy") & Format(Odate, "hhmmss")

                oItem.SaveAs("H:\Outlook Exported\" & Strname & ".msg", OlSaveAsType.olMSG)


                oItem.Close(1)
                On Error GoTo errorHandler

            Next

    ErrorHandler:

            MsgBox(Err.Description & " (" & Err.Number & ")")


            Resume Next

            MsgBox("Outlook Mails Extracted to Excel")

            Wbook.Close(SaveChanges:=True)

        End Sub





    End Module

    • Moved by Tina-Shi Tuesday, May 19, 2015 6:15 AM the issue is related to the VBA
    Monday, May 18, 2015 4:50 PM

All replies

  • Hi Joe,

    Thank you for posting in MSND forum.

    Since this issue is related to the VBA, so we will move this case to this Excel for Developer forum, you will get better support.

    Best Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, May 19, 2015 6:14 AM
  • Seems like a limitation of Citrix.  I'm not sure you have any control over this via VBA.  Maybe someone will jump in with a more concrete, or better, perspective. 

    Citrix is always hard to deal with when it comes to these kinds of things.


    Knowledge is the only thing that I can give you, and still retain, and we are both better off for it.

    Tuesday, May 19, 2015 4:26 PM