我使用MdlTwain在Excel VBA中编写了一个模块,但它似乎只能一次扫描一页,而如果我打开Epson扫描工具,它将扫描所有页面。
按下按钮激活Excel工作表上的宏会显示此框,供用户选择他们正在扫描的文档类型(这样我就可以自动将文件保存到正确的文件夹中)。
选择文档后会出现一个框,询问您要扫描的页数。这就是我试图通过自动文档进纸器自动扫描所有页面来摆脱。
这是模块的代码。
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
作为参考,这里是 TransferWithoutUI 的mdlTwain代码(我没有写,但我可以修改)。
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
在文档进纸器为空之前,如何修改此项进行扫描?