2

I have written a module in Excel VBA using MdlTwain but it seems to only be able to scan one page at a time, whereas if I open the Epson Scan Tool it will scan all the pages.

Pressing the button to activate the Macro on the excel sheet brings up this box for the user to select the type of document they are scanning (this is so I can automatically save the file to the correct folder).

Type of Document

After selecting a document a box comes up asking how many pages you are scanning. This is what I am trying to get rid of by having the Automatic Document Feeder scan all the pages at once automatically.

How many pages?

Here is the code for the module.

Sub scanWithMdlTwain()
Application = False
FIF = False
ID = False
CancelClicked = False


Dim Test As Long

Scan.Show
If CancelClicked = True Then Exit Sub

ScanEmpty = True
Do While ScanEmpty = True
    ScanEmpty = True
    Pages.Show
    If CancelClicked = True Then Exit Sub
    If ScanEmpty = True Then
        MsgBox "You must enter the number of pages you are scanning.", vbOKOnly
    End If
Loop

Dim i As Integer
For i = 1 To numOfPages
    Test = mdlTwain.TransferWithoutUI(300, BW, 0, 0, 8.5, 11, "Scan" & i & ".jpg")
Next i
End Sub

And for reference, here is the mdlTwain code for TransferWithoutUI (which I didn't write, but I can modify).

Public Function TransferWithoutUI(ByVal sngResolution As Single, _
                              ByVal tColourType As TWAIN_MDL_COLOURTYPE, _
                              ByVal sngImageLeft As Single, _
                              ByVal sngImageTop As Single, _
                              ByVal sngImageRight As Single, _
                              ByVal sngImageBottom As Single, _
                              ByVal sBMPFileName As String) As Long

'----------------------------------------------------------------------------
' Function transfers one image from Twain data source without showing
'   the data source user interface (silent transfer).
'
' Input values
'   - sngResolution (Single) - resolution of the image in DPI
'                              (dots per inch)
'   - tColourType (UDT) - colour depth of the imaged - monochromatic (BW),
'                         colours of grey (GREY), full colours (COLOUR)
'   - sngImageLeft, sngImageTop, sngImageRight, sngImageBottom (Single) -
'       values determine the rectangle on the scanner glass that will
'       be scanned (default units are inches) - if you set Right and Bottom
'       values to 0, the module sets maximum values the scanner driver allows
'       (the bottom right corner of the scanner glass)
'   - sBMPFileName (String) - the file name of the saved image
'
' Function returns 0 if OK, 1 if an error occurs
'----------------------------------------------------------------------------

Dim lRtn As Long
Dim lTmp As Long
Dim blTwainOpen As Boolean
Dim lhDIB As Long

On Local Error GoTo ErrPlace

'-------------------------------
' Open Twain Data Source Manager
'-------------------------------
lRtn = OpenTwainDSM()
If lRtn Then GoTo ErrPlace
blTwainOpen = True

'-----------------------
' Open Twain Data Source
'-----------------------
lRtn = OpenTwainDS()
If lRtn Then GoTo ErrPlace

'-----------------------------------------------------------
' Set all important attributes of the image and the transfer
'-----------------------------------------------------------

'----------------------------------------------------------------------
' Set image size and position
' If sngImageRight or sngImageBottom is 0 put physical width and height
'   of the scanner into these values
'----------------------------------------------------------------------
If (sngImageRight = 0) Or (sngImageBottom = 0) Then
    lRtn = TwainGetOneValue(PHYSICALWIDTH, sngImageRight)
    If lRtn Then GoTo ErrPlace
    lRtn = TwainGetOneValue(PHYSICALHEIGHT, sngImageBottom)
    If lRtn Then GoTo ErrPlace
End If

lRtn = SetImageSize(sngImageLeft, sngImageTop, sngImageRight, sngImageBottom)
If lRtn Then GoTo ErrPlace

'-----------------------------------------------
' Set the image resolution in DPI - both X and Y
'-----------------------------------------------
lRtn = TwainSetOneValue(XRESOLUTION, FIX32, sngResolution)
If lRtn Then GoTo ErrPlace

lRtn = TwainSetOneValue(YRESOLUTION, FIX32, sngResolution)
If lRtn Then GoTo ErrPlace

'--------------------------
' Set the image colour type
'--------------------------
lRtn = TwainSetOneValue(PIXELTYPE, UINT16, tColourType)
If lRtn Then GoTo ErrPlace

'----------------------------------------------------------------
' If the colour type is fullcolour, set the bitdepth of the image
'   - 24 bits, 32 bits, ...
'----------------------------------------------------------------
If tColourType = RGB Then lRtn = TwainSetOneValue(BITDEPTH, UINT16, 24)

'---------------------------------------------------
' Set number of images you want to transfer (just 1)
'---------------------------------------------------
lRtn = TwainSetOneValue(XFERCOUNT, INT16, 1)
If lRtn Then GoTo ErrPlace

'----------------------------------------------------
' TRANSFER the image with UI disabled.
'   If successful, lhDIB is filled with handle to DIB
'----------------------------------------------------
lRtn = TwainTransfer(False, lhDIB)
If lRtn Then GoTo ErrPlace

'------------------
' Close Data Source
'------------------
lRtn = CloseTwainDS()
If lRtn Then GoTo ErrPlace

'--------------------------
' Close Data Source Manager
'--------------------------
lRtn = CloseTwainDSM()
If lRtn Then GoTo ErrPlace
blTwainOpen = False

'----------------------------------
' Save DIB handle into the BMP file
'----------------------------------
lRtn = SaveDIBToFile(lhDIB, sBMPFileName)
If lRtn Then GoTo ErrPlace

TransferWithoutUI = 0
Exit Function

ErrPlace:
    If lhDIB Then lRtn = GlobalFree(lhDIB)
    If blTwainOpen Then lRtn = CloseTwainDS(): lRtn = CloseTwainDSM()
    TransferWithoutUI = 1
End Function

How can I modify this to scan until the document feeder is empty?

pnuts
  • 58,317
  • 11
  • 87
  • 139
Jordan Harris
  • 131
  • 1
  • 9
  • `'Set number of images you want to transfer (just 1)| lRtn = TwainSetOneValue(XFERCOUNT, INT16, 1)` Try passing an argument for number of pages instead of 1? – findwindow Dec 11 '15 at 23:48
  • I saw that, but it is essentially the same thing I am doing with my while loop. I still have to pass a number of pages, rather than having it do it all automatically. – Jordan Harris Dec 11 '15 at 23:50
  • Hmm not sure how you can know number of pages without getting into API of the scanner's feeder. – findwindow Dec 11 '15 at 23:56
  • No personal experience with this but a small bit of research shows that you'll probably need to enable the ADF capability. The `TwainSetOneValue` function could probably be used for that. More info in the [TWAIN spec (pdf)](http://twain.org/docs/530fe0da85f7511c510004ff/TWAIN-2.3-Spec.pdf). Also see [this question](http://stackoverflow.com/questions/1260357/twain-question-is-it-possible-to-scan-just-one-document-from-feeder) for possible clues. – Porcupine911 Dec 12 '15 at 21:32

0 Answers0