Link to home
Create AccountLog in
Outlook

Outlook

--

Questions

--

Followers

Top Experts

Avatar of jana
jana🇺🇸

How to code in VB.net to display a list of Calendar items of Outlook 2010
We want to use VB.net and code a process where it reads our Outlook 2010 Calendar items and displays a list of items with the columns or fields: Subject, Location, Start-Time and End-Time.

Please advice

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of Karrtik IyerKarrtik Iyer🇮🇳

You can use the outlook object model using the outlook com interop in vb.net.
Please see a complete article with example from Microsoft .
https://support.microsoft.com/en-us/kb/313801

Avatar of janajana🇺🇸

ASKER

Hi,

Went thru the sample and set VB as specified with no changes in "TODO" area , as recommended in the link (so we can we it work).  Yet we get this error:

User generated image
The code is exact as in the link:
Imports System.Reflection

Module Module1

    Sub Main()
       ' Create Outlook application.
        Dim oApp As Outlook.Application = New Outlook.Application()

        ' Get NameSpace and Logon.
        Dim oNS As Outlook.NameSpace = oApp.GetNamespace("mapi")
        oNS.Logon("YourValidProfile", Missing.Value, False, True) ' TODO:

        ' Get Appointments collection from the Calendar folder.
        Dim oCalendar As Outlook.MAPIFolder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar)
        Dim oItems As Outlook.Items = oCalendar.Items

        ' TODO: You may want to use Find or Restrict to retrieve the appointment that you prefer.
        ' ...

        ' Get the first AppointmentItem.
        Dim oAppt As Outlook.AppointmentItem = oItems.GetFirst()

        ' Display some common properties.
        Console.WriteLine(oAppt.Organizer)
        Console.WriteLine(oAppt.Subject)
        Console.WriteLine(oAppt.Body)
        Console.WriteLine(oAppt.Location)
        Console.WriteLine(oAppt.Start.ToString())
        Console.WriteLine(oAppt.End.ToString())

        ' Display.
        'oAppt.Display(true)

        ' Log off.
        oNS.Logoff()

        ' Clean up.
        oApp = Nothing
        oNS = Nothing
        oItems = Nothing
        oAppt = Nothing
    End Sub

End Module

Open in new window


Avatar of Karrtik IyerKarrtik Iyer🇮🇳

Can you check while debugging if the below item (oItems) collection has some items in it? Only then get first shall return a non null value in code below. OAppt could be null since there may not be any appointments in your default folders calendar.

Dim oAppt As Outlook.AppointmentItem = oItems.GetFirst()

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of janajana🇺🇸

ASKER

You are correct! We didn't have any appointments in Default Folders. We use Calendar from iCloud (see below);  How can we set THAT Calendar in the code?

User generated image

Avatar of Karrtik IyerKarrtik Iyer🇮🇳

We will have to check the below API to find out how we can open a specific folder.
Dim oCalendar As Outlook.MAPIFolder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar)

Avatar of janajana🇺🇸

ASKER

(Yup!, that's what we are googling)

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of janajana🇺🇸

ASKER

We have been searching google for modifying the line:

Dim oCalendar As Outlook.MAPIFolder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar)

Open in new window


so it can open a specific folder we need.  Unfortunately to no avail.

Please help on opening in VB specific Calendar folder.

(the above line is from the coe we are working with below)
Imports System.Reflection

Module Module1

    Sub Main()
       ' Create Outlook application.
        Dim oApp As Outlook.Application = New Outlook.Application()

        ' Get NameSpace and Logon.
        Dim oNS As Outlook.NameSpace = oApp.GetNamespace("mapi")
        oNS.Logon("YourValidProfile", Missing.Value, False, True) ' TODO:

        ' Get Appointments collection from the Calendar folder.
        Dim oCalendar As Outlook.MAPIFolder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar)
        Dim oItems As Outlook.Items = oCalendar.Items

        ' TODO: You may want to use Find or Restrict to retrieve the appointment that you prefer.
        ' ...

        ' Get the first AppointmentItem.
        Dim oAppt As Outlook.AppointmentItem = oItems.GetFirst()

        ' Display some common properties.
        Console.WriteLine(oAppt.Organizer)
        Console.WriteLine(oAppt.Subject)
        Console.WriteLine(oAppt.Body)
        Console.WriteLine(oAppt.Location)
        Console.WriteLine(oAppt.Start.ToString())
        Console.WriteLine(oAppt.End.ToString())

        ' Display.
        'oAppt.Display(true)

        ' Log off.
        oNS.Logoff()

        ' Clean up.
        oApp = Nothing
        oNS = Nothing
        oItems = Nothing
        oAppt = Nothing
    End Sub

