How do I run vba every weekday morning

DKY

Joined
Sep 4, 2008
Messages
4
Reaction score
0
I have a set of code that sends my calendar in email form to an email address. I have this mapped to a button but get tired of forgetting to send it.

Is there a way to make this code send automatically every weekday morning? I'd even like it if it checked if it had been sent that day already and if it didn't then send it when I open my outlook. I can't figure out how to do this.

Code:
Public Sub ShareWorkCalendarByPayload()
	
	Dim oNamespace As NameSpace
	Dim oFolder As Folder
	Dim oCalendarSharing As CalendarSharing
	Dim oMailItem As MailItem
	
	On Error GoTo ErrRoutine
		' Get a reference to the Calendar default folder
		Set oNamespace = Application.GetNamespace("MAPI")
		Set oFolder = oNamespace.GetDefaultFolder(olFolderCalendar)
		
		' Get a reference to a CalendarSharing object for that
		' folder.
		Set oCalendarSharing = oFolder.GetCalendarExporter
	
		' Set the CalendarSharing object to restrict
		' the information shared in the iCalendar file.
		With oCalendarSharing
			' Send free/busy information only.
			'.CalendarDetail = olFreeBusyOnly
			.CalendarDetail = olFreeBusyAndSubject
						
			' Send information for the next seven days.
			.StartDate = Now
			.EndDate = DateAdd("d", 0, Now)
			'.EndDate = DateAdd("d", 7, Now)
			
			' Restrict information to working hours only.
			.RestrictToWorkingHours = False
			
			' Exclude attachments and private information.
			.IncludeAttachments = False
			.IncludePrivateDetails = True
		End With
		
		' Get the mail item containing the iCalendar file
		' and calendar information.
		'Set oMailItem = oCalendarSharing.ForwardAsICal( _
			olCalendarMailFormatlistofevents)
		Set oMailItem = oCalendarSharing.ForwardAsICal(olCalendarMailFormatEventList)
		' Send the mail item to the specified recipient.
		With oMailItem
			.Recipients.Add "[email="[email protected]"][email protected][/email]"
			.Send
		End With
EndRoutine:
	On Error GoTo 0
	Set oMailItem = Nothing
	Set oCalendarSharing = Nothing
	Set oFolder = Nothing
	Set oNamespace = Nothing
Exit Sub
ErrRoutine:
	Select Case Err.Number
		Case 287 ' &H0000011F
			' The user denied access to the Address Book.
			' This error occurs if the code is run by an
			' untrusted application, and the user chose not to
			' allow access.
			MsgBox "Access to Outlook was denied by the user.", _
				vbOKOnly, _
				Err.Number & " - " & Err.Source
		Case -2147467259  ' &H80004005
			' Export failed.
			' This error typically occurs if the CalendarSharing
			' method cannot export the calendar information because
			' of conflicting property settings.
			MsgBox Err.Description, _
				vbOKOnly, _
				Err.Number & " - " & Err.Source
		Case -2147221233  ' &H8004010F
			' Operation failed.
			' This error typically occurs if the GetCalendarExporter method
			' is called on a folder that doesn't contain calendar items.
			MsgBox Err.Description, _
				vbOKOnly, _
				Err.Number & " - " & Err.Source
		Case Else
			' Any other error that may occur.
			MsgBox Err.Description, _
				vbOKOnly, _
				Err.Number & " - " & Err.Source
	End Select
			
	GoTo EndRoutine
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top