[Solved]Outlook Appointment in other Calendar

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
gilliduck
Posts: 265
Joined: 06 Oct 2014, 15:01

[Solved]Outlook Appointment in other Calendar

06 Aug 2015, 08:48

I'm trying to set up an appointment in something other than my default calendar in Outlook.

Code: Select all

subjectfield := "Test Appointment"
startfield := "2015/08/06 9:00a"
endfield := "2015/08/06 10:00a"

Application := ComObjActive("Outlook.Application")
olAppointmentItem := 1
calItem := ComObjActive("Outlook.Application").CreateItem(olAppointmentItem)
olFormatHTML := 2
calItem.Subject := subjectfield
calItem.Start := startfield
calItem.End := endfield
calItem.AllDayEvent := False
calItem.Save()
That works fine for adding an appointment, just have no idea what to add it get it to add the appointment to some other calendar. My end goal is to have it add to a public shared calendar I have access to, but for now I just want it to add to ANY calendar other than default.
Last edited by gilliduck on 08 Aug 2015, 15:57, edited 1 time in total.
gilliduck
Posts: 265
Joined: 06 Oct 2014, 15:01

Re: Outlook Appointment in other Calendar

08 Aug 2015, 15:56

Ok, figured it out (sorta) myself. I can't create on another Calendar, but I can move it fairly easily.

Here are two methods (one for local calendars and the other is for a shared calendar).

FYI there is more junk in these scripts than is barebones necessary for someone else to use, but if someone needs the same thing they should be able to figure out the parts needed.

Shared Calendar:

Code: Select all

; Calendar := "Supervisor"
Category := "BLue Category"
Name := "Test Name"
Comment := "Test Comment"
Start_Time := "5:00p"
Start_Date := "8/8"
End_Time := "7:00p"
End_Date := "8/8"

olFolderCalendar := 9 
olAppointmentItem := 1
profileName := "Outlook"


Outlook := ComObjCreate("Outlook.Application")
namespace := Outlook.GetNamespace("MAPI")
namespace.Logon(profileName)  
olcalendar := namespace.GetDefaultFolder(olFolderCalendar)
; myCal := olcalendar.Folders(Calendar)
myCal := namespace.Folders("SharePoint Lists").Folders("Operations - Operations Schedule")
calItem := Outlook.CreateItem(olAppointmentItem)
calItem.Categories := Category
calItem.Subject := Name . " - " . Comment
If (Comment = "")
	calItem.Subject := Name
IsTime := SubStr(Start_Time,1,1)
If IsTime is Not Digit
	{
	calItem.Start := Start_Date
	calItem.AllDayEvent := True
	calItem.Subject := Name . " - " . Start_Time
	}
Else
	{
	calItem.Start := Start_Date . A_Space . Start_Time
	calItem.End := End_Date . A_Space . End_Time
	}
calItem.Save()
calItem.Move(myCal)
Return
Local Calendar:

Code: Select all

Calendar := "Supervisor"
Category := "BLue Category"
Name := "Test Name"
Comment := "Test Comment"
Start_Time := "5:00p"
Start_Date := "8/8"
End_Time := "7:00p"
End_Date := "8/8"

olFolderCalendar := 9 
olAppointmentItem := 1
profileName := "Outlook"


Outlook := ComObjCreate("Outlook.Application")
namespace := Outlook.GetNamespace("MAPI")
namespace.Logon(profileName)  
olcalendar := namespace.GetDefaultFolder(olFolderCalendar)
myCal := olcalendar.Folders(Calendar)
; myCal := namespace.Folders("SharePoint Lists").Folders("Operations - Operations Schedule")
calItem := Outlook.CreateItem(olAppointmentItem)
calItem.Categories := Category
calItem.Subject := Name . " - " . Comment
If (Comment = "")
	calItem.Subject := Name
IsTime := SubStr(Start_Time,1,1)
If IsTime is Not Digit
	{
	calItem.Start := Start_Date
	calItem.AllDayEvent := True
	calItem.Subject := Name . " - " . Start_Time
	}
Else
	{
	calItem.Start := Start_Date . A_Space . Start_Time
	calItem.End := End_Date . A_Space . End_Time
	}
calItem.Save()
calItem.Move(myCal)
Return
User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Re: [Solved]Outlook Appointment in other Calendar

01 Mar 2017, 18:50

@gilliduck- thank you for sharing this! I needed to automate creating calendar entries so I borrowed and added a few more parameters and helpful links

https://autohotkey.com/boards/viewtopic.php?t=77#p476 ;general read
https://msdn.microsoft.com/en-us/library/bb219939 ;Appointment
https://msdn.microsoft.com/en-us/librar ... e.14).aspx

Code: Select all

