tag:blogger.com,1999:blog-284600789737488130.post2286098758884985472..comments2024-02-07T02:22:20.642-05:00Comments on Ruby on Windows: Automating Outlook with Ruby: Inbox & MessagesDavid Mullethttp://www.blogger.com/profile/05597492688893461137noreply@blogger.comBlogger15125tag:blogger.com,1999:blog-284600789737488130.post-29837021480925188002013-09-23T15:26:13.605-05:002013-09-23T15:26:13.605-05:00found a link for the microsoft ole outlook referen...found a link for the microsoft ole outlook reference for the default folder type, which might be helpful to people: http://msdn.microsoft.com/en-us/library/bb208072(v=office.12).aspxGliderhttps://www.blogger.com/profile/13702161228316362620noreply@blogger.comtag:blogger.com,1999:blog-284600789737488130.post-86451742980909352552012-06-04T09:41:46.631-05:002012-06-04T09:41:46.631-05:00David, I am trying to iterate over all the items i...David, I am trying to iterate over all the items in the inbox, but in a slightly different way i.e. passing the index of email as an argument to the "Items" method. In particular, I have written APIs to accomplish this:<br /><br />inbox = get_inbox_folder()<br />count = get_email_count(inbox)<br />email = get_email_item(inbox,count)<br />...and then later<br />subject = get_subject(email)<br />sender = get_sender(email)<br />I am expecting that i should get the latest email in 'email'... for some reason, I always get some older email (at index (count - 5) or something. <br />Iterating over the inbox with the following code:<br />inbox.items.each{|email|<br /> puts email.subject<br />}<br />confirms it that the email at index 'count' is indeed what I got with previous approach. Any ideas as to why this might be happening? I also followed similar approach for lotus notes and it turned out fine.Sdevikarhttps://www.blogger.com/profile/11069383402636500663noreply@blogger.comtag:blogger.com,1999:blog-284600789737488130.post-9409591217606295952011-10-31T23:49:07.252-05:002011-10-31T23:49:07.252-05:00Hi, I'm working on a project. I have to read e...Hi, I'm working on a project. I have to read email using Ruby program. <br /><br />Could you explain how I'd go about doing that? I tried researching but it seems that there is more help using RoR. <br /><br />Thanks!Tamanhttps://www.blogger.com/profile/09938790308388927963noreply@blogger.comtag:blogger.com,1999:blog-284600789737488130.post-2101516457071537642008-07-08T10:34:00.000-05:002008-07-08T10:34:00.000-05:00Thanks this was really appreciated.Just used your ...Thanks this was really appreciated.<BR/><BR/>Just used your code to move 6,900 messages from Outlook local sent mail to IMAP Gmail sent mail. <BR/><BR/>For anyone else looking to do it, I just added the Gmail IMAP account and then used the code above to tell Outlook to move the messages from the Outlook local folder to the Gmail IMAP connected folder in Outlook. <BR/><BR/>Add the line puts "Moved msg #{i}" to monitor progress.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-284600789737488130.post-39679691199587814242008-07-07T10:21:00.000-05:002008-07-07T10:21:00.000-05:00>>>When you say "move and entire tree of messages"...>>>When you say "move and entire tree of messages", do you mean you are moving messages AND subfolders -- or just messages?<<<<BR/><BR/>The move only applies to the messages. So I have these folders:<BR/><BR/>\Inbox<BR/>..\foo<BR/>..\bar<BR/><BR/>Our retention policy moves these to <BR/><BR/>\Managed Folders<BR/>..\Retention Review<BR/>....\Inbox<BR/>......\foo<BR/>......\bar <BR/><BR/>For each folder under retention review, I make sure that a like folder exists back in the original tree. Then I move the messages. I keep recursing down until all messages have been moved back.<BR/><BR/>When I am done, an empty folder structure exists under Retention Review. Hope this makes sense.<BR/><BR/>I don't think any of my runs ever got to 300 before flaking out, but I should mention, I have some folders with more than 1000 messages. Smaller folders didn't seem to create as much of a problem.<BR/><BR/>HHAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-284600789737488130.post-46984657338935659302008-06-28T14:49:00.000-05:002008-06-28T14:49:00.000-05:00@HH-Your code worked for me, moving over 300 messa...@HH-<BR/><BR/>Your code worked for me, moving over 300 messages (including attachments) between folders on my Exchange server, and moving messages between my server and my local Personal Folders.<BR/><BR/>When you say "move and entire tree of messages", do you mean you are moving messages AND subfolders -- or just messages?<BR/><BR/>DavidDavid Mullethttps://www.blogger.com/profile/05597492688893461137noreply@blogger.comtag:blogger.com,1999:blog-284600789737488130.post-69113346569963848422008-06-25T15:30:00.000-05:002008-06-25T15:30:00.000-05:00I am trying to write a ruby script to move an enti...I am trying to write a ruby script to move an entire tree of messages from one folder to another. This is to counter automatic cleanup that exchange server does.<BR/><BR/>Unfortunately, I can only move about 150 messages before outlook flakes out. Here is the routine I am using to move messages from one folder to another:<BR/><BR/>def archiveMessages(sourceFolder, destinationFolder)<BR/> sourceFolder.Items.Count.downto(1) do |i|<BR/> message = sourceFolder.Items(i)<BR/> message.Move(destinationFolder)<BR/> end<BR/>end<BR/><BR/>Any ideas what I am doing wrong? I have tried different flavors of this, but outlook flakes out on all of them. Once it flakes out, all ole commands on the folders fail.<BR/><BR/>The error I get is :<BR/>archiver.rb:43:in `method_missing': Move (WIN32OLERuntimeError)<BR/> OLE error code:4096 in Microsoft Office Outlook<BR/> The operation failed.<BR/> HRESULT error code:0x80004005<BR/><BR/>All suggestions are welcome!<BR/><BR/>HHAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-284600789737488130.post-53496132275610923332008-01-09T22:06:00.000-05:002008-01-09T22:06:00.000-05:00To get a reference to a sub-folder, call the paren...To get a reference to a sub-folder, call the parent folder's <B>Folders</B> method with the name of the sub-folder:<BR/><BR/>subfolder1 = inbox.Folders('My SubFolder')<BR/><BR/>subfolder2 = subfolder1.Folders('My Sub-SubFolder')<BR/><BR/>DavidDavid Mullethttps://www.blogger.com/profile/05597492688893461137noreply@blogger.comtag:blogger.com,1999:blog-284600789737488130.post-30484723223210729272008-01-09T17:01:00.000-05:002008-01-09T17:01:00.000-05:00How would you access a nested folder within outloo...How would you access a nested folder within outlook??<BR/><BR/>say I have a folder structure like<BR/><BR/>Inbox\Paul\todo<BR/><BR/>Thanks!! Good article!!<BR/><BR/>BTW in VBA I found this code snippet that Gets a folder based on a path if it does not exist it creates it then passes the folder object to the caller.... Is there a Ruby equiv to this??<BR/><BR/>Public Function GetFolder(strFolderPath As String) As MAPIFolder<BR/> ' folder path needs to be something like<BR/> ' "Public Folders\All Public Folders\Company\Sales"<BR/> Dim objApp As Outlook.Application<BR/> Dim objNS As Outlook.NameSpace<BR/> Dim colFolders As Outlook.Folders<BR/> Dim objFolder As Outlook.MAPIFolder<BR/> Dim arrFolders() As String<BR/> Dim I As Long<BR/> On Error Resume Next<BR/><BR/> strFolderPath = Replace(strFolderPath, "/", "\")<BR/> arrFolders() = Split(strFolderPath, "\")<BR/> Set objApp = CreateObject("Outlook.Application")<BR/> Set objNS = objApp.GetNamespace("MAPI")<BR/> Set objFolder = objNS.Folders.Item(arrFolders(0))<BR/> If Not objFolder Is Nothing Then<BR/> For I = 1 To UBound(arrFolders)<BR/> Set colFolders = objFolder.Folders<BR/> Set objFolder = Nothing<BR/> Set objFolder = colFolders.Item(arrFolders(I))<BR/> If objFolder Is Nothing Then<BR/> colFolders.Add (arrFolders(I))<BR/> Set objFolder = colFolders.Item(arrFolders(I))<BR/> 'Exit For<BR/> End If<BR/> Next<BR/> End If<BR/><BR/> Set GetFolder = objFolder<BR/> Set colFolders = Nothing<BR/> Set objNS = Nothing<BR/> Set objApp = Nothing<BR/>End FunctionAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-284600789737488130.post-33843836418175161172007-12-28T20:03:00.000-05:002007-12-28T20:03:00.000-05:00I would like to go into a subfolder under Inbox to...I would like to go into a subfolder under Inbox to get attachments.<BR/><BR/>So how would <BR/>inbox = mapi.GetDefaultFolder(6) apply in my case?<BR/><BR/>ThanksAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-284600789737488130.post-11413876391588179672007-10-18T19:54:00.000-05:002007-10-18T19:54:00.000-05:00@Greg:The body of the message can be accessed via ...@Greg:<BR/><BR/>The body of the message can be accessed via the Body property:<BR/><BR/>for item in inbox.Items<BR/> puts item.Body<BR/>end<BR/><BR/>The attachments are accessible via the message's Attachments collection. Each attachment has a FileName property and a SaveAsFile method:<BR/><BR/>for item in inbox.Items<BR/> for attachment in item.Attachments<BR/> attachment.SaveAsFile("C:\\Temp\\#{attachment.FileName}")<BR/> end<BR/>end<BR/><BR/>DavidDavid Mullethttps://www.blogger.com/profile/05597492688893461137noreply@blogger.comtag:blogger.com,1999:blog-284600789737488130.post-39397669389224352122007-10-17T09:35:00.000-05:002007-10-17T09:35:00.000-05:00Is there a way to read the contents of the message...Is there a way to read the contents of the message (not just the subject). I'm also trying to access any attachments that the message might have, is this possible through through Ruby WIN32OLE?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-284600789737488130.post-3830572846629019112007-09-07T19:55:00.000-05:002007-09-07T19:55:00.000-05:00@Emmanuel:Yes, I determined that the integer 6 rep...@Emmanuel:<BR/><BR/>Yes, I determined that the integer 6 represents the Inbox folder via code samples in books and online, such as Microsoft TechNet.<BR/><BR/>@Ana:<BR/><BR/>Excellent points, thanks! I updated my Inbox iteration example to use your method.<BR/><BR/>DavidDavid Mullethttps://www.blogger.com/profile/05597492688893461137noreply@blogger.comtag:blogger.com,1999:blog-284600789737488130.post-46540869997748731412007-09-05T07:22:00.000-05:002007-09-05T07:22:00.000-05:00I have found that moving items out of a folder whi...I have found that moving items out of a folder while iterating over the Items collection for that folder is problematic. Some of my messages weren't being processed, and I would have to run a script multiple times to fully process the target folder. I have instead switched to, e.g., <BR/><BR/>inbox.Items.Count.downto(1) do |i|<BR/> message = inbox.Items(i)<BR/> if message.Subject =~ /cardinals/i<BR/> message.Move(baseball_folder)<BR/> end<BR/>end<BR/><BR/>which systematically iterates over all messages even when some messages have been moved/deleted.<BR/><BR/>Also, I use WIN32OLE.connect('Outlook.Application') rather than new() since I found that my inbox processing rules were not running. A search revealed that this can happen when there are multiple instances of Outlook running, and indeed since I switched to connect() my rules have been running normally again.Ana Nelsonhttps://www.blogger.com/profile/07952731641901031606noreply@blogger.comtag:blogger.com,1999:blog-284600789737488130.post-11765380897877042782007-08-29T08:26:00.000-05:002007-08-29T08:26:00.000-05:00excellent! I'm going to try this soon. Thank you v...excellent! I'm going to try this soon. Thank you very much!<BR/><BR/>P.D.: <BR/><BR/>"inbox = mapi.GetDefaultFolder(6)"<BR/><BR/>How do you know is 6? Have you read it on Microsoft technet or something?Emmanuelhttps://www.blogger.com/profile/06017463228937154219noreply@blogger.com