Coonect to exchange server and copy items from calendar in public folders

rsturner8

Member
Joined
Feb 23, 2011
Messages
6
Programming Experience
1-3
Hi, I`m not sure if I`m posting this in the correct place, but as my code is going to be in VB.Net, I thought I would post it here. Below is my code:


Imports Microsoft.Office.Interop.Outlook
VB.NET:
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Partial[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#000000] [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#000000] Default2[/COLOR]
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Inherits[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Web.UI.Page
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Protected[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Page_Load([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Load
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] olkApp [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Microsoft.Office.Interop.Outlook.Application
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] olkCalendar [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Microsoft.Office.Interop.Outlook.MAPIFolder
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] olkSession [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Microsoft.Office.Interop.Outlook.NameSpace
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] oMapiRecipient [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Recipient
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] oMapiFolder [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Folder
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] addrEntry [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] AddressEntry
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] manager [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ExchangeUser
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] olApp [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Application
olApp = CreateObject([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Outlook.Application"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]' Logon. Doesn't hurt if you are already running and logged on...
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] olNs [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Microsoft.Office.Interop.Outlook.NameSpace
olNs = olApp.GetNamespace([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"MAPI"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])
olNs.Logon()
[/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]'olkApp = GetObject("Outlook.Application")
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]olkSession = olApp.Session
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] oEntries [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] AddressEntries = olkSession.AddressLists [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]'("Contacts")
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]' Dim oEntries As AddressEntries = olNs
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] oEntry [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] AddressEntry
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] oEntry [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] oEntries
[/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]' Get the first user.
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] oEntry.Name = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"richardt_gdt"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]addrEntry = oEntry
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]' addrEntry = olkSession.CurrentUser.AddressEntry
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (addrEntry.Type = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"EX"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]) [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]manager = addrEntry
[/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]' manager = olkSession.CurrentUser.AddressEntry.GetExchangeUser().GetExchangeUserManager()
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]' manager = olkSession.CurrentUser.AddressEntry.GetExchangeUser()
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]oMapiRecipient = olkSession.CreateRecipient(manager.Name)
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (oMapiRecipient.Resolve()) [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]olkCalendar = olkSession.GetSharedDefaultFolder(oMapiRecipient, OlDefaultFolders.olFolderCalendar)
olkCalendar.Display()
[/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]'If Not manager Is Nothing Then
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]' 'Replace MyFolder on the next line with the name of the team calendar folder'
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]' oMapiRecipient = olkSession.CreateRecipient(manager.Name)
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]' If (oMapiRecipient.Resolve()) Then
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]' olkCalendar = olkSession.GetSharedDefaultFolder(oMapiRecipient, OlDefaultFolders.olFolderCalendar)
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]' olkCalendar.Display()
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]' End If
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]'End If
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]olkCalendar = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Nothing
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]olkSession = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Nothing
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]olkApp = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Nothing
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE]
[SIZE=2][SIZE=2][COLOR=#0000ff]Sub
End [/COLOR][/SIZE]
[SIZE=2][COLOR=#000000] [/COLOR][/SIZE][SIZE=2]Class
[COLOR=#000000][/COLOR]


I`ve managed to get this far with some help, but am now getting an error message which I do not understand. The error message is:

"Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Outlook.AddressEntries'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{0006304A-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE))."

The line of code it falls over on is:

"Dim oEntries As AddressEntries = olkSession.AddressLists '("Contacts")
 "


Can anyone give me any help as to why this may be happening please.


Thanks
[/SIZE]
 
Considerations for server-side Automation of Office
Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.
 
Back
Top