Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scan multiple pages with ADF scanner using VBA

I am writing a Microsoft Access application and I want to enable the user to scan multiple pages to a single PDF format. The conversion to PDF works fine once I have all the pages scanned. Here's my code:

Option Compare Database
Option Explicit

Const WIA_FORMAT_JPEG = "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}"

Public Function MyScan()
  Dim ComDialog As WIA.CommonDialog
  Dim DevMgr As WIA.DeviceManager
  Dim DevInfo As WIA.DeviceInfo
  Dim dev As WIA.Device
  Dim img As WIA.ImageFile
  Dim i As Integer
  Dim wiaScanner As WIA.Device

  Set ComDialog = New WIA.CommonDialog
  Set wiaScanner = ComDialog.ShowSelectDevice(WiaDeviceType.UnspecifiedDeviceType, False, True)

  Set DevMgr = New WIA.DeviceManager

  For i = 1 To DevMgr.DeviceInfos().Count
    If DevMgr.DeviceInfos(i).DeviceID = wiaScanner.DeviceID Then
      Set DevInfo = DevMgr.DeviceInfos(i)
    End If
  Next i

  Set dev = DevInfo.Connect

  Set img = dev.Items(1).Transfer(WIA_FORMAT_JPEG)

  img.SaveFile "C:\img.jpg"

  Set img = Nothing
  Set dev = Nothing
  Set DevInfo = Nothing
  Set DevMgr = Nothing
  Set ComDialog = Nothing


End Function

Of course it is important to say that my scanner is Avision AV121 with an automatic document feeder.

My problem is that Set img = dev.Items(1).Transfer(WIA_FORMAT_JPEG) scans ALL the pages at once (and not just a single page) but I only see the first one in the image file. Because all the pages are scanned at once, I can't do it in a loop - an error will be raised in the second iteration (saying that the feeder is empty as it really is) and I still only have the first page scanned.

I would like to state that this seems to be a common problem. I've read a lot of threads regarding this problem, but didn't find anything that answered my question.

I hope to find help here, I am really frustrated.

Many thanks

like image 223
Yotam Avatar asked Oct 21 '22 08:10

Yotam


1 Answers

For anyone still working on the problem, I modified this code from JIM's code to work with a scanner with a ADF. It scans the documents continuously unlimit pages and stores them as a jpeg file temporarily. It then outputs a report to a pdf. This is the only way I can figure out scanning multiple documents using an ADF scanner.


'Requirements:
'Must include reference to Microsoft Windows Image Acquisition 2.0 dll
'Create a table named scantemp. Create ID column as Autonumber. Create 2nd column named Picture with Text as datatype.
'Create a continuous report named rptscan. Set scantemp table as recordsource. Add image control to report and set Picture
'as the control source. Make the image control the size of an 8.5 x 11 sheet so that the whole document appears normally when the
'create textbox set name txt_id for enter PDF files name
'report is exported to pdf.
'For use with a scanner that continually scans documents until the ADF tray is empty unlimit pages.

option Compare Database
Option Explicit
Const WIA_FORMAT_JPEG = "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}"

Public Sub ScanDocs()

    Dim intPages As Integer 'number of pages
    Dim img As WIA.ImageFile 
    Dim strPath As String 
    Dim strFileJPG As String

    strPath = CurrentProject.Path 'set path to save files
    intPages = 1


On Error GoTo ErrorHandler

 'scan
ScanStrat:

    Dim DialogScan As New WIA.CommonDialog, dpi As Integer, pp As Integer, l    As Integer
    dpi = 250
    Dim Scanner As WIA.Device
    Set Scanner = DialogScan.ShowSelectDevice(WIA.WiaDeviceType.ScannerDeviceType, False, False)

    'set properties device
        Scanner.Properties("3088").Value = 1 'Automatic Document Feeder
        Scanner.Items(1).Properties("6146").Value = 4 'Colour intent
        Scanner.Items(1).Properties("6147").Value = dpi 'DPI horizontal
        Scanner.Items(1).Properties("6148").Value = dpi 'DPI vertical
        Scanner.Items(1).Properties("6149").Value = 0 'x point to start scan
        Scanner.Items(1).Properties("6150").Value = 0 'y point to start scan
        Scanner.Items(1).Properties("6151").Value = 8.27 * dpi  'Horizontal extent
        Scanner.Items(1).Properties("6152").Value = 11.7 * dpi    'Vertical extent for A4
        Scanner.Items(1).Properties("6154").Value = 80 'brightness
      '  Scanner.Items(1).Properties("6155").Value = 30 'contrast

 'Start Scan if err number -2145320957 Scan document finish 

    Do While Err.Number <> -2145320957 'error number is ADF status don't feed document

        Set img = Scanner.Items(1).Transfer(WIA_FORMAT_JPEG) 
        strFileJPG = strPath & "\FileScan\temp\" & CStr(intPages) & ".jpg"
        img.SaveFile (strFileJPG) 'save files .jpg in temp folder
        DoCmd.SetWarnings False 
       DoCmd.RunSQL "insert into scantemp (picture) values ('" & strFileJPG & "')" 'insert picture temp to table scan temp

        intPages = intPages + 1 'add number pages
   Loop

'after finish scan start convert to pdf
StartPDFConversion: 

    Dim strFilePDF As String '
    Dim RptName As String
    strFilePDF = CurrentProject.Path & "\FileScan\" & txt_id.Value & ".pdf" 'pdf file name by textbox
    RptName = "rptScan" 'report picture file for export to PDF 
    DoCmd.OpenReport RptName, acViewDesign, , , acHidden
    DoCmd.Close acReport, RptName, acSaveYes
    DoCmd.OutputTo acOutputReport, RptName, acFormatPDF, strFilePDF
    DoCmd.RunSQL "delete from scantemp" 'delete all data from table scantemp



DeleteTemp:
'delete files temp (JPG)
Dim i As Integer
Dim filesname As String
i = 1

'loop pages number (intpages)
Do While i < intPages
    filesname = CurrentProject.Path & "\FileScan\temp\" & i & ".jpg"

    If Dir(filesname) <> "" Then
        'SetAttr filesname, vbNormal
        Kill filesname
    Else
        Exit Do
    End If
    i = i + 1
Loop


MsgBox ("done")
    Exit Sub


ErrorHandler:
Select Case Err.Number
    Case -2145320957
    If intPages = 1 Then
        MsgBox ("not found document to scan")
        Exit Sub
    Else
      GoTo StartPDFConversion
      End If
    End Select


 MsgBox "Error" & ":  " & Err.Number & vbCrLf & "Description: " _
    & Err.Description, vbExclamation, Me.Name & ".ScanDocs"
End Sub
like image 57
chalermpon Avatar answered Nov 03 '22 22:11

chalermpon