Outlook Copy and Search & Other Functions

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
Datapoint
Posts: 294
Joined: 18 Mar 2018, 17:06

Outlook Copy and Search & Other Functions

Post by Datapoint » 12 May 2021, 13:44

Outlook Copy and Search
This is probably my most-used hotkey. It copies the text that is selected and then searches in MS Outlook.

Code: Select all

; Microsoft Outlook hotkeys (WinExist)
#IfWinExist, ahk_class rctrl_renwnd32

; Copy the selection and find an exact match (enclosed in quotes with "*" at the end) in Outlook
^F11::OutlookCopyAndSearch()
    
OutlookCopyAndSearch()
{
    static olSearchScopeAllFolders := 1
    
    Clipboard := ""
    Send, ^c
    ClipWait, 1, 1
    if ErrorLevel
        return
    ComObjActive("Outlook.Application").ActiveExplorer.Search("""" Clipboard "*""", olSearchScopeAllFolders)
    ; Activate the Outlook window
    WinActivate, ahk_class rctrl_renwnd32
    ; Send Tab to hide the "suggested searches" drop down
    ControlSend, RICHEDIT60W1, {Tab}, ahk_class rctrl_renwnd32
}
Outlook Add Caption and Shadow
With an image selected, add a caption and shadow.

Code: Select all

; Microsoft Outlook hotkeys (WinActive)
#IfWinActive ahk_class rctrl_renwnd32

; With an image selected, press F8 to add a caption and shadow
F8::OutlookAddCaptionAndShadow()

OutlookAddCaptionAndShadow()
{
    static olExplorer := 34
    static olInspector := 35
    static wdSelectionInlineShape := 7
    static msoShadow25 := 25
    
    ; Get the Word Editor Application of the email that is being written
    myWindow := ComObjActive("Outlook.Application").ActiveWindow
    if (myWindow.Class = olExplorer) ; The main Outlook window
        try wdApp := myWindow.ActiveInlineResponseWordEditor.Application
    else if (myWindow.Class = olInspector) ; The email is open in its own window
    {
        ; Check if the current item is being edited
        if (MyWindow.CurrentItem.Sent = 0)
            wdApp := myWindow.WordEditor.Application
    }
    ; Add a shadow and caption to the selected image
    if (wdApp.Selection.Type = wdSelectionInlineShape)
    {
        wdApp.Selection.InlineShapes.Item(1).Shadow.Type := msoShadow25
        ; .InsertCaption changes the selection, so you can't use .Selection to refer to the image after this
        wdApp.Selection.InsertCaption("Figure")
    }
}
Outlook Add Attachment

Code: Select all

F9::OutlookAddAttachment("C:\Test\New Text Document.txt")

OutlookAddAttachment(File)
{
    static olExplorer := 34
    static olInspector := 35

    ; Get the email that is being written
    myWindow := ComObjActive("Outlook.Application").ActiveWindow
    if (myWindow.Class = olExplorer) ; The main Outlook window
        olItem := myWindow.ActiveInlineResponse
    else if (myWindow.Class = olInspector) ; The email is open in its own window
        olItem := myWindow.CurrentItem
    ; Attach the file
    olItem.Attachments.Add(File)
}
Outlook Create Email
Create an email and add a subject, body, and recipients.

Code: Select all

; Example usage
OutlookCreateEmail("This is the subject.", "This is the body.", ["Jane Smith", "Jill Wilson", "[email protected]"])
ExitApp