End Module

Open in new window


SOLUTION
Avatar of Karrtik IyerKarrtik Iyer🇮🇳

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of janajana🇺🇸

ASKER

Tried it but it gives errors (see pix below):

User generated image
Here the code:
Sub Main()
        'Create Outlook application.
        Dim oApp As Outlook.Application = New Outlook.Application()

        'Get NameSpace and Logon.
        Dim oNS As Outlook.NameSpace = oApp.GetNamespace("mapi")
        oNS.Logon("RT.pst", Missing.Value, False, True)
        Dim oCalendar As Outlook.MAPIFolder = oNS.GetDefaultFolder(Outlook.Folder)

        Dim oItems As Outlook.Items = oCalendar.Items
        Dim folders As Outlook.Folders = oApp.Session.Folders

        For Each folder As Outlook.Folder In folders
            'Replace the calendar name with your calendar name 
            Dim objModule As Outlook.CalendarModule = folder.GetExplorer().NavigationPane.Modules.GetNavigationModule(Outlook.OlNavigationModuleType.olModuleCalendar)
            Dim objGroup As Outlook.NavigationGroup = objModule.NavigationGroups.GetDefaultNavigationGroup(Outlook.OlGroupType.olMyFoldersGroup)
            For Each navfolder As Outlook.NavigationFolder In objGroup.NavigationFolders
                Console.WriteLine(navfolder.Folder.Name)
                Debug.WriteLine(navfolder.DisplayName)
                'TODO:Replace the calendar name with your calendar name 
                If navfolder.Folder.Name = "Calendar" Then
                    oItems = navfolder.Folder.Items
                    Exit For
                End If
            Next
        Next

        'Display.
        'oAppt.Display(true)

        'Log off.
        oNS.Logoff()

        'Clean up.
        oApp = Nothing
        oNS = Nothing
        oItems = Nothing
        oAppt = Nothing
    End Sub

Open in new window


Where are going wrong?

Avatar of Karrtik IyerKarrtik Iyer🇮🇳

I am assuming your using visual studio and VB.NET (not writing VBA or macro code in outlook)
Also please give your calendar name appropriately, by default all calendars have name as 'Calendar' so try to give your iCloud calendar a different name and try to use the same in the code below.
1> First error: Expression is of type Outlook.Folders, which is not a collection type.
Please add reference to Microsoft.Office.InterOp.Outlook assembly to your project.
Right click your project in solution explorer and click on add reference and select the above mentioned assembly
And import following namespace at the top.
Imports Microsoft.Office.Interop

Open in new window

2> Second error: oAppt is not declared
Remove oAppt = nothing at the last line, not required since not used

Open in new window

