Skip to content

Instantly share code, notes, and snippets.

@tdalon
Last active January 30, 2023 17:55
Show Gist options
  • Save tdalon/8d396639c43bc2100224df00a5f84c7d to your computer and use it in GitHub Desktop.
Save tdalon/8d396639c43bc2100224df00a5f84c7d to your computer and use it in GitHub Desktop.
Outlook to Teams Group Chat - AutoHotkey
Outlook_GetCurrentItem(olApp:="") {
If (olApp = "")
olApp := ComObjActive("Outlook.Application")
try
olItem := olApp.ActiveWindow.CurrentItem
catch
olItem := olApp.ActiveExplorer.Selection.Item(1)
return olItem
} ; eofun
Outlook_Item2Emails(oItem:="",sMode :="",validate:=False) {
; sEmailList := Outlook_Item2Emails(oItem:="",sMode:="",validate:= False)
; If oItem is empty, GetCurrentItem
; If sMode is empty, user will be prompted for selection noFrom|noTo|noCc
; sMode string containing noFrom, noTo, noCc
; validate: if True user will have the possibility to edit the email List in an InputBox
olApp := ComObjActive("Outlook.Application")
If (oItem == "")
oItem := Outlook_GetCurrentItem(olApp)
If (sMode == "") { ; Prompt user for selection
sMode := ListView("To Chat","Select Fields to exclude","noCc|noFrom|noTo","Fields",False)
If (sMode == "ListViewCancel")
return
}
; Loop on Recipients
cnt := oItem.Recipients.Count
Loop, %cnt%
{
oRecip := oItem.Recipients.Item(A_Index)
; https://learn.microsoft.com/en-us/office/vba/api/outlook.olmailrecipienttype
If ((oRecip.Type == 1) and !InStr(sMode,"noTo"))
or ((oRecip.Type == 2) and !InStr(sMode,"noCc"))
or ((oRecip.Type == 0) and !InStr(sMode,"noFrom"))
{
sEmail := Outlook_Recipient2Email(oRecip)
sEmailList := sEmailList . ";" . sEmail
}
}
RegExReplace(sEmailList, ";" , , nCount)
sEmailList := SubStr(sEmailList,2) ; remove starting ;
If (validate) {
sEMailList:=MultiLineInputBox("Edit Email List (" . nCount . "):", sEmailList, "To Chat")
if (ErrorLevel)
return
}
return sEmailList
} ; eofun
Outlook_Recipient2Email(oRecip) {
; https://stackoverflow.com/a/51939384/2043349
; takes a Display Name (i.e. "James Smith") and turns it into an email address (james.smith@myco.com)
; necessary because the Outlook address is a long, convoluted string when the email is going to someone in the organization.
; source: https://stackoverflow.com/questions/31161726/creating-a-check-names-button-in-excel
Address := oRecip.Address
If (SubStr(Address, 1,1) == "/") { ; Inside Organization -> Resolve to SMTP email
oRecip.Resolve
If oRecip.Resolved {
Switch oRecip.AddressEntry.AddressEntryUserType
{
Case 0,5: ;olExchangeUserAddressEntry & olExchangeRemoteUserAddressEntry
oEU := oRecip.AddressEntry.GetExchangeUser
return oEU.PrimarySmtpAddress
Case 10, 30: ;olOutlookContactAddressEntry & 'olSmtpAddressEntry
return oRecip.AddressEntry.Address
Default:
} ; end switch
} ; End If
} Else
return Address
} ; eofun
Teams_Emails2Chat(sEmailList,sTopicName :=""){
; Open Teams 1-1 Chat or Group Chat from list of Emails
; See https://learn.microsoft.com/en-us/microsoftteams/platform/concepts/build-and-test/deep-link-teams
sLink := "msteams:/l/chat/0/0?users=" . StrReplace(sEmailList, ";",",") ; msteams:
If InStr(sEmailList,";") { ; Group Chat
InputBox, sTopicName , To Chat, Enter Group Chat Name, , , , , , , ,%sTopicName%
if (ErrorLevel=0) { ; TopicName defined
sLink := sLink . "&topicName=" . StrReplace(sTopicName, ":", "")
}
}
Run, %sLink%
} ; eofun
Teams_OpenChat(sSelection := "") {
; Use Selection or Outlook Object
If WinActive("ahk_exe Outlook.exe") {
olApp := ComObjActive("Outlook.Application")
oItem := Outlook_GetCurrentItem(olApp)
Switch Outlook_Item2Type(oItem) {
Case "Mail":
sTopicName := "[Email] " . oItem.Subject
Case "Meeting": ; From Inbox Meeting Request
oItem := oItem.GetAssociatedAppointment(False)
sTopicName := "[Meeting] " . oItem.Subject
Case "Appointment":
sTopicName := "[Meeting] " . oItem.Subject
}
sEmailList := Outlook_Item2Emails(oItem,,True)
If (sEmailList=="") {
return
}
Teams_Emails2Chat(sEmailList,sTopicName)
} Else {
Teams_Selection2Chat(sSelection)
}
} ; eofun
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment