Scan multi-page documents into an Access database using single sheet scanner

Overview:

We had a client who wanted to scan documents into an Access database using a Brother DS600 mobile scanner. Our client wanted the scanned documents to be saved in PDF format. Rather than actually storing the documents in the database, we opted to merely store in the Access database the location of the PDF file that the scanner had saved on the client's file network share.

Challenges involved:

1) How to scan multiple page documents and turn them into a single file when using a single sheet fed scanner like the Brother DS600 mobile scanner?

2) How to output the scanned image as a PDF?

3) How to deal with different sized documents (letter versus legal)?

Solution:

See the VBA code below. Our solution involved creating a temporary jpg file for each page of the document, storing the name and location of each temporary jpg file in an Access database table, and then outputting an Access report linked to that database table to a PDF file. If the user scans a legal size document, this code will shrink the legal size document onto a letter size PDF, which was acceptable to our client.

Requirements to use the VBA code:

1) The scanner you use has to be a TWAIN compatible scanner.

2) In VBA, you must include reference to Microsoft Windows Image Acquisition 2.0 dll

3) This code was built for a single sheet scanner like the Brother DS600 mobile scanner. You may need to tweak this code for other types of scanners (such as a scanner that is an autofeed or a flatbed scanner).

4) This code only works with documents of up to 10 pages in length. To scan more than 10 pages into one document, you would need to tweak the code to allow it to do so.

5) Before you run the code, you must create an Access table called "scantemp". This table has 2 fields: an autonumber ID field, and a field called "picture" which is a hyperlink data type.

6) You must create an Access report called "rptScan" . This report's recordsource is the scantemp table. This report has only 1 object on it: an image field whose control source is the "picture" field in the scantemp table. This image field has been sized to fit the entire page (8.5 x 11).

7) The built in ability to output an Access report to a pdf file (see the "DoCmd.OutputTo acOutputReport, RptName, acFormatPDF, strFilePDF" line in the code below) only works I believe with Access 2007 and Access 2010.

VBA CODE:

Public Sub scan(size As String, strdocnumber As String)

'Size has two values: "Yes" and "No". A "yes" means that the user is scanning a legal size document.

'A "no" means that the user is scanning an 8.5 x 11 document

'strDocument normally says "1". Sometimes, our client has 2 versions of the same document.

'So users can change the strDocument to a "2" which would merely create a 2nd version of the same document

On Error GoTo Err_Handler

Dim strFileName As String

strFilename = "filename"

If strdocnumber <> "1" Then

strFileName = strFileName & " Doc " & strdocnumber

End If

End If

'Now begin code for scanner

'Must include reference to Microsoft Windows Image Acquisition 2.0 dll

Dim Dialog1 As New wia.CommonDialog, DPI As Integer, PP As Integer, l As Integer

DPI = 600

PP = 4 'No of pages

Dim Scanner As wia.device

Set Scanner = Dialog1.ShowSelectDevice(wia.WiaDeviceType.ScannerDeviceType, False, False)

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

If size = "Yes" Then

Scanner.Items(1).properties("6152").Value = 14# * DPI 'Vertical extent for legal

ElseIf size = "No" Then

Scanner.Items(1).properties("6152").Value = 11# * DPI 'Vertical extent for letter

End If

'NOTE: the next 2 lines are optional. With the Brother DS600 mobile scanner, we found that the client's

'documents looked better if we tweaked the scanner's default settings for brightness and contrast

Scanner.Items(1).properties("6154").Value = -30 'brightness

Scanner.Items(1).properties("6155").Value = 30 'contrast

Dim img As wia.ImageFile

Set img = Scanner.Items(1).Transfer(wia.FormatID.wiaFormatJPEG)

Dim strFileJPG As String

'NOTE: calculatefilelocation is a subroutine that automatically calculates the

'appropriate path on the network to store the file

strFileJPG = calculatefilelocation & strFileName & ".jpg"

If FileExists(strFileJPG) Then

Dim FSO As New FileSystemObject

FSO.DeleteFile (strFileJPG)

Set FSO = Nothing

End If

img.SaveFile (strFileJPG)

'End scanner code for 1st page

'start variable to count pages

Dim intPages As Integer

intPages = 1

'Prompt user if there are additional pages to scan