calItem := ComObjCreate("Outlook.Application").CreateItem(1)
calItem.Categories := "Red Category" ;~ Category := "Blue Category"
calItem.Location:="Internet"
calItem.OptionalAttendees:="Bob the Builder"
calItem.RequiredAttendees:="James Brown"
calItem.Importance:=2 ;0=low 1=Normal 2=High
calItem.Sensitivity:=2 ;0=Normal 1=Personal 2=Private 3=Confidential
calItem.Body :="Make sure you plan ahead and do this"
calItem.BusyStatus:=0 ;0=Free 1=Tentative 2=Busy  3=Out of Office 4=Working Elsewhere
calItem.ReminderMinutesBeforeStart:="60"
calItem.Subject := "Cert Expiration-specific tool"
calItem.Start := "3/2/2017" . A_Space . "5:00pm"
calItem.Duration :=60	; minutes  ;note if you use this, you don't need to set end date
;~ calItem.End   := "3/2/2017" . A_Space . "7:00pm"
calItem.Display() ;if you want it to be visible
;~ calItem.Save()
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: [Solved]Outlook Appointment in other Calendar

01 Mar 2017, 20:59

Take a look at NameSpace.CreateRecipient and NameSpace.GetSharedDefaultFolder Though, I'm not sure that NameSpace.GetSharedDefaultFolder() would work if the shared calendar is not the default calendar. Typically it has been that the shared calendars we have access to all have their own emails as well so this hasn't been a problem for me.

I use it currently as in the below example:

Code: Select all

ol := ComObjActive("Outlook.Application")
olNS := ol.GetNameSpace("MAPI")
olNS.Logon(A_Username "@domain.net")
olOwner := olNS.CreateRecipient("IS-OnCall")
olOwner.Resolve()
If olOwner.Resolved
	olCalendar := olNS.GetSharedDefaultFolder(olOwner, olFolderCalendar := 9).Items
olNewAppt := olCalendar.Add(olAppointmentItem := 1)
olNewAppt.BusyStatus := olFree := 0
olNewAppt.AllDayEvent := True
olNewAppt.ReminderSet := False
olNewAppt.Display
olNewAppt.Save()
Let me know if that does what you're wanting.
User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Re: [Solved]Outlook Appointment in other Calendar

01 Mar 2017, 21:29

I haven't had an issue with it as of now (and I no-longer work in a corporate environment so I probably wont) but will definitely note it for future. :)
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Re: [Solved]Outlook Appointment in other Calendar

02 Mar 2017, 17:51

Has anyone played with removing/deleting a calendar event?
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Re: [Solved]Outlook Appointment in other Calendar

02 Mar 2017, 19:04

Thanks. I'm writing a script and will share later
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Re: [Solved]Outlook Appointment in other Calendar

02 Mar 2017, 21:59

In the below I iterate over every calendar entry and search the subject line for "My Test". If it is found, it will delete the entry. This could easily be adapted to look in the body or on many different properties.

Code: Select all

Calendar:= ComObjCreate("Outlook.Application").GetNamespace("MAPI").GetDefaultFolder(9)
MsgBox % "you have the following number of calendar items: " count := calendar.items.Count

for appt in calendar.items {
if  Instr(appt.Subject,"My Test"){  ;updated to reflect Kon's comments below.  Much cleaner!
	  MsgBox % "found " appt.Subject " in #" A_Index
		    appt.Delete ;Deletes it
    }
}
Last edited by Joe Glines on 03 Mar 2017, 07:17, edited 1 time in total.
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: [Solved]Outlook Appointment in other Calendar

02 Mar 2017, 22:55

There's no braces around the body of the if, so this would delete all the appointments, no?

Also, calendar.items[A_Index].Subject is used inside the for-loop...

Does this work instead?
appt.Subject

And just a tip: Instead of .GetNamespace("MAPI") you can use .Session. ;)
User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Re: [Solved]Outlook Appointment in other Calendar

03 Mar 2017, 07:13

@Kon- Thanks! I initially did not have the msgbox thus I only had 1 line to delete the item (and the braces were unneeded. I'll go revise it to add braces so it would work as expected.

As for the appt.Subject- Yes that works. I had started off writing that and was having issues with what I was looping over (I think because I didn't initially have the .items on calendar). Anyway, I'll make this update too as it is obviously the right approach. :oops:

Is there an advantage of using .Session over .GetNamespace("MAPI")?
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: [Solved]Outlook Appointment in other Calendar

03 Mar 2017, 09:37

Joe Glines wrote:Is there an advantage of using .Session over .GetNamespace("MAPI")?
It's not particularly important, it's just shorter. :)
Application.Session Property (Outlook)
jodahush
Posts: 6
Joined: 28 Feb 2017, 03:17

Re: [Solved]Outlook Appointment in other Calendar

06 Mar 2017, 10:20

Hey, great peace of code. And the best ist, it works even for me.

It shows how to create a com object.

I would like to find a way to saveas the whole calendar. Can you provide any clue how to get there?
User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Re: [Solved]Outlook Appointment in other Calendar

06 Mar 2017, 11:30

When you say "save as" -what do you mean? My only guess is that you'd want to export your calendar entries... For that you could just iterate over each item and then save it locally as a file. I haven't done this with a calendar item but have with email... The other thing you might want to do is just export releveant fields (subject, time/body) to flat file like Excel or tab delimited.
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], mikeyww, steve88 and 180 guests