; OutlookCreateEmail
;   Subject (String)    Email subject.
;   Body    (String)    Email Body.
;   Recips  (Object)    An array containing names or email addresses to add as recipients. Names are attempted to be 
;                       resolved against the Outlook Address Book. Email addresses seem to be resolved as long as they 
;                       are valid. Duplicate email addresses will not be added twice, so you can attempt to resolve the 
;                       name and if that fails you can use the email. (Place names before emails in the array so that 
;                       when the array is enumerated the names are resolved before the email addresses.)
OutlookCreateEmail(Subject, Body, Recips)
{
    static olMailItem := 0
    
    try oApp := ComObjActive("Outlook.Application")
    catch
    {
        MsgBox, 0x40, New Email Error, Failed to connect to Outlook.`nOutlook must be running first.
        return
    }
    oMailItem := oApp.CreateItem(olMailItem)    
    oMailItem.Subject := Subject
    oMailItem.Body := Body
    ; Add each recipient and attempt to resolve it against the Address Book.
    Added := {} ; Record added emails to prevent duplicates
    for i, Recip in Recips
    {
        if (Recip = "")
            continue
        oRecip := oMailItem.Recipients.Add(Recip)
        oRecip.Resolve
        ; Deletes duplicate recipients or ones that are not resolved
        if (!oRecip.Resolved) || Added[oRecip.Address]
            oRecip.Delete
        else
            Added[oRecip.Address] := true
    }
    oMailItem.Display
    ; Fix paragraph spacing
    oParFormat := oMailItem.GetInspector.WordEditor.Range.ParagraphFormat
    oParFormat.SpaceBeforeAuto := 0
    oParFormat.SpaceAfterAuto := 0
    oParFormat.SpaceBefore := 0
    oParFormat.SpaceAfter := 0
}
Outlook Events
When an Outlook application event is fired this script shows the name of the event and info about the parameters.
Note: Try Outlook Rules before resorting to events.

Code: Select all

#Persistent
olApp := ComObjActive("Outlook.Application")
ComObjConnect(olApp, new Outlook_Events)
return

class Outlook_Events
{
    __Call(Event, Args*)
    {
        EventInfo := ""
        for i, val in Args
        {
            if (Format("{:X}", ComObjType(val)) = 0)
                EventInfo .= "Arg#: "       i
                           . "`nNot an object"
                           . "`nValue: "    val "`n`n"
            else
                EventInfo .= "Arg#: "       i
                           . "`nVarType: "  Format("{:X}", ComObjType(val))
                           . "`nIName: "    ComObjType(val, "Name")
                           . "`nIID: "      ComObjType(val, "IID")
                           . "`nCName: "    ComObjType(val, "Class")
                           . "`nCLSID: "    ComObjType(val, "CLSID") "`n`n"
        }
        MsgBox,, % "Application." Event " - " Args[Args.MaxIndex()].Name, % EventInfo
    }
    
    ; Define methods here to respond to specific events
    
;    static olFolderInbox := 6
;    
;    ; Example method fired when new mail is received in the inbox. This will assign the mail item to categories, 
;    ; mark it as read, and show a MsgBox with the subject.
;    NewMailEx(EntryIDCollection, olApp)
;    {
;        olns := olApp.Session ; Namespace
;        olFolder := olns.GetDefaultFolder(Outlook_Events.olFolderInbox) ; Get the Inbox folder
;        StoreID := olFolder.StoreID ; Get the StoreID, which is a property of the folder. 
;        olItem := olns.GetItemFromID(EntryIDCollection, StoreID) ; Get the MailItem object from the entry ID
;        olItem.Categories := "Yellow Category, Blue Category" ; Assign categories to the mail item
;        olItem.Unread := 0 ; Mark as read
;        olItem.Save
;        MsgBox % olItem.Subject
;    }
}
Last edited by Datapoint on 20 Jan 2023, 14:18, edited 13 times in total.

likethevegetable
Posts: 81
Joined: 05 May 2021, 08:54

Re: Outlook Copy and Search

Post by likethevegetable » 18 May 2021, 18:23

Nice share! I'm going to incorporate this in my quick search pop-up window I've made.

shonilchi
Posts: 2
Joined: 14 Nov 2019, 06:25

Re: Outlook Copy and Search

Post by shonilchi » 20 Jun 2021, 12:39

likethevegetable wrote:
18 May 2021, 18:23
Nice share! I'm going to incorporate this in my quick search pop-up window I've made.
Could you please share that quick search pop-up for this? it would be a great deal for me.. thanks in advance.

likethevegetable
Posts: 81
Joined: 05 May 2021, 08:54

Re: Outlook Copy and Search

Post by likethevegetable » 23 Sep 2021, 20:11

shonilchi wrote:
20 Jun 2021, 12:39
likethevegetable wrote:
18 May 2021, 18:23
Nice share! I'm going to incorporate this in my quick search pop-up window I've made.
Could you please share that quick search pop-up for this? it would be a great deal for me.. thanks in advance.
So sorry for the late reply. I haven't been on in a while. I based it off of this, but made some improvments: https://github.com/TheBestPessimist/AutoHotKey-Launcher

I'm going to make a GitHub repo of it soon (I have some private data in it I need to clean out first).

Post Reply

Return to “Scripts and Functions (v1)”