3> Please use modified code below.
Imports System.Reflection
Imports Microsoft.Office.Interop
Sub Main()
           'Create Outlook application.
        Dim oApp As Outlook.Application = New Outlook.Application()

        'Get NameSpace and Logon.
        Dim oNS As Outlook.NameSpace = oApp.GetNamespace("mapi")
        oNS.Logon("RT.pst", Missing.Value, False, True)

        Dim oItems As Outlook.Items
        Dim myfolders As Outlook.Folders = oApp.Session.Folders

        For Each myfolder As Outlook.Folder In myfolders
            Dim calendarfound As Boolean = False
            'Replace the calendar name with your calendar name 
            Dim objModule As Outlook.CalendarModule = myfolder.GetExplorer().NavigationPane.Modules.GetNavigationModule(Outlook.OlNavigationModuleType.olModuleCalendar)
            Dim objGroup As Outlook.NavigationGroup = objModule.NavigationGroups.GetDefaultNavigationGroup(Outlook.OlGroupType.olMyFoldersGroup)
            For Each navfolder As Outlook.NavigationFolder In objGroup.NavigationFolders
                Console.WriteLine(navfolder.Folder.Name)
                Debug.WriteLine(navfolder.DisplayName)
                'TODO:Replace the calendar name with your calendar name 
                If navfolder.Folder.Name = "Calendar" Then
                    oItems = navfolder.Folder.Items
                    calendarfound = True
                    Exit For
                End If
            Next
            If calendarfound Then Exit For
        Next

        'Display.
        'oAppt.Display(true)

        'Log off.
        oNS.Logoff()

        'Clean up.
        oApp = Nothing
        oNS = Nothing
        oItems = Nothing

    End Sub

Open in new window


Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of janajana🇺🇸

ASKER

Yes, we are using VB.net 2012.

the Microsoft.Office.InterOp.Outlook not in "add reference" instructed (see pix below)
User generated image
"...give your iCloud calendar a different name and try to use the same in the code below"
Based on the pix below of our Calendars, what name should we use?
User generated image

Avatar of Karrtik IyerKarrtik Iyer🇮🇳

1> Not in COM , go to Assemblies at the top.
2> For correct calendar name, just iterate through once to see what all calendar names are getting printed and then based on that we can find out which one is yours.

Avatar of janajana🇺🇸

ASKER

Ok got it!  Added reference.

For Calendar name, we thought that code-line 23 'If navfolder.Folder.Name = "Calendar" Then' reflects the actual name.

Leave as it is?

Ran the code again; please advice on the messages:

User generated image

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of Karrtik IyerKarrtik Iyer🇮🇳

Can you please add one more reference, Microsoft.office.Tools.Outlook from assemblies?
Leave that if condition as it is, and first check all the calendar names getting printed in the debug output window and based on that change the if condition later on.

Avatar of janajana🇺🇸

ASKER

ok will do

Avatar of janajana🇺🇸

ASKER

Same "blue" line as in the pix before.
(and we added the reference instructed)

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of Karrtik IyerKarrtik Iyer🇮🇳

Give me few minutes I shall try to upload a VB.NET 2012 project with sample code for you.

Avatar of janajana🇺🇸

ASKER

Thanx

ASKER CERTIFIED SOLUTION
Avatar of Karrtik IyerKarrtik Iyer🇮🇳

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.

Avatar of janajana🇺🇸

ASKER