Dim Answer2 As String

Dim MyNote2 As String

MyNote2 = "Scan another page?"

Answer2 = MsgBox(MyNote2, vbQuestion + vbYesNo, "???")

If Answer2 = vbNo Then

GoTo StartPDFConversion

Else

Dim img2 As wia.ImageFile

Set img2 = Scanner.Items(1).Transfer(wia.FormatID.wiaFormatJPEG)

Dim strFileJPG2 As String

strFileJPG2 = CalculateFileLocation & strFileName & "2.jpg"

If FileExists(strFileJPG2) Then

Dim fso2 As New FileSystemObject

fso2.DeleteFile (strFileJPG2)

Set fso2 = Nothing

End If

img2.SaveFile (strFileJPG2)

intPages = intPages + 1

End If

'Prompt user if there are additional pages to scan

Dim Answer3 As String

Dim MyNote3 As String

MyNote3 = "Scan another page?"

Answer3 = MsgBox(MyNote3, vbQuestion + vbYesNo, "???")

If Answer3 = vbNo Then

GoTo StartPDFConversion

Else

Dim img3 As wia.ImageFile

Set img3 = Scanner.Items(1).Transfer(wia.FormatID.wiaFormatJPEG)

Dim strFileJPG3 As String

strFileJPG3 = CalculateFileLocation & strFileName & "3.jpg"

If FileExists(strFileJPG3) Then

Dim fso3 As New FileSystemObject

fso3.DeleteFile (strFileJPG3)

Set fso3 = Nothing

End If

img3.SaveFile (strFileJPG3)

intPages = intPages + 1

End If

'Prompt user if there are additional pages to scan

Dim Answer4 As String

Dim MyNote4 As String

MyNote4 = "Scan another page?"

Answer4 = MsgBox(MyNote4, vbQuestion + vbYesNo, "???")

If Answer4 = vbNo Then

GoTo StartPDFConversion

Else

Dim img4 As wia.ImageFile

Set img4 = Scanner.Items(1).Transfer(wia.FormatID.wiaFormatJPEG)

Dim strFileJPG4 As String

strFileJPG4 = CalculateFileLocation & strFileName & "4.jpg"

If FileExists(strFileJPG4) Then

Dim fso4 As New FileSystemObject

fso4.DeleteFile (strFileJPG4)

Set fso4 = Nothing

End If

img4.SaveFile (strFileJPG4)

intPages = intPages + 1

End If

'Prompt user if there are additional pages to scan

Dim Answer5 As String

Dim MyNote5 As String

MyNote5 = "Scan another page?"

Answer5 = MsgBox(MyNote5, vbQuestion + vbYesNo, "???")

If Answer5 = vbNo Then

GoTo StartPDFConversion

Else

Dim img5 As wia.ImageFile

Set img5 = Scanner.Items(1).Transfer(wia.FormatID.wiaFormatJPEG)

Dim strFileJPG5 As String

strFileJPG5 = CalculateFileLocation & strFileName & "5.jpg"

If FileExists(strFileJPG5) Then

Dim fso5 As New FileSystemObject

fso5.DeleteFile (strFileJPG5)

Set fso5 = Nothing

End If

img5.SaveFile (strFileJPG5)

intPages = intPages + 1

End If

'Prompt user if there are additional pages to scan

Dim Answer6 As String

Dim MyNote6 As String

MyNote6 = "Scan another page?"

Answer6 = MsgBox(MyNote6, vbQuestion + vbYesNo, "???")

If Answer6 = vbNo Then

GoTo StartPDFConversion

Else

Dim img6 As wia.ImageFile

Set img6 = Scanner.Items(1).Transfer(wia.FormatID.wiaFormatJPEG)

Dim strFileJPG6 As String

strFileJPG6 = CalculateFileLocation & strFileName & "6.jpg"

If FileExists(strFileJPG6) Then

Dim fso6 As New FileSystemObject

fso6.DeleteFile (strFileJPG6)

Set fso6 = Nothing

End If

img6.SaveFile (strFileJPG6)

intPages = intPages + 1

End If

'Prompt user if there are additional pages to scan

Dim Answer7 As String

Dim MyNote7 As String

MyNote7 = "Scan another page?"

Answer7 = MsgBox(MyNote7, vbQuestion + vbYesNo, "???")

