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.


Sub ReadAttachment()

Dim MyItem As Outlook.MailItem
Dim MyAttach As Outlook.Attachments
Dim MyDoc As MODI.Document
Dim MyLayout As MODI.Layout
Dim Att As String
Dim strWords As String
Dim int As Long

    On Error Resume Next
    Select Case TypeName(Application.ActiveWindow)
        Case “Explorer”
            Set MyItem = ActiveExplorer.Selection.Item(1)
        Case “Inspector”
            Set MyItem = ActiveInspector.CurrentItem
        Case Else
    End Select
    On Error GoTo 0

    If MyItem Is Nothing Then
        GoTo ExitProc
    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.


Set MyAttach = MyItem.Attachments

If MyAttach.Count > 0 Then
  Att = "C:\" & MyAttach.Item(1).DisplayName
  MyAttach.Item(1).SaveAsFile Att

  Set MyDoc = New MODI.Document
  MyDoc.create (Att)
  MyDoc.images(0).OCR

  Set MyLayout = MyDoc.images(0).Layout

  For int = 0 To (MyLayout.NumWords - 1)
    strWords = strWords & " " & MyLayout.Words(int).Text
  Next int

  MsgBox strWords

Else
  GoTo ExitProc
End If

ExitProc:
Set MyItem = Nothing
Set MyAttach = Nothing
Set MyDoc = Nothing
Set MyLayout = Nothing

On Error Resume Next
Kill Att
On Error GoTo 0

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


If Instr(strWords, "My String") > 0 Then
' your code here
End If

Enjoy,
JP


If you enjoyed this page, share it!
    StumbleUpon Technorati Digg Google del.icio.us MisterWong TwitThis

Print This Post Print This Post  |  Email This Post Email This Post  |  rss Subscribe to Posts Feed  |  rss Subscribe to Comments

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

Post a Comment


Certain comments (including first-time comments) are subject to moderation and will not appear immediately. You can use HTML tags in your comment. If you include a greater-than or less-than sign or anything else that could be interpreted as HTML, you need to escape those characters. 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 »