Hi,
(sorry couldn't continue last night.  Even though desperately to finish this the computer acted up no internet)

Ok, we downloaded your sample and open the 'OutLookCalendarTest2012.sln' solution and added the 'Microsoft.Office.Interop.Outlook'.  Yet when we go back to Right-Click Project >> Add Reference, the reference is not marked (like it was not selected previously).  However, we also marked 'Microsoft.office.Tools.Outlook' as instructed in 'ID: 41391132' and when we go back to Right-Click Project >> Add Reference, THAT reference is reference.

The curious thing is our project has  'Microsoft.Office.Interop.Outlook' reference.

Neverthess, see your sample loaded; still the blue underline:
User generated image
There has to be something we are missing (sorry that we are not too tech in VB)

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of Karrtik IyerKarrtik Iyer🇮🇳

For the project that I had uploaded, I had already added the reference, the screenshot was to let you know in case you wanted to do it for the project that you were trying out.
But anyway, I wonder how in first comment, you were able to compile and open the default calendar because that also used the same outlook API's and DLL being used. Has anything changed when since then when you were able to log on and open the default calendar which did not have any appointments?

Avatar of Karrtik IyerKarrtik Iyer🇮🇳

Can you check in your visual studio installation folder whether you have following folder?
The assemblies that we are referring should be inside this folder under Office 12 or Office 14 or Office 15 folder.
The below is an example, in my case it is installed in F drive, please check in your computer based on where you have installed visual studio.
F:\Program Files (x86)\Microsoft Visual Studio 12.0\Visual Studio Tools for Office\PIA\

Avatar of Karrtik IyerKarrtik Iyer🇮🇳

If you do not have the PIA assemblies as mentioned in comment above, then please install (assuming you are using Office 2010) the PIA assemblies by downloading it from the below link.
PIA assemblies for Office 2010

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of janajana🇺🇸

ASKER

Has anything changed when since then when you were able to log on and open the default calendar which did not have any appointments?

NO, we are using your sample.

Can you check in your visual studio installation folder whether you have following folder?

YES (see pix below).
User generated image
If you do not have the PIA assemblies as mentioned in comment above, then please install

Though we have them, we will download your link just incase. (Note: we had MS2010)

Avatar of Karrtik IyerKarrtik Iyer🇮🇳

If you have that folder,  no need to download it again, when you say add reference in the project, say browse and go to that folder (office 14) and select the Microsoft.Office.Interop.Outlook.dll to add reference to this DLL.
Then please perform a compile and post if you get any compilation errors.

Avatar of janajana🇺🇸

ASKER

ok yes, that sounds logical; will do!

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of janajana🇺🇸

ASKER

Super strange!

Ok here is the result.

Adding by browsing.  After saving the solution and re-opening, it's not marked.
User generated image
if we go to the Assemblies Extension, it not marked also.
User generated image

SOLUTION
Avatar of QlemoQlemo🇩🇪

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.

Avatar of janajana🇺🇸

ASKER

Yes Thanx!!! Didn't quite got that and you are right, it's just clicking the suggestion and VB applies the request.

Avatar of janajana🇺🇸

ASKER

Didn't work... it changes to another request until non of the suggestions work.

Did your code worked at your computer?

Did your code open a specific calendar?

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of QlemoQlemo🇩🇪

I'm programming with PowerShell and that doesn't require such strict typing and importing ;-).

Avatar of Karrtik IyerKarrtik Iyer🇮🇳

What's the latest update, can you post your modified code and errors you are getting?

Avatar of janajana🇺🇸

ASKER

Hi,

The updated modified code is found  in ID: 41391159 (you can download from there).

The Error are found in ID: 41391583.

Thanx! (Hope u can find what we haven't)

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of Karrtik IyerKarrtik Iyer🇮🇳

can you please download the PIA assemblies, and then give it a try? Please close all your visual studio instances and then install, open the visual studio with your project and now refer the outlook interop DLL from the place (directory ) you installed this PIA assembly. And then give your project a build/compile, do not worry about blue lines, just do a compile and post the errors.

PIA assemblies

Avatar of janajana🇺🇸

ASKER

Ok will try

Avatar of janajana🇺🇸

ASKER

Sorry for the delay.

Just downloaded but read the install instruction and makes no sense:
User generated image
Should just run the download file "PIARedist.exe"?  or what exaclty do we do with the install instructions MS gav

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of janajana🇺🇸

ASKER

Ok got it.

Installing!

Avatar of QlemoQlemo🇩🇪

The points are mutual exclusive. Download, execute, then execute the generated MSI file.

Avatar of janajana🇺🇸

ASKER

Nope. Didn't work.

Installed it, the installation just showed a windows (see below) and disappear.
User generated image
went into "add reference", went to folder "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Visual Studio Tools for Office\PIA\Office14"
User generated image
no luck.

Can you send a code or zipped file with the project? We must doing something wrong

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of QlemoQlemo🇩🇪

I'm out...

Avatar of janajana🇺🇸

ASKER

Yeah that's how we feel.  We really think something may be wrong with our environment.

Ok thanx!

SOLUTION
Avatar of Karrtik IyerKarrtik Iyer🇮🇳

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.

Avatar of janajana🇺🇸

ASKER

Good advice, thanx!

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of janajana🇺🇸

ASKER

Did as advised and closed the question.  We will be getting another users PC with similar configuration and try it there.
Outlook

Outlook

--

Questions

--

Followers

Top Experts

Microsoft Outlook is a personal information manager from Microsoft, available as a part of the Microsoft Office suite. Although often used mainly as an email application, it also includes a calendar, task manager, contact manager, note-taker, journal, and web browser.