Hotkey to copy subject of email in Outlook

Get help with using AutoHotkey and its commands and hotkeys
User avatar
coder_chick
Posts: 221
Joined: 05 Nov 2015, 10:43

Hotkey to copy subject of email in Outlook

05 Nov 2015, 11:00

I desperately need help with this. I currently have to highlight the subject and then I have another script that will copy my highlight and search for emails with that subject in the middle pane.

What I want is this:
1. Select the email in the middle pane. (This will be manual and done by the user)
2. Push a hotkey and have the script copy the subject from the email (This is what I need help with)
3. Have the copied email subject pasted in the search bar in the middle pane (I already have this part of the script done).

Can someone please help me with #2?!!!! I would also prefer that it ignore any "RE:" or "FW:" prefixes and just copy the rest of the subject.

Thanks!
♥ ❤ ❥ coder_chick ♥ ❤ ❥
User avatar
coder_chick
Posts: 221
Joined: 05 Nov 2015, 10:43

Re: Hotkey to copy subject of email in Outlook

05 Nov 2015, 11:04

Image
♥ ❤ ❥ coder_chick ♥ ❤ ❥
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Hotkey to copy subject of email in Outlook

05 Nov 2015, 11:20

Maybe:

Code: Select all

; Get the subject of the active item in Outlook. Works in both the main window and
; if the email is open in its own window.
olApp := ComObjActive("Outlook.Application")
olNamespace := olApp.GetNamespace("MAPI")
; Get the active window so we can determine if an Explorer or Inspector window is active.
Window := olApp.ActiveWindow 
if (Window.Class = 34) {  ; 34 = An Explorer object. (The Outlook main window)
      Selection := Window.Selection
      if (Selection.Count > 0)
            Clipboard := Selection.Item(1).Subject
}
else if (Window.Class = 35) ; 35 = An Inspector object. (The email is open in its own window)
      Clipboard := Window.CurrentItem.Subject

MsgBox, % Clipboard
return
I hope this helps :)
User avatar
AlphaBravo
Posts: 476
Joined: 29 Sep 2013, 22:59

Re: Hotkey to copy subject of email in Outlook

05 Nov 2015, 11:24

this also works to get subject, not sure how to set search

Code: Select all

oApp := ComObjActive("Outlook.Application")	; Get Outlook
oExp := oApp.ActiveExplorer					; Get the ActiveExplorer.
oSel := oExp.Selection						; Get the selection.
oItem := oSel.Item(1)						; Get a selected item.
MsgBox % Subject := oItem.subject			; Display subject
User avatar
coder_chick
Posts: 221
Joined: 05 Nov 2015, 10:43

Re: Hotkey to copy subject of email in Outlook

09 Nov 2015, 14:33

I am getting an error with that code. Any thoughts?

Image
♥ ❤ ❥ coder_chick ♥ ❤ ❥
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Hotkey to copy subject of email in Outlook

09 Nov 2015, 15:52

coder_chick wrote:I am getting an error with that code. Any thoughts?
Do you have Outlook open?
User avatar
coder_chick
Posts: 221
Joined: 05 Nov 2015, 10:43

Re: Hotkey to copy subject of email in Outlook

09 Nov 2015, 16:03

It seems to be working now. Maybe I had an old script running that was messing with it. However, what is the variable for the subject? If I want to paste it (using the Send,%subject%), how would i do that?
♥ ❤ ❥ coder_chick ♥ ❤ ❥
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Hotkey to copy subject of email in Outlook

09 Nov 2015, 16:14

In AlphaBravo's code, this line displays a MsgBox and stores the subject in the "Subject" variable at the same time:
MsgBox % Subject := oItem.subject ; Display subject

To Send instead of displaying the MsgBox, try replacing it with something like this:

Code: Select all

Subject := oItem.subject
SendInput, {Raw}%Subject%
User avatar
coder_chick
Posts: 221
Joined: 05 Nov 2015, 10:43

Re: Hotkey to copy subject of email in Outlook

09 Nov 2015, 16:32

It is not working now. I'm getting the same error as before. Is it working on your end? And yes, outlook is open. Running Outlook Professional Plus 2013.
♥ ❤ ❥ coder_chick ♥ ❤ ❥
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Hotkey to copy subject of email in Outlook

09 Nov 2015, 18:43

Yes it works for me in Outlook 2010.

Another way to get that error is if Outlook has been newly launched similar to what is described here. You can test if this is the case by temporarily activating a window in another application, then switch back to Outlook and test if your hotkey works.
User avatar
coder_chick
Posts: 221
Joined: 05 Nov 2015, 10:43

Re: Hotkey to copy subject of email in Outlook

09 Nov 2015, 19:28

Kon, thanks for the reply. I'm sorry. I'm a little bit of a n00b. Just a chick trying to learn all this coding stuff. Can you help me with some code that will perform that activating window test?
♥ ❤ ❥ coder_chick ♥ ❤ ❥
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Hotkey to copy subject of email in Outlook

09 Nov 2015, 20:45

Does the error occur only when you have just launched Outlook?
Does the error not occur if you minimize outlook, switch to another program, then switch back to outlook?
If you answered no to either of those questions then the problem you are experiencing is not what I am describing.

Hopefully someone else can offer more ideas regarding the cause of your error. Sorry, I'm out of ideas for now. :)
User avatar
coder_chick
Posts: 221
Joined: 05 Nov 2015, 10:43

Re: Hotkey to copy subject of email in Outlook

10 Nov 2015, 00:59

Error seems to always appear in both instances. :-(
♥ ❤ ❥ coder_chick ♥ ❤ ❥
User avatar
sinkfaze
Posts: 614
Joined: 01 Oct 2013, 08:01

Re: Hotkey to copy subject of email in Outlook

10 Nov 2015, 02:23

Are you running AutoHotkey as administrator?
User avatar
coder_chick
Posts: 221
Joined: 05 Nov 2015, 10:43

Re: Hotkey to copy subject of email in Outlook

10 Nov 2015, 09:47

Yes. Running as admin.
♥ ❤ ❥ coder_chick ♥ ❤ ❥
User avatar
coder_chick
Posts: 221
Joined: 05 Nov 2015, 10:43

Re: Hotkey to copy subject of email in Outlook

10 Nov 2015, 09:51

Ok, I think I may have figured out the issue. Sinkfaze got me thinking. First off, it was not working when I ran it through SciTE4AutoHotkey. I needed to compile it. Then, once compiled, it did not work when ran as admin, but ran fine when not as admin.
♥ ❤ ❥ coder_chick ♥ ❤ ❥
User avatar
coder_chick
Posts: 221
Joined: 05 Nov 2015, 10:43

Re: Hotkey to copy subject of email in Outlook

10 Nov 2015, 09:57

Ok, now we are on to something. The code is now working. Just needed to compile and not run as admin.

Code: Select all

F1::
oApp := ComObjActive("Outlook.Application")	; Get Outlook
oExp := oApp.ActiveExplorer					; Get the ActiveExplorer.
oSel := oExp.Selection						; Get the selection.
oItem := oSel.Item(1)						; Get a selected item.
Subject := oItem.subject
Send,^e ;shortcut to jump to search bar in Outlook
SendInput,"{Raw}%Subject%" ;sends subject to search bar in quotes
return
Next step, adding some logic that if the subject starts with RE: or FW: to trim that off before sending the subject to the search bar. Any thoughts on this?
♥ ❤ ❥ coder_chick ♥ ❤ ❥
User avatar
sinkfaze
Posts: 614
Joined: 01 Oct 2013, 08:01

Re: Hotkey to copy subject of email in Outlook

10 Nov 2015, 10:11

Code: Select all

F1::
oApp := ComObjActive("Outlook.Application")	; Get Outlook
oExp := oApp.ActiveExplorer					; Get the ActiveExplorer.
oSel := oExp.Selection						; Get the selection.
oItem := oSel.Item(1)						; Get a selected item.
Subject := RegExReplace(oItem.subject,"^(?:RE|FW): ")
Send,^e ;shortcut to jump to search bar in Outlook
SendInput,"{Raw}%Subject%" ;sends subject to search bar in quotes
return
aaffe
Posts: 158
Joined: 16 Jan 2014, 04:23

Re: Hotkey to copy subject of email in Outlook

10 Nov 2015, 10:12

Yes, you could use Regexreplace or Stringreplace.
User avatar
MasterFocus
Posts: 146
Joined: 01 Oct 2013, 09:47
GitHub: MasterFocus
Location: Rio de Janeiro - RJ - Brasil
Contact:

Re: Hotkey to copy subject of email in Outlook

10 Nov 2015, 10:13

sinkfaze gave you a fancy way of dealing with the prefixes, using RegEx.
Still, this could be easily implemented with things like InStr() and SubStr() (built-in functions).
I suggest you take a look at the documentation so you can do these minor tweaks by yourself. :)

(also, please avoid posting many consecutive posts so quickly - you can probably edit your last post if it's still "fresh" ;))
Antonio França - git.io | github.com | ahk4.net | sites.google.com
Member of the AHK community since 08/Apr/2009. Moderator since mid-2012.
Need help? Please post on the forum before sending me a PM.

Return to “Ask For Help”

Who is online

Users browsing this forum: anhnha, Chris117, GlynD, Jovannb, rhaos, Sefrautic, slupka and 42 guests