Click here to Skip to main content
15,886,002 members
Articles / Operating Systems / Windows

The Poetry of Ruby - Jumping to an Outlook Folder from the DOS Console

Rate me:
Please Sign up or sign in to vote.
3.80/5 (7 votes)
17 Aug 2007CPOL 30.8K   44   6   5
The poetry of Ruby - jumping to an Outlook folder from the DOS console.

Introduction

This article is offered to you as a short simple poem written in Ruby. It is by no means a masterpiece, but utility in the smallest of circumstances could cause a shift of balance in popular, everyday life.

The Purpose

Imagine you work with the Outlook program running on Windows. You might have a lot of folders in your personal store because you respect yourself as an organized and thorough professional? Perhaps you are so desperate sometimes to find these folders that you are even willing to open a command line "DOS" console if someone suggested it might help?

The Script

Running the following little Ruby program from the command line and passing at least a part of the folder name you would like to locate as an argument, should find the first folder with such a name. If so, it will ask Outlook to jump to the folder and display itself.

Sample image

Ruby
# //////////////////////////////////////////////////////////////////////////////
# // file: pgog.rb
# // desc: pgog 'perfectly good outlook grep' - outlook folder scanning utility
# // auth: pieter greyling [-pg-] <a href="mailto:pieter@demonsource.com">pieter at demonsource dot com
</a># // copy: pieter greyling<a href="mailto:pieter@demonsource.com">
</a># // free/public domain/credit mention mandatory for all uses
# // date: 2006.07
# // hist: [-pg-]/2006.07/created
# // like: 
# // > pgog inbox
# // -- find folder : inbox
# // -- outlook version : 11.0.0.6568
# // -- start folder : PGREYLING
# // -- found [inbox] in [Inbox]
# // ...OR...
# // > pgog specs
# // -- find folder : specs
# // -- outlook version : 11.0.0.6568
# // -- start folder : PGREYLING
# // -- found [specs] in [__SPECS__]
# // ...OR...
# // > pgog toolatetonight
# // -- find folder : toolatetonight
# // -- outlook version : 11.0.0.6568
# // -- start folder : PGREYLING
# // -- folder "toolatetonight" not found
# //////////////////////////////////////////////////////
require 'win32ole'
# //-- members in the main namespace--------------------
@outlook_server = "Outlook.Application"
# //-- globals -----------------------------------------

module Outlook  
    OlFolderCalendar = 9
    OlFolderContacts = 10
    OlFolderDeletedItems = 3
    OlFolderDrafts = 16
    OlFolderInbox = 6
    OlFolderJournal = 11
    OlFolderNotes = 12
    OlFolderOutbox = 4
    OlFolderSentMail = 5
    OlFolderTasks = 13
    VbBinaryCompare = 0
    VbTextCompare = 1
end

# //----------------------------------------------------
# // get parent of inbox folder

def getoutlook
    begin
        WIN32OLE.connect(@outlook_server)
    rescue
        WIN32OLE.new(@outlook_server)
    end
end

# //----------------------------------------------------
# // mapi namespace

def getmapinamespace
    getoutlook.GetNameSpace('mapi')
end

# //----------------------------------------------------
# // root folder of personal folders

def getparentfolder_of_personalfolders
    getmapinamespace.folders('Personal Folders')
end

# //----------------------------------------------------
# // get parent of inbox folder

def getparentfolder_of_inbox
    getmapinamespace.GetDefaultFolder(<a href="outlook::OlFolderInbox%29.Parentend">Outlook::OlFolderInbox).Parent
end

</a># //----------------------------------------------------
# // recursively dump all sub-folder names

def dumptree(oparentfolder)
    ofolders=oparentfolder.Folders
    ifolders=ofolders.count
    ofolder=ofolders.getFirst
        1.upto(ifolders) do |i|
            puts '-- ' + i.to_s + ': ' + ofolder.Name
            if 0 < ofolder.folders.count then dumptree(ofolder) end
            ofolder=ofolders.getNext
        end
end

# //----------------------------------------------------
# // recursively search sub-folder names

def searchtree(oparentfolder, sfind)

    sfolder='' #// folder name
    tfolder=nil #// work folder
    ffolder=nil #// found folder must be nothing to begin
    sfind=sfind.downcase #// no case
    ofolders=oparentfolder.Folders
    ifolders=ofolders.count
    ofolder=ofolders.getFirst

    1.upto(ifolders) do |i|
        sfolder=ofolder.Name.downcase #// no case
        if not sfolder.index(sfind) #// not this one?
            if 0 < ofolder.folders.count #// search the children, if we have any
                tfolder=searchtree(ofolder,sfind) 
                if tfolder #// found a match, break out
                    ffolder=tfolder
                    return ffolder
                end
            end
        else #// found a match, break out
            ffolder=ofolder
            return ffolder
        end
        ofolder=ofolders.getNext #// keep going...
    end
    return ffolder
end

# //----------------------------------------------------
# // try to determine where to start recursing

def choosestartfolder() 
    begin
        getparentfolder_of_personalfolders()
    rescue
        getparentfolder_of_inbox()
    else
        nil
    end
end

# //-- MAIN --------------------------------------------

folder_to_find = ARGV[0]
otl = getoutlook
puts '-- find folder : ' + folder_to_find
puts '-- outlook version : ' + otl.version
puts '-- start folder : ' + choosestartfolder.name
if tfolder = searchtree( choosestartfolder(), folder_to_find )
    puts '-- found [' + folder_to_find + '] in [' + tfolder.Name + ']'
    otl.ActiveExplorer.CurrentFolder = tfolder;
else
    printf '-- folder "%s" not found', folder_to_find
end

# //////////////////////////////////////////////////////
# // ends: pgog.rb
# //////////////////////////////////////////////////////

The Result

Should be a hop to the first folder with the string "_of_ruby" in its name.

Sample image

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer ChileServe
Chile Chile
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionhow can I identify that an email is added to a folder Pin
Einas3-Mar-08 4:26
Einas3-Mar-08 4:26 
GeneralExcellent Pin
jantzeno9-Aug-07 11:50
jantzeno9-Aug-07 11:50 
GeneralRe: Excellent Pin
Pieter H. C. Greyling10-Aug-07 9:10
Pieter H. C. Greyling10-Aug-07 9:10 
GeneralIt would be nice ... Pin
Roland Pibinger1-Nov-06 23:08
Roland Pibinger1-Nov-06 23:08 
GeneralRe: It would be nice ... Pin
Pieter H. C. Greyling5-Nov-06 6:58
Pieter H. C. Greyling5-Nov-06 6:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.