Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <STAThread()> _
- Sub Main()
- AutomateOutlook()
- ' Clean up the unmanaged Outlook COM resources by forcing a garbage
- ' collection as soon as the calling function is off the stack (at
- ' which point these objects are no longer rooted).
- GC.Collect()
- GC.WaitForPendingFinalizers()
- ' GC needs to be called twice in order to get the Finalizers called -
- ' the first time in, it simply makes a list of what is to be finalized,
- ' the second time in, it actually is finalizing. Only then will the
- ' object do its automatic ReleaseComObject.
- GC.Collect()
- GC.WaitForPendingFinalizers()
- End Sub
- Sub AutomateOutlook()
- Dim missing As Object = Type.Missing
- Dim oOutlook As Outlook.Application = Nothing
- Dim oNS As Outlook.NameSpace = Nothing
- Dim oCtFolder As Outlook.MAPIFolder = Nothing
- Dim oCts As Outlook.Items = Nothing
- Dim oMail As Outlook.MailItem = Nothing
- Dim oCal As Outlook.CalendarModule = Nothing
- ' $folder = $namespace.getDefaultFolder($olFolders::olFolderCalendar)
- Dim folder = Nothing
- Dim CalendarSharing = Nothing
- Try
- ' Start Microsoft Outlook and log on with your profile.
- ' Create an Outlook application.
- oOutlook = New Outlook.Application()
- Console.WriteLine("Outlook.Application is started")
- ' Get the namespace
- oNS = oOutlook.GetNamespace("MAPI")
- ' Log on by using a dialog box to choose the profile.
- oNS.Logon(missing, missing, True, True)
- folder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar)
- CalendarSharing = folder.GetCalendarExporter()
- CalendarSharing.CalendarDetail = Outlook.olCalendarDetail.olFreeBusyOnly
- CalendarSharing.startDate = Date.Now
- CalendarSharing.endDate = DateAdd(DateInterval.Day, 7, Date.Now)
- CalendarSharing.RestrictToWorkingHours = True
- CalendarSharing.IncludeAttachments = False
- CalendarSharing.IncludePrivateDetails = False
- oMail = CalendarSharing.ForwardAsICal(Outlook.OlCalendarMailFormat.olCalendarMailFormatDailySchedule)
- oMail.Recipients.Add("me@com.com")
- oMail.Send()
- ' Alternative logon method that uses a specific profile.
- ' If you use this logon method, change the profile name to an
- ' appropriate value. The second parameter of Logon is the password
- ' (if any) associated with the profile. This parameter exists only
- ' for backwards compatibility and for security reasons, and it is
- ' not recommended for use.
- 'oNS.Logon("YourValidProfile", missing, False, True)
- Console.WriteLine("Press ENTER to continue when Outlook is ready.")
- Console.ReadLine()
- ' User logs off and quits Outlook.
- Console.WriteLine("Log off and quit the Outlook application")
- oNS.Logoff()
- oOutlook.Quit()
- Catch ex As Exception
- Console.WriteLine("AutomateOutlook throws the error: {0}", ex.Message)
- Finally
- ' Manually clean up the explicit unmanaged Outlook COM resources by
- ' calling Marshal.FinalReleaseComObject on all accessor objects.
- ' See http://support.microsoft.com/kb/317109.
- If Not oMail Is Nothing Then
- Marshal.FinalReleaseComObject(oMail)
- oMail = Nothing
- End If
- If Not oCts Is Nothing Then
- Marshal.FinalReleaseComObject(oCts)
- oCts = Nothing
- End If
- If Not oCtFolder Is Nothing Then
- Marshal.FinalReleaseComObject(oCtFolder)
- oCtFolder = Nothing
- End If
- If Not oNS Is Nothing Then
- Marshal.FinalReleaseComObject(oNS)
- oNS = Nothing
- End If
- If Not oOutlook Is Nothing Then
- Marshal.FinalReleaseComObject(oOutlook)
- oOutlook = Nothing
- End If
- End Try
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement