Help making Outlook Calendar item. Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
kunkel321
Posts: 184
Joined: 30 Nov 2015, 21:19
Facebook: https://www.facebook.com/kunkel321
Google: kunkel321

Help making Outlook Calendar item.

07 Nov 2017, 17:25

I have a simple app that uses Outlook's command line abilities to make an appointment run, C:\Program Files (x86)\Microsoft Office\Office16\OUTLOOK.EXE /c ipm.appointment. It works.

But I thought I'd try my hand at accessing OL via it's object model. So far it seems to be above my ability level though. I tried this one https://autohotkey.com/boards/viewtopic.php?t=77 by SinkFaze and it doesn't do anything. Can anyone tell me if the code is even compatible with Outlook 2016 on Windows 10? Also, I tried Joe's code here http://the-automator.com/. With Joe's I get an error from #Include <default_Settings>. I thought maybe my .ahk needed to be in the same folder as Autohotkey.exe, but that didn't help. Also note: I had Outlook running in the background while trying this.

Any ideas what I'm doing wrong?

EDIT: It occurs to me that I'm not specifying the exact code I'm actually using...
The SinkFaze code is this:

Code: Select all

#NoEnv
#SingleInstance force
Esc:: ExitApp
;################################# source SinkFaze https://autohotkey.com/boards/viewtopic.php?t=77


olFolderCalendar :=	9   ; Outlook.OlDefaultFolders.olFolderContacts
olFolderContacts :=	10  ; get constants from visual studio
olAppointmentItem = 1
                        
profileName :=	"Outlook"
Outlook :=	ComObjCreate("Outlook.Application")
namespace :=	Outlook.GetNamespace("MAPI")
namespace.Logon(profileName)  
calendar :=	namespace.GetDefaultFolder(olFolderCalendar)
items :=	calendar.Items
count :=	items.Count
msgbox %	"calendar items: " count
item :=	calendar.Items(1)
item1 :=	"subject: " item.Subject . "`n"
item1 .=	"Start: " item.Start . "`n"
item1 .=	"Duration: " item.Duration . "`n"
item1 .=	"Body: " item.Body "`n"
msgbox %	"item1" item1
date :=	"9/1/2010 9:00:00 AM"	; this works, although the recommended
				; format is year/month/date
				; hours:minutes:seconds AM/PM

date :=	"2010/9/1 9:00:00 AM"
msgbox %	makeAppointment(Outlook, "auto", "autohotkey rocks", date, "60", "2")

!r::reload
!q::exitapp

