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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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.

25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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

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

Enjoy,
JP



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 109 views since May 22, 2008 – 5:10 pm.

Post a Comment

Browse Posts:


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