Mickers Outlook COM MSDN for Ahk_L

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Micromegas
Posts: 260
Joined: 28 Apr 2015, 23:02
Location: Germany

Mickers Outlook COM MSDN for Ahk_L

15 May 2015, 02:06

Several discussion here, such as Outlook .OFT & Mailto, link to a great start of a tutorial, Mickers Outlook COM MSDN for Ahk_L. That link doesn't resolve for me anymore, but it's archived at web.archive.org. I tried out the first example,

Code: Select all

^a::
	MsgBox % "The display name is " . ComObjActive("Outlook.Application").ActiveWindow.Caption . "."
	Return
, and am happy to see it works. But when I try to apply a function such as the following

Code: Select all

	win := ComObjActive("Outlook.Application").ActiveWindow
	MsgBox % TypeName(win)
I get Error: Call to nonexistent function. Is there a way to get such functions in AHK? Should I be using AHK_L, whatever that is? (Unfortunately, Novice Question: AHK_Basic vs. AHK_L, what's the difference ... has not made it to the Wayback Machine.)
Guest

Re: Mickers Outlook COM MSDN for Ahk_L

15 May 2015, 02:39

The .com website has been down for the last day or so. It will likely come back online soon. Ownership of that domain is currently being transferred to the admins of this site, but it is not under their control yet.

"Basic" now commonly refers to the most recent version of AHK authored by Chris (~2009), AHK's original author. AHK_L was a fork of AHK by lexikos, but it has been the main branch for a while. It's now referred to as AHK v1.1

RE "TypeName" function: It may be possible, but it's probably easier to hard code the values into the script.
User avatar
Micromegas
Posts: 260
Joined: 28 Apr 2015, 23:02
Location: Germany

Re: Mickers Outlook COM MSDN for Ahk_L

15 May 2015, 09:13

Thanks for the explanation for the website and the fork.

But what do you mean by "hard code the values into the script"?
Guest

Re: Mickers Outlook COM MSDN for Ahk_L

15 May 2015, 12:18

Application.ActiveWindow returns an object upon success, and nothing upon failure. So to check the returned value use if (IsObject(win)) or simply if (win).

Upon failure an error message will probably be displayed anyway. But, if you want your script to handle error checks without prompting the user you can use ComObjError() or try/catch to suppress the errors.
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Mickers Outlook COM MSDN for Ahk_L

15 May 2015, 13:52

From the example on the Application.ActiveWindow docs page, it looks like you may be trying to determine if the window is an Inspector or Explorer.
I think you can use Inspector.Class / Explorer.Class to do that. (Edit: MsgBox, % ComObjActive("Outlook.Application").ActiveWindow.Class)
unexus
Posts: 1
Joined: 20 Oct 2017, 06:57

Re: Mickers Outlook COM MSDN for Ahk_L

20 Oct 2017, 07:36

Hi All,

I know there has been no posts to this thread for a couple of years, but I'm so close to getting where I need to be with a bit of code ... just can't figure out one issue... Hoping someone can help.

I use Spiceworks for Helpdesk Ticket management nevertheless some users still insist on emailing me directly instead of raising a helpdesk ticket. I wrote a bit of AHK that allows me to hit a hotkey and forward the email to spiceworks to create the ticket, however, for the ownership of the ticket to be correct, I need the From field of the email to be the person that sent me the email. Hope this is making sense.

We run on premise exchange so when I detect the SenderName from within Outlook I get an AD OU format.. something like this...

/O=TESTNET/OU=EXCHANGE ADMINISTRATIVE GROUP (FYDIBOHF23SPDLT)/CN=RECIPIENTS/CN=DOE, JOHN23F

From the AHK archived posts, I found this code that resolves the Primary SMTP address of the email sender

Code: Select all

; http://answers.microsoft.com/en-us/office/forum/office_2007-customize/need-vba-to-obtain-smtp-address-of-exchange-user/97833c7c-18e3-4b8d-923a-606b81c9ecd1?auth=1
olApp := ComObjActive("Outlook.Application")
olNamespace := olApp.GetNamespace("MAPI")
Window := olApp.ActiveWindow 
if (Window.Class = 35) ; 35 = An Inspector object. (The email is open in its own window)
{
  SmtpAddress := ""
  MailItem := Window.CurrentItem
  FromName := MailItem.SenderName
  Recip := olApp.Session.CreateRecipient(FromName)
  Recip.Resolve
  if (Recip.Resolved)
  {
    UserType := Recip.AddressEntry.AddressEntryUserType
    if (UserType = 0 || UserType = 10) ; olExchangeUserAddressEntry || olOutlookContactAddressEntry
      SmtpAddress := Recip.AddressEntry.GetExchangeUser.PrimarySmtpAddress
    else if (UserType = 1) ; olExchangeDistributionListAddressEntry
      SmtpAddress := Recip.AddressEntry.GetExchangeDistributionList.PrimarySmtpAddress
  }
  if (SmtpAddress = "")
    SmtpAddress := MailItem.SenderEmailAddress
}
MsgBox, % "SmtpAddress: " SmtpAddress
return
The weird thing is that this works for some exchange but not for others.

In some cases this line...

Code: Select all

Recip := olApp.Session.CreateRecipient(FromName)
...still produces an OU, but for other exchange users it does return the Primary SMTP address.

I can't figure out the difference between the accounts. We only have one domain and one exchange server. We model all accounts in the same way. So I can't see why is it that the script cannot resolve the primary SMTP for some users but it can for others.

Is there a more reliable way of getting the Primary SMTP address of the sender from an active outlook window?

Thanks.
User avatar
Micromegas
Posts: 260
Joined: 28 Apr 2015, 23:02
Location: Germany

Re: Mickers Outlook COM MSDN for Ahk_L

22 Oct 2017, 03:50

I'm not familiar with "premise exchange" or some of the other things you mention in your mail, but maybe it helps if I chime in what I did way back, when I had a problem that sounds similar to yours: Replying using a certain e-mail address. Since, just as in your case, it was not meant to be used by others, a quick-and-dirty solution sufficed. For that, I didn't use the OL Object model, but just called the dialog with keystrokes. I had to get used to waiting a second so as not to write text in the dialog box, but I grew used to that before I felt the need for a more professional solution.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Araphen, Draken and 313 guests