Read those image files in Outlook


May 22, 2008 – 5:10 pm by JP

I recently discovered a new object library that seems pretty useful - the Microsoft Office Document Imaging Type Library. From the MSDN site:

The Microsoft® Office Document Imaging 2003 (MODI) object model makes it possible to develop custom applications for managing document images (such as scanned and faxed documents) and the recognizable text that they contain.

This object model is not available in Office 2002(XP).

Continuing with the Outlook theme from my latest blog posts, I can already see the potential for automating Outlook by handling incoming emails, opening image file attachments, then filing, saving or forwarding them depending on the image content, or using Excel to browse through a folder and doing the same thing. I wrote a short sub that can show you how to read an image file that is sent to you via Outlook attachment.


First you must set a reference to the Microsoft Office Document Imaging 11.0 Type Library. Here's mine on the left. And below is the code.

VBA:
  1. Sub ReadAttachment()
  2.  
  3. Dim MyItem As Outlook.MailItem
  4. Dim MyAttach As Outlook.Attachments
  5. Dim MyDoc As MODI.Document
  6. Dim MyLayout As MODI.Layout
  7. Dim Att As String
  8. Dim strWords As String
  9. Dim int As Long
  10.  
  11.     On Error Resume Next
  12.     Select Case TypeName(Application.ActiveWindow)
  13.         Case “Explorer”
  14.             Set MyItem = ActiveExplorer.Selection.Item(1)
  15.         Case “Inspector”
  16.             Set MyItem = ActiveInspector.CurrentItem
  17.         Case Else
  18.     End Select
  19.     On Error GoTo 0
  20.  
  21.     If MyItem Is Nothing Then
  22.         GoTo ExitProc
  23.     End If

This bit of code should be familiar, it is the stock code I always use to get a reference to an item in a mail folder. Of course, the code assumes that you are acting on a mail item; to make sure, change "Dim MyItem As Outlook.MailItem" to "Dim MyItem As Object" and then wrap the rest of the code below in a "If MyItem.Class = olMail" statement. Or, just make sure you select or open a mail item before running this code.

VBA:
  1. Set MyAttach = MyItem.Attachments
  2.  
  3. If MyAttach.Count> 0 Then
  4.   Att = "C:\" & MyAttach.Item(1).DisplayName   
  5.   MyAttach.Item(1).SaveAsFile Att
  6.  
  7.   Set MyDoc = New MODI.Document
  8.   MyDoc.create (Att)
  9.   MyDoc.images(0).OCR
  10.  
  11.   Set MyLayout = MyDoc.images(0).Layout
  12.  
  13.   For int = 0 To (MyLayout.NumWords - 1)
  14.     strWords = strWords & " " & MyLayout.Words(int).Text   
  15.   Next int
  16.  
  17.   MsgBox strWords
  18.  
  19. Else   
  20.   GoTo ExitProc
  21. End If
  22.  
  23. ExitProc:
  24. Set MyItem = Nothing
  25. Set MyAttach = Nothing
  26. Set MyDoc = Nothing
  27. Set MyLayout = Nothing
  28.  
  29. On Error Resume Next   
  30. Kill Att
  31. On Error GoTo 0
  32.  
  33. End Sub

The rest of the code simply saves the attachment to a file on the hard drive, creates a new image document, then builds a string consisting of the readable characters in the image file. That string could be parsed to see if it contains something you are looking for, for example

VBA:
  1. If Instr(strWords, "My String")> 0 Then
  2. ' your code here
  3. End If

Enjoy,
JP


Share and Enjoy:
  • StumbleUpon
  • Technorati
  • Digg
  • Google
  • del.icio.us
  • MisterWong

Print This Post Print This Post  |  Email This Post Email This Post  |  Permalink  |  Subscribe to this feed Subscribe now!

Filed Under: Outlook, VBA, automation
Tags: , , , ,

This post has 191 views since May 22, 2008 – 5:10 pm.

Post a Comment

To post VBA code in your comment, use [VBA] tags, like this: [VBA]Code goes here[/VBA].





Subscribe without commenting

Keep Reading:

Browse Posts:


« Outlook Automated File Request System || Save Incoming Attachments, Choose Your Folder »