gpt4 book ai didi

vba - 使用 VBA 使用 ADF 扫描仪扫描多页

转载 作者:行者123 更新时间:2023-12-04 21:18:33 33 4
gpt4 key购买 nike

我正在编写一个 Microsoft Access 应用程序,我希望用户能够将多个页面扫描为单个 PDF 格式。一旦我扫描了所有页面,转换为 PDF 就可以正常工作。这是我的代码:

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

当然重要的是说我的扫描仪是 Avision AV121带有自动文档进纸器。

我的问题是 Set img = dev.Items(1).Transfer(WIA_FORMAT_JPEG)一次扫描所有页面(而不仅仅是单个页面),但我只看到图像文件中的第一个。因为一次扫描所有页面,所以我不能循环扫描 - 第二次迭代时会出现错误(说进纸器实际上是空的),我仍然只扫描了第一页。

我想说明这似乎是一个普遍的问题。我已经阅读了很多关于这个问题的线程,但没有找到任何可以回答我的问题的内容。

我希望能在这里找到帮助,我真的很沮丧。

非常感谢

最佳答案

对于仍在解决这个问题的任何人,我从 JIM 的代码中修改了这段代码,以便与带有 ADF 的扫描仪一起使用。它连续扫描文档,无限制页面并将它们临时存储为 jpeg 文件。然后将报告输出为 pdf。这是我使用 ADF 扫描仪扫描多个文档的唯一方法。

'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

关于vba - 使用 VBA 使用 ADF 扫描仪扫描多页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17008480/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com