Advertisement
Guest User

Untitled

a guest
Oct 1st, 2014
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. <STAThread()> _
  2. Sub Main()
  3.  
  4. AutomateOutlook()
  5.  
  6.  
  7. ' Clean up the unmanaged Outlook COM resources by forcing a garbage
  8. ' collection as soon as the calling function is off the stack (at
  9. ' which point these objects are no longer rooted).
  10.  
  11. GC.Collect()
  12. GC.WaitForPendingFinalizers()
  13. ' GC needs to be called twice in order to get the Finalizers called -
  14. ' the first time in, it simply makes a list of what is to be finalized,
  15. ' the second time in, it actually is finalizing. Only then will the
  16. ' object do its automatic ReleaseComObject.
  17. GC.Collect()
  18. GC.WaitForPendingFinalizers()
  19.  
  20. End Sub
  21.  
  22.  
  23. Sub AutomateOutlook()
  24.  
  25. Dim missing As Object = Type.Missing
  26.  
  27. Dim oOutlook As Outlook.Application = Nothing
  28. Dim oNS As Outlook.NameSpace = Nothing
  29. Dim oCtFolder As Outlook.MAPIFolder = Nothing
  30. Dim oCts As Outlook.Items = Nothing
  31. Dim oMail As Outlook.MailItem = Nothing
  32. Dim oCal As Outlook.CalendarModule = Nothing
  33. ' $folder = $namespace.getDefaultFolder($olFolders::olFolderCalendar)
  34. Dim folder = Nothing
  35. Dim CalendarSharing = Nothing
  36.  
  37. Try
  38. ' Start Microsoft Outlook and log on with your profile.
  39.  
  40. ' Create an Outlook application.
  41. oOutlook = New Outlook.Application()
  42. Console.WriteLine("Outlook.Application is started")
  43.  
  44. ' Get the namespace
  45. oNS = oOutlook.GetNamespace("MAPI")
  46.  
  47. ' Log on by using a dialog box to choose the profile.
  48. oNS.Logon(missing, missing, True, True)
  49.  
  50. folder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar)
  51. CalendarSharing = folder.GetCalendarExporter()
  52. CalendarSharing.CalendarDetail = Outlook.olCalendarDetail.olFreeBusyOnly
  53. CalendarSharing.startDate = Date.Now
  54. CalendarSharing.endDate = DateAdd(DateInterval.Day, 7, Date.Now)
  55. CalendarSharing.RestrictToWorkingHours = True
  56. CalendarSharing.IncludeAttachments = False
  57. CalendarSharing.IncludePrivateDetails = False
  58. oMail = CalendarSharing.ForwardAsICal(Outlook.OlCalendarMailFormat.olCalendarMailFormatDailySchedule)
  59. oMail.Recipients.Add("me@com.com")
  60. oMail.Send()
  61.  
  62. ' Alternative logon method that uses a specific profile.
  63. ' If you use this logon method, change the profile name to an
  64. ' appropriate value. The second parameter of Logon is the password
  65. ' (if any) associated with the profile. This parameter exists only
  66. ' for backwards compatibility and for security reasons, and it is
  67. ' not recommended for use.
  68. 'oNS.Logon("YourValidProfile", missing, False, True)
  69.  
  70. Console.WriteLine("Press ENTER to continue when Outlook is ready.")
  71. Console.ReadLine()
  72.  
  73.  
  74. ' User logs off and quits Outlook.
  75.  
  76. Console.WriteLine("Log off and quit the Outlook application")
  77. oNS.Logoff()
  78. oOutlook.Quit()
  79.  
  80. Catch ex As Exception
  81. Console.WriteLine("AutomateOutlook throws the error: {0}", ex.Message)
  82. Finally
  83.  
  84. ' Manually clean up the explicit unmanaged Outlook COM resources by
  85. ' calling Marshal.FinalReleaseComObject on all accessor objects.
  86. ' See http://support.microsoft.com/kb/317109.
  87.  
  88. If Not oMail Is Nothing Then
  89. Marshal.FinalReleaseComObject(oMail)
  90. oMail = Nothing
  91. End If
  92. If Not oCts Is Nothing Then
  93. Marshal.FinalReleaseComObject(oCts)
  94. oCts = Nothing
  95. End If
  96. If Not oCtFolder Is Nothing Then
  97. Marshal.FinalReleaseComObject(oCtFolder)
  98. oCtFolder = Nothing
  99. End If
  100. If Not oNS Is Nothing Then
  101. Marshal.FinalReleaseComObject(oNS)
  102. oNS = Nothing
  103. End If
  104. If Not oOutlook Is Nothing Then
  105. Marshal.FinalReleaseComObject(oOutlook)
  106. oOutlook = Nothing
  107. End If
  108.  
  109. End Try
  110.  
  111. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement