Question
October 23, 2007 at 08:41 AM
rucas

VBA – OCR Automation

by rucas . Updated 18 years, 5 months ago

I am having a problem with a Word VBA macro that is supposed to automate an OCR(optical character recognition) task described as follows:

1)Open Paint
2)Paste the selected picture to Paint
3)Save it as a tif in temp folder
4)Close Paint
5)Open MSPVIEW.EXE (Microsoft Office Document Imaging) with this picture
6)Perform a OCR scan and send it to word (Ctrl+t)

I post here the code I have that’s giving me the headaches, but first let me say that the problem is at the end when I send keys to MSPVIEW and this application refuses to come to foregroud even with AppActivate.

—-Begin CODE—–Begin CODE——————
Private Declare Sub Sleep Lib “kernel32” (ByVal dwMilliseconds As Long)

Private Declare Sub keybd_event Lib “user32” (ByVal bVk As Byte, ByVal _
bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Declare Function GetActiveWindow Lib “user32” () As Long
Private Declare Function SetActiveWindow Lib “user32” (ByVal hwnd As Long) As Long

Private Sub cmd_OCR_Click()
Dim RetVal ‘ mspview.exe ProcessID
RetVal = Shell(“C:\Windows\system32\mspaint.exe”, 1)
Sleep (1000)
Tasks(“Paint”).Activate
Tasks(“Paint”).WindowState = wdWindowStateNormal

SendKeys “^v”, True ‘ Paste clipboard content into Paint.
SendKeys “^s”, True ‘ Save file
SendKeys “C:\Documents and Settings\You\Local Settings\Temp\eraseThis”
SendKeys “{TAB}”, True
SendKeys “t”, True
SendKeys “{ENTER}”, True

‘Check to see if overwrite?s needed -> filesearch for eraseThis.tif
With Application.FileSearch
.NewSearch
.LookIn = “C:\Documents and Settings\You\Local Settings\Temp”
.SearchSubFolders = True
.FileName = ” eraseThis.tif”
.MatchTextExactly = True
.FileType = msoFileTypeAllFiles
.Execute
End With
If Application.FileSearch.FoundFiles.Count > 0 Then
SendKeys “y” ‘confirm overwrite
End If

‘Close paint
Tasks(“Paint”).Close

‘ Open mspview.exe
ocrPrg = “C:\Program Files\Common Files\Microsoft Shared\MODI\11.0\MSPVIEW.EXE ” & _
Chr(34) & “C:\Documents and Settings\You\Local Settings\Temp\ eraseThis.tif” & Chr(34)
Set objWMIService = GetObject(“winmgmts:\\.\root\cimv2”)
Set objConfig = objWMIService.Get(“Win32_ProcessStartup”).SpawnInstance_
objConfig.ShowWindow = 5 ‘5 => Show Window SW_SHOW
Call objWMIService.Get(“Win32_Process”).Create(ocrPrg, Null, Null, ProcID_MSPVIEW)
For Each objView In GetObject(“winmgmts:\\.\root\cimv2”). _
ExecQuery(“SELECT * FROM Win32_Process WHERE ProcessId=” & ProcID_MSPVIEW)
CreateObject(“WScript.Shell”).AppActivate objView.ProcessID
Next

‘Send Keys to MSPVIEW -> Lib “user32” -> Win32 API
Const KEYEVENTF_KEYUP = &H2
Const VK_MENU = &H12 ‘ ALT
Const VK_CONTROL = &H11
SetActiveWindow (GetActiveWindow())
keybd_event VK_CONTROL, 0, 0, 0
keybd_event 116, 0, 0, 0 ‘116 = decimal ASCII for ?t?
keybd_event 116, 0, KEYEVENTF_KEYUP, 0
keybd_event VK_CONTROL, 0, KEYEVENTF_KEYUP, 0

‘Close MSPVIEW.EXE -> using WMI, WSH
For Each objView In GetObject(“winmgmts:\\.\root\cimv2”). _
ExecQuery(“SELECT * FROM Win32_Process WHERE ProcessId=” & RetValView)
objView.Terminate
Next

End Sub
—-End CODE—–End CODE———————-

This code’s a bit schizophrenic since it uses WMI, VBA and Win32 API. This was due to several attemps of doing the same things differently trying to correct the problem.

Funny thing is that if I open Notepad instead of MSPVIEW the application activates and receives the keys sent to it. If MSPVIEW is used it stays behing the form that has the command button that triggers the code.

Hope someone helps me.

Thanks

Rui Carvalho

This discussion is locked

All Comments