Page 1 of 2

Hotkey to copy subject of email in Outlook

Posted: 05 Nov 2015, 11:00
by coder_chick
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!

Re: Hotkey to copy subject of email in Outlook

Posted: 05 Nov 2015, 11:04
by coder_chick
Image

Re: Hotkey to copy subject of email in Outlook

Posted: 05 Nov 2015, 11:20
by kon
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 :)

Re: Hotkey to copy subject of email in Outlook

Posted: 05 Nov 2015, 11:24
by AlphaBravo
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

Re: Hotkey to copy subject of email in Outlook

Posted: 09 Nov 2015, 14:33
by coder_chick
I am getting an error with that code. Any thoughts?

Image

Re: Hotkey to copy subject of email in Outlook

Posted: 09 Nov 2015, 15:52
by kon
coder_chick wrote:I am getting an error with that code. Any thoughts?
Do you have Outlook open?

Re: Hotkey to copy subject of email in Outlook

Posted: 09 Nov 2015, 16:03
by coder_chick
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?

Re: Hotkey to copy subject of email in Outlook

Posted: 09 Nov 2015, 16:14
by kon
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%

Re: Hotkey to copy subject of email in Outlook

Posted: 09 Nov 2015, 16:32
by coder_chick
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.

Re: Hotkey to copy subject of email in Outlook

Posted: 09 Nov 2015, 18:43
by kon
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.

Re: Hotkey to copy subject of email in Outlook

Posted: 09 Nov 2015, 19:28
by coder_chick
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?

Re: Hotkey to copy subject of email in Outlook

Posted: 09 Nov 2015, 20:45
by kon
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. :)

Re: Hotkey to copy subject of email in Outlook

Posted: 10 Nov 2015, 00:59
by coder_chick
Error seems to always appear in both instances. :-(

Re: Hotkey to copy subject of email in Outlook

Posted: 10 Nov 2015, 02:23
by sinkfaze
Are you running AutoHotkey as administrator?

Re: Hotkey to copy subject of email in Outlook

Posted: 10 Nov 2015, 09:47
by coder_chick
Yes. Running as admin.

Re: Hotkey to copy subject of email in Outlook

Posted: 10 Nov 2015, 09:51
by coder_chick
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.

Re: Hotkey to copy subject of email in Outlook

Posted: 10 Nov 2015, 09:57
by coder_chick
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?

Re: Hotkey to copy subject of email in Outlook

Posted: 10 Nov 2015, 10:11
by sinkfaze

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

Re: Hotkey to copy subject of email in Outlook

Posted: 10 Nov 2015, 10:12
by aaffe
Yes, you could use Regexreplace or Stringreplace.

Re: Hotkey to copy subject of email in Outlook

Posted: 10 Nov 2015, 10:13
by MasterFocus
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" ;))