If Answer7 = vbNo Then

GoTo StartPDFConversion

Else

Dim img7 As wia.ImageFile

Set img7 = Scanner.Items(1).Transfer(wia.FormatID.wiaFormatJPEG)

Dim strFileJPG7 As String

strFileJPG7 = CalculateFileLocation & strFileName & "7.jpg"

If FileExists(strFileJPG7) Then

Dim fso7 As New FileSystemObject

fso7.DeleteFile (strFileJPG7)

Set fso7 = Nothing

End If

img7.SaveFile (strFileJPG7)

intPages = intPages + 1

End If

'Prompt user if there are additional pages to scan

Dim Answer8 As String

Dim MyNote8 As String

MyNote8 = "Scan another page?"

Answer8 = MsgBox(MyNote8, vbQuestion + vbYesNo, "???")

If Answer8 = vbNo Then

GoTo StartPDFConversion

Else

Dim img8 As wia.ImageFile

Set img8 = Scanner.Items(1).Transfer(wia.FormatID.wiaFormatJPEG)

Dim strFileJPG8 As String

strFileJPG8 = CalculateFileLocation & strFileName & "8.jpg"

If FileExists(strFileJPG8) Then

Dim fso8 As New FileSystemObject

fso8.DeleteFile (strFileJPG8)

Set fso8 = Nothing

End If

img8.SaveFile (strFileJPG8)

intPages = intPages + 1

End If

'Prompt user if there are additional pages to scan

Dim Answer9 As String

Dim MyNote9 As String

MyNote9 = "Scan another page?"

Answer9 = MsgBox(MyNote9, vbQuestion + vbYesNo, "???")

If Answer9 = vbNo Then

GoTo StartPDFConversion

Else

Dim img9 As wia.ImageFile

Set img9 = Scanner.Items(1).Transfer(wia.FormatID.wiaFormatJPEG)

Dim strFileJPG9 As String

strFileJPG9 = CalculateFileLocation & strFileName & "9.jpg"

If FileExists(strFileJPG9) Then

Dim fso9 As New FileSystemObject

fso9.DeleteFile (strFileJPG9)

Set fso9 = Nothing

End If

img9.SaveFile (strFileJPG9)

intPages = intPages + 1

End If

'Prompt user if there are additional pages to scan

Dim Answer10 As String

Dim MyNote10 As String

MyNote10 = "Scan another page?"

Answer10 = MsgBox(MyNote10, vbQuestion + vbYesNo, "???")

If Answer10 = vbNo Then

GoTo StartPDFConversion

Else

Dim img10 As wia.ImageFile

Set img10 = Scanner.Items(1).Transfer(wia.FormatID.wiaFormatJPEG)

Dim strFileJPG10 As String

strFileJPG10 = CalculateFileLocation & strFileName & "10.jpg"

If FileExists(strFileJPG10) Then

Dim fso10 As New FileSystemObject

fso10.DeleteFile (strFileJPG10)

Set fso10 = Nothing

End If

img10.SaveFile (strFileJPG10)

intPages = intPages + 1

End If

StartPDFConversion:

Dim strFilePDF As String

strFilePDF = CalculateFileLocation & strFileName & ".pdf"

'Check for existing PDF file

If FileExists(strFilePDF) = True Then

Dim Answer As String

Dim MyNote As String

MyNote = "A file already exists. Do you want to overwrite it?"

Answer = MsgBox(MyNote, vbQuestion + vbYesNo, "???")

If Answer = vbNo Then

'Code for No button Press

Exit Sub

Else

Dim fso44 As New FileSystemObject

fso44.DeleteFile (strFilePDF)

Set fso44 = Nothing

End If

Else

End If

DoCmd.SetWarnings False

'NOTE: scantemp in the following lines is a table in your Access database used just for this process

DoCmd.RunSQL "delete from scantemp"

DoCmd.RunSQL "insert into scantemp (picture) values ('" & strFileJPG & "')"

If intPages >= 2 Then

DoCmd.RunSQL "insert into scantemp (picture) values ('" & strFileJPG2 & "')"

End If

If intPages >= 3 Then

DoCmd.RunSQL "insert into scantemp (picture) values ('" & strFileJPG3 & "')"

End If

