65.9K
CodeProject is changing. Read more.
Home

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

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.80/5 (7 votes)

Nov 1, 2006

CPOL
viewsIcon

31138

downloadIcon

45

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

# //////////////////////////////////////////////////////////////////////////////
# // file: pgog.rb
# // desc: pgog 'perfectly good outlook grep' - outlook folder scanning utility
# // auth: pieter greyling [-pg-] pieter at demonsource dot com
# // copy: pieter greyling
# // 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(Outlook::OlFolderInbox).Parent
end

# //----------------------------------------------------
# // 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