ComObjConnect() help
Posted: 16 Mar 2017, 14:35
Hey all,
This is my first dive into classes with AHK. I'm trying to do something fairly simple. I have a tray menu item that launches a function called changeControl(). This creates a new instance of my class call changeControl.
I have it correctly creating the email via the createCCEmail method but I am having trouble connecting onto the events of the email. I tried creating a nested class for the email's events and another for the events of an appointment item (we can worry about that later). I tried changing my menu item to launch an actual label instead of a function because i thought the problem was that my class instance was disappearing after the function returned but that still didn't seem to work. Below is a snippet my code. The menu item's function is at the bottom. This gets called correctly and the instance of changeControl is created and the email displays as desired. I just can't get the event sink to work.
This is my first dive into classes with AHK. I'm trying to do something fairly simple. I have a tray menu item that launches a function called changeControl(). This creates a new instance of my class call changeControl.
I have it correctly creating the email via the createCCEmail method but I am having trouble connecting onto the events of the email. I tried creating a nested class for the email's events and another for the events of an appointment item (we can worry about that later). I tried changing my menu item to launch an actual label instead of a function because i thought the problem was that my class instance was disappearing after the function returned but that still didn't seem to work. Below is a snippet my code. The menu item's function is at the bottom. This gets called correctly and the instance of changeControl is created and the email displays as desired. I just can't get the event sink to work.
Code: Select all
class changeControl {
Static olFolderCalendar := 9
Static olAppointmentItem := 1
Static olFree := 0
Static olTentative := 1
Static olBusy := 2
Static olOutOfOffice := 3
__New(itemName) {
if(!this.ol := this.getOutlookReference())
return
this.labelName := itemName
return this
}
; Attempts to get the active Outlook application
; If it fails, it attempts to create one and display the main inbox
; Returns the reference if it is found, otherwise returns false
getOutlookReference() {
try
ol := ComObjActive("Outlook.Application")
catch e1 {
try {
ol := ComObjCreate("Outlook.Application")
ol.Session.GetDefaultFolder(6).Display
}
catch e2 {
MsgBox, % "Did not find an instance of Outlook and was unable to create one!`r`nErrors were:`r`n`t" e1.message "`r`n`t" e2.message
return False
}
}
Return ol
}
createCCEmail() {
olNS := ol.session
ccBody := "
(
<body style='font-family:Arial;'>
<div>
<p><b>Purpose of the change:</b>
<o:p><br /></o:p>
</p>
<p><b>Risk Level:</b>
<o:p><br /></o:p>
</p>
<p><b>Date of the change:</b>
<o:p></o:p>
</p>
<p>
<o:p>" A_MM "/" A_DD "/" A_YYYY "</o:p>
</p>
<p><b>Implementation Plan (Technical Version):</b>
<o:p><br /></o:p>
</p>
<p><b>Downtime expected/potential:</b>
<o:p><br /></o:p>
</p>
<p><b>Rollback Plan:</b>
<o:p><br /></o:p>
</p>
<p><b>Peer Review:</b>
<o:p><br /></o:p>
</p>
<p><b>Management/Team Lead Approval:</b>
<o:p><br /></o:p>
</p>
<p><b>User awareness communication plan:</b>
<o:p><br /></o:p>
</p>
</div>
<body>
)"
olEmail := this.ol.CreateItem(0)
olEmail.To := "<email>"
olEmail.Subject := "CC# - "
olEmail.Display
if (this.labelName = "Change Control")
olEmail.HTMLBody := ccBody . olEmail.HTMLBody
ComObjConnect(olEmail, new this.email_events(this))
}
createCCAppt(subject, body) {
olOwner := this.CreateRecipient("IS-EventsCalendar")
olOwner.Resolve
If olOwner.Resolved
olCalendar := this.ol.session.GetSharedDefaultFolder(olOwner, olFolderCalendar).Items
olNewAppt := olCalendar.Add(olAppointmentItem)
olNewAppt.BusyStatus := olFree
olNewAppt.AllDayEvent := True
olNewAppt.ReminderSet := False
olNewAppt.Subject := (subject ? subject : "CC# - ")
olNewAppt.Body := body
olNewAppt.Display
ComObjConnect(olNewAppt, new this.appt_events(this))
}
class email_events {
__New(parent) {
this.parent := parent
}
Send(args*) {
this.parent.createCCAppt(args[1].Subject, args[1].RTFBody)
}
Close(args*) {
msgbox, test
for _,arg in args
msgbox, % arg
}
}
class appt_events {
__New(parent) {
this.parent := parent
}
BeforeAutoSave(args*) {
return true
}
Send(args*) {
this.parent.ol := ""
}
}
}
changeControl(itemName, itemPos, menuName) {
ccEmail := new changeControl(itemName).createCCEmail()
}