If intPages >= 4 Then

DoCmd.RunSQL "insert into scantemp (picture) values ('" & strFileJPG4 & "')"

End If

If intPages >= 5 Then

DoCmd.RunSQL "insert into scantemp (picture) values ('" & strFileJPG5 & "')"

End If

If intPages >= 6 Then

DoCmd.RunSQL "insert into scantemp (picture) values ('" & strFileJPG6 & "')"

End If

If intPages >= 7 Then

DoCmd.RunSQL "insert into scantemp (picture) values ('" & strFileJPG7 & "')"

End If

If intPages >= 8 Then

DoCmd.RunSQL "insert into scantemp (picture) values ('" & strFileJPG8 & "')"

End If

If intPages >= 9 Then

DoCmd.RunSQL "insert into scantemp (picture) values ('" & strFileJPG9 & "')"

End If

If intPages >= 10 Then

DoCmd.RunSQL "insert into scantemp (picture) values ('" & strFileJPG10 & "')"

End If

DoCmd.SetWarnings True

'Now let's run an Access report called rptScan and output it to a PDF file on the network

'rptScan is an Access report whose recordsource is the scantemp table

Dim RptName As String

RptName = "rptScan"

DoCmd.OpenReport RptName, acViewDesign, , , acHidden

DoCmd.Close acReport, RptName, acSaveYes

DoCmd.OutputTo acOutputReport, RptName, acFormatPDF, strFilePDF

'Now delete all the temporary jpg files

Dim fso46 As New FileSystemObject

fso46.DeleteFile strFileJPG

If intPages = 2 Then

fso46.DeleteFile strFileJPG2

ElseIf intPages = 3 Then

fso46.DeleteFile strFileJPG2

fso46.DeleteFile strFileJPG3

ElseIf intPages = 4 Then

fso46.DeleteFile strFileJPG2

fso46.DeleteFile strFileJPG3

fso46.DeleteFile strFileJPG4

ElseIf intPages = 5 Then

fso46.DeleteFile strFileJPG2

fso46.DeleteFile strFileJPG3

fso46.DeleteFile strFileJPG4

fso46.DeleteFile strFileJPG5

ElseIf intPages = 6 Then

fso46.DeleteFile strFileJPG2

fso46.DeleteFile strFileJPG3

fso46.DeleteFile strFileJPG4

fso46.DeleteFile strFileJPG5

fso46.DeleteFile strFileJPG6

ElseIf intPages = 7 Then

fso46.DeleteFile strFileJPG2

fso46.DeleteFile strFileJPG3

fso46.DeleteFile strFileJPG4

fso46.DeleteFile strFileJPG5

fso46.DeleteFile strFileJPG6

fso46.DeleteFile strFileJPG7

ElseIf intPages = 8 Then

fso46.DeleteFile strFileJPG2

fso46.DeleteFile strFileJPG3

fso46.DeleteFile strFileJPG4

fso46.DeleteFile strFileJPG5

fso46.DeleteFile strFileJPG6

fso46.DeleteFile strFileJPG7

fso46.DeleteFile strFileJPG8

ElseIf intPages = 9 Then

fso46.DeleteFile strFileJPG2

fso46.DeleteFile strFileJPG3

fso46.DeleteFile strFileJPG4

fso46.DeleteFile strFileJPG5

fso46.DeleteFile strFileJPG6

fso46.DeleteFile strFileJPG7

fso46.DeleteFile strFileJPG8

fso46.DeleteFile strFileJPG9

ElseIf intPages = 10 Then

fso46.DeleteFile strFileJPG2

fso46.DeleteFile strFileJPG3

fso46.DeleteFile strFileJPG4

fso46.DeleteFile strFileJPG5

fso46.DeleteFile strFileJPG6

fso46.DeleteFile strFileJPG7

fso46.DeleteFile strFileJPG8

fso46.DeleteFile strFileJPG9

fso46.DeleteFile strFileJPG10

End If

Set fso46 = Nothing

MsgBox "Done!"

Err_Handler:

If Err.Description = "The user requested a scan and there are no documents left in the document feeder." Then

MsgBox "Please insert paper into the scanner.", vbCritical, "Warning"

Resume

Else

Debug.Print Err.Description

Exit Sub

End If

End Sub