makeAppointment(outlook, subject, body, startTime, duration, busyStatus)
{
	static	olAppointmentItem = 1
	 , olFree = 0
	 , olTentative = 1
	 , olBusy = 2
	 , olOutOfOffice = 3
	item :=	outlook.CreateItem(olAppointmentItem)
	item.Body :=	body
	item.BusyStatus :=	busyStatus ; olBusy
	msgbox %	startTime
	item.Start :=	startTime ; 9/1/2010 9:00:00 AM
	item.Duration :=	duration ; 60
	item.Subject :=	subject
	return	item.save()  ; warns about programmatic access with normal settings
}
And the Joe Glines code is this: (with the #Include commented out)

Code: Select all

#NoEnv
#SingleInstance force
Esc:: ExitApp
;#################################
;#Include <default_Settings>
; source Joe Glines: http://the-automator.com/
;***********************Read and Write appointments from Calendar**************************.
;http://www.autohotkey.com/forum/viewtopic.php?t=61509&p=379775#379775
;~https://msdn.microsoft.com/EN-US/library/office/ff862177.aspx

Start_Time:="9/1/2017 11:00:00 AM"  ; "3/2/2017" . A_Space . "5:00pm"
Body:="my test of body"
Subj:="my test of subject"
Audio:="C:\Windows\Media\Windows Notify Messaging.wav"

;~ Create_Appoint(Body,0,"Red",1,10,0,"Office","Joe","joejunk",20,Audio,1,Start_Time,Subj)
Create_Appoint(Body,0,"Red",1,10,0,"Office","Joe","joejunk",,,,Start_Time,Subj)
return

Create_Appoint(Body="",Busy_Status="2",Category="Blue",Display=0,Duration=60,Importance=1,Location="",Opt_Attend="",Req_Attend="",Reminder_Min=20,Reminder_Audio="",Sensitivity=0,Start_Time="",Subject=""){
Outlook := ComObjCreate("Outlook.Application") ;also check to see if Outlook is running?
item := outlook.CreateItem(1) ;create appointment
;~item.Attachments.Add:=""http://replyapp.acemlnb.com/lt.php?s=c95b6ab39c2d493c61162c1479113bc2&i=497A659A26A8815http://replyapp.acemlnb.com/lt.php?s=c95b6ab39c2d493c61162c1479113bc2&i=497A659A26A8815
;~item.AllDayEvent:=1 ;True
item.Body := Body
item.BusyStatus := Busy_Status ;0=Free 1=Tentative 2=Busy  3=Out of Office 4=Working Elsewhere
item.Categories := Category . " Category" ;~ "Blue Category"  https://msdn.microsoft.com/en-us/library/office/ff864208.aspx
item.Duration := Duration ; 60 ;use this or item.end
;~ item.End   := End_Time ;"3/2/2017" . A_Space . "7:00pm"
item.Importance:= Importance ;0=low 1=Normal 2=High
item.Location:= Location ;"Internet" ;Location of appointment
item.OptionalAttendees:= Opt_Attend ; "Bob the Builder"
item.RequiredAttendees:= Req_Attend ; "James Brown"
Item.ReminderMinutesBeforeStart:= Reminder_Min ; 60
item.ReminderSet := True
;~ item.RTFBody:=Body ;https://msdn.microsoft.com/en-us/library/office/ff861303.aspx
item.ReminderPlaySound := True
item.ReminderSoundFile:=Reminder_Audio
item.Sensitivity:= Sensitivity ;2 ;0=Normal 1=Personal 2=Private 3=Confidential
item.Start := Start_Time ; 9/1/2010 9:00:00 AM
item.Subject := Subject
if (Display=1)
    item.Display()
item.save()  ; warns about programmatic access with normal settings
}

FanaticGuru
Posts: 1406
Joined: 30 Sep 2013, 22:25

Re: Help making Outlook Calendar item.  Topic is solved

07 Nov 2017, 18:22

kunkel321 wrote:But I thought I'd try my hand at accessing OL via it's object model.
Here is a simple start:

Code: Select all

olApp := ComObjCreate("Outlook.Application")
olAppoint := olApp.CreateItem(1)	; olAppointmentItem := 1
olAppoint.Subject := "Start a New Year"
olAppoint.Location := "Top of the World" 
olAppoint.Start := "1/1/2018 8:32:00 AM" 
olAppoint.Duration := 67
olAppoint.Body := "Don't forget to breath"

olAppoint.Display ; Remove this line to have it all happen in the background
;~ olAppoint.Save ; Uncomment to automatically save
There is a ton of other things you can do with an Appointment object but this is the basics.

Creating an email, creating a contact, etc. are all very similar processes.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

[Function] Timer - Create and Manage Timers
kunkel321
Posts: 184
Joined: 30 Nov 2015, 21:19
Facebook: https://www.facebook.com/kunkel321
Google: kunkel321

Re: Help making Outlook Calendar item.

07 Nov 2017, 18:38

Thanks FG!
It turns out that I (apparently) don't know how AHK works. :-/
Your code also did not work until I removed Esc:: ExitApp from the top of it. Removing this also fixed the other two. This line of code was not "exiting" the script, but it did prevent any of the lines of code from running. (Apparently.)

This does nothing:

Code: Select all

#NoEnv
#SingleInstance force
Esc:: ExitApp
;################################# FantasticGuru https://autohotkey.com//boards/viewtopic.php?f=5&t=39576
MsgBox, beginning of script

olApp := ComObjCreate("Outlook.Application")
olAppoint := olApp.CreateItem(1)	; olAppointmentItem := 1
olAppoint.Subject := "Start a New Year"
olAppoint.Location := "Top of the World" 
olAppoint.Start := "1/1/2018 8:32:00 AM" 
olAppoint.Duration := 67
olAppoint.Body := "Don't forget to breath"

olAppoint.Display ; Remove this line to have it all happen in the background
;~ olAppoint.Save ; Uncomment to automatically save
MsgBox, end of script
But this:

Code: Select all

#NoEnv
#SingleInstance force

;################################# FantasticGuru https://autohotkey.com//boards/viewtopic.php?f=5&t=39576
MsgBox, beginning of script

olApp := ComObjCreate("Outlook.Application")
olAppoint := olApp.CreateItem(1)	; olAppointmentItem := 1
olAppoint.Subject := "Start a New Year"
olAppoint.Location := "Top of the World" 
olAppoint.Start := "1/1/2018 8:32:00 AM" 
olAppoint.Duration := 67
olAppoint.Body := "Don't forget to breath"

olAppoint.Display ; Remove this line to have it all happen in the background
;~ olAppoint.Save ; Uncomment to automatically save
MsgBox, end of script

Esc:: ExitApp
works as expected. ....live and learn, I guess. :oops:
FanaticGuru
Posts: 1406
Joined: 30 Sep 2013, 22:25

Re: Help making Outlook Calendar item.

07 Nov 2017, 18:50

kunkel321 wrote:Thanks FG!
It turns out that I (apparently) don't know how AHK works. :-/
Your code also did not work until I removed Esc:: ExitApp from the top of it. Removing this also fixed the other two. This line of code was not "exiting" the script, but it did prevent any of the lines of code from running. (Apparently.)
Yea, the top of a script before any hotkeys is special:
https://autohotkey.com/docs/Scripts.htm#auto

Esc::ExitApp is often put at the end of scripts when testing as a quick way to kill a script and stop it from processing. This is pretty much never something you would want in an actual working script as the script would stop as soon as you press Esc.

Maybe my simple code will help you to better understand the more complex code of others where they created a function with lots of parameters to create calendar appointments.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

[Function] Timer - Create and Manage Timers

Return to “Ask For Help”

Who is online

Users browsing this forum: artur1884, Bing [Bot], Google [Bot], Rohwedder, songdg, zavf and 33 guests