My Assistant
![]() ![]() |
|
|
Aug 17 2011, 02:09 PM
Post
#1
|
|
|
UtterAccess Addict Posts: 164 From: Hamilton, NJ USA |
I'm trying to find info on MS Document Scanning. I'm trying to automate scanning from Access 2007 VBA.
Most searches just lead me to MS Document Imaging. These are two separate entities in my computer's programs list though. The only reference I see in my VBA References List is for MSDI. Can anyone point me in the direction of some code or even where to start working with these programs? Thanks! |
|
|
|
Aug 17 2011, 03:08 PM
Post
#2
|
|
|
UtterAccess Editor Posts: 6,717 From: Capital District, NY, USA |
Hi,
Someone may already have something ready-built, but I probably what you're going to end up having to do is a lot of work with the Windows API in order to communicate with the hardware drivers and perform the required tasks. If it's anything like the old Printing API (before the Printer object came around in 2002), it's a real nightmare. A quick search of "Scanner Win32 API" landed me at the MSDN base page for Windows Image Acquisition. If you're API savvy you might be able to work your way through it. At the bottom of the page is the Reference link: "Information on WIA interfaces, methods, objects, and data types used in C/C++ and scripting." - where you'll find all the documentation on what to use and how. Good luck! If you think it's something you want to get into I wouldn't mind trailing along and seeing if I can help out (I like API work), but I think it's going to be a process and I haven't had the time I usually have lately. Again, someone may already have something ready built. Cheers, |
|
|
|
Aug 17 2011, 03:51 PM
Post
#3
|
|
|
UtterAccess Addict Posts: 164 From: Hamilton, NJ USA |
I've never worked with API before, but I don't have a deadline so it looks like a good project to work on if I can't find something out there already. I'm always interested in learning new things. I will keep you posted of my progress.
Thank you for the link. Definitely a good place to start. |
|
|
|
Aug 17 2011, 07:26 PM
Post
#4
|
|
|
UtterAccess Editor Posts: 6,717 From: Capital District, NY, USA |
It looks like we won't have to use the API after all.
For non C/C++ languages, the WIA Automation Layer is provided (for XP SP1 and newer clients and Server 2003 an newer servers). You should see in your list of references Microsoft Windows Image Aquisition Library 2.0. I just started playing around with a few minutes ago, but it looks like the Object Listing page is where you'll be spending a fair amount of time. It seems that you create a DeviceManager object, read/select a Device, check the Devices Items, work with the Item to transfer data, etc etc. So far I've done nothing but return a list of imaging devices and a few properties of them. Also of note is the Shared Samples. Pretty handy. Lots of digesting. Cheers, |
|
|
|
Aug 17 2011, 08:05 PM
Post
#5
|
|
|
UtterAccess Editor Posts: 6,717 From: Capital District, NY, USA |
I thought it'd be a lot more difficult than this. Check it out:
CODE Public Function WIATest() Dim comDialog As WIA.CommonDialog Dim img As WIA.ImageFile Set comDialog = New WIA.CommonDialog Set img = New WIA.ImageFile Set img = comDialog.ShowAcquireImage() img.SaveFile ("D:\imgfile.bmp") Set img = Nothing End Function Opens a dialog to scan an image, click Scan, code waits until the scan is complete, saves the scanned image as directed. Pretty cool. This is on a machine with one scanner (one image device connected). Presumably with more, you'd have to select the appropriate device. This of course doesn't include any detail settings, etc etc, and isn't 100% transparent to the user as the ShowAquireImage method shows a dialog where they need to Preview/Scan or Cancel (per my device, that's what the options are - not sure if this is OS driven or Device driven). By checking the img.FileExtension I saw the default to be a .bmp, so that's what I attempted to save it as. Lo and behold, there sits the file. Pretty cool. edit: Apparently WIA is the "replacement" for the TWAIN interface (which is what I was grimacing about with all the advanced API work). It's been around for a while, but still it may be well to make sure that all the devices you plan to use support WIA. Is there a particular task/process you wish to employ? Myself, I'm going to see if I can't work on scanning on a button click, with no dialog, and save as a specified image type (not a bmp fan). Let me know and I'll see what else I can come up with while playing around. |
|
|
|
Aug 17 2011, 09:28 PM
Post
#6
|
|
|
UtterAccess Addict Posts: 164 From: Hamilton, NJ USA |
Wow! I actually wish I was at work right now! That doesn't happen every day.
I just build the Db a month ago to hold all the info I need for tracking Regulated Medical Waste my company transports. Each DEP tracking form has a unique number at the top of it that I use as the primary key. I was scanning the form one at a time and renaming each file with that number. Earlier today I noticed the MSDS and MSDI programs and thought I'd give it a shot. Not only did it scan the form, the OCR was able to pick out the Form Number and name the TIFF file it created with that number which is exactly what I wanted. Basically I want to be able to click a button and have it scan for me instead of the manual way which involves a lot more clicking and typing. Now that I see it's not so difficult I can see a lot of possibilities with this. Thank you so much! |
|
|
|
Aug 17 2011, 09:42 PM
Post
#7
|
|
|
UtterAccess Editor Posts: 6,717 From: Capital District, NY, USA |
I came up with the following, which is (almost) complete transparent to the user, with the exception of there being a wait while the scan is in progress (I don't see a way around this except to wrap the thing in a COM dll and send it off on it's own process).
CODE Option Compare Database Option Explicit Const DEVNAME As String = "EPSON NX210/TX210" Const WIA_FORMAT_JPEG = "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}" Public Function WIATest2(Optional bShowProgress As Boolean = True) 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 Set DevMgr = New WIA.DeviceManager For i = 1 To DevMgr.DeviceInfos().Count If DevMgr.DeviceInfos(i).Properties("Name") = DEVNAME Then Set DevInfo = DevMgr.DeviceInfos(i) End If Next i Set dev = DevInfo.Connect 'I'm not positive on this, but it appears that Item(1) is the root item 'of the device. The only scanner I have at hand only has one item anyway, 'AFAICT other items might include tray feeders, etc. Not sure, more importantly 'not sure why Item(1) is the one to use or how safe it is to rely on... If bShowProgress Then Set ComDialog = New WIA.CommonDialog Set img = ComDialog.ShowTransfer(dev.Items(1), WIA_FORMAT_JPEG, True) Else Set img = dev.Items(1).Transfer(WIA_FORMAT_JPEG) End If img.SaveFile "D:\img7.jpg" Set img = Nothing Set dev = Nothing Set DevInfo = Nothing Set DevMgr = Nothing Set ComDialog = Nothing End Function Obviously we can adjust the image output name. The only issue I'm having at the moment is trying to adjust the properties of the imagefile before it's saved... preferable to a smaller more managable size. I can think of a few ways to maybe handle it after the fact, but as of now I'm still drawing some blanks on how to handle it through WIA. Indeed, I was quite surprised and pleased with this as well. I'll certainly find a use for it... I've always hated the interface for scanners, and now I can scan and send/store/attach to a record in one click. One of those things that I always thought would be great but never persued because I thought it would be very difficult... so thanks for asking! I'll post more as I find more, but for the time being I'm going to call it night. Cheers, |
|
|
|
Aug 17 2011, 09:54 PM
Post
#8
|
|
|
UtterAccess Editor Posts: 6,717 From: Capital District, NY, USA |
Ok, one more, realized my error and couldn't put it down yet.
This one pulls up a dialog where you can edit some properties of the scan. With my device, these are very limited (DPI only basically). I've also looped the properties via VBA to see which are writable... a test of changing the horizontal extend from 687 to 200 works just like it should. You'll see the immediate window output for the properties... there's a fair amount we can manage here with some trial and error on known paper sizes, etc. Now I'm done for the night (IMG:style_emoticons/default/thumbup.gif) CODE Option Compare Database Option Explicit Const DEVNAME As String = "EPSON NX210/TX210" Const WIA_FORMAT_JPEG = "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}" Public Function WIATest2(Optional bShowProgress As Boolean = True) Dim ComDialog As WIA.CommonDialog Dim DevMgr As WIA.DeviceManager Dim DevInfo As WIA.DeviceInfo Dim dev As WIA.Device Dim itm As WIA.Item Dim img As WIA.ImageFile Dim p As Object Dim i As Integer Dim s As String Set DevMgr = New WIA.DeviceManager For i = 1 To DevMgr.DeviceInfos().Count If DevMgr.DeviceInfos(i).Properties("Name") = DEVNAME Then Set DevInfo = DevMgr.DeviceInfos(i) End If Next i Set dev = DevInfo.Connect Set itm = dev.Items(1) Set ComDialog = New WIA.CommonDialog ComDialog.ShowItemProperties itm For Each p In itm.Properties If p.IsReadOnly Then s = "[READONLY] " s = s & p.Name & " " & p.Value Debug.Print s s = "" Next 'test non readonly item property itm.Properties("Horizontal Extent") = 200 'was 687 or something for regular paper 'I'm not positive on this, but it appears that Item(1) is the root item 'of the device. The only scanner I have at hand only has one item anyway, 'AFAICT other items might include tray feeders, etc. Not sure, more importantly 'not sure why Item(1) is the one to use or how safe it is to rely on... If bShowProgress Then Set img = ComDialog.ShowTransfer(dev.Items(1), WIA_FORMAT_JPEG, True) Else Set img = dev.Items(1).Transfer(WIA_FORMAT_JPEG) End If img.SaveFile "D:\img8.jpg" Set img = Nothing Set dev = Nothing Set DevInfo = Nothing Set DevMgr = Nothing Set ComDialog = Nothing End Function item properties: CODE [READONLY] Item Name Top
[READONLY] Full Item Name 0000\Root\Top [READONLY] Item Flags 67 Color Profile Name NX210_R.ICC Horizontal Resolution 75 Vertical Resolution 75 Horizontal Extent 637 Vertical Extent 877 Horizontal Start Position 0 Vertical Start Position 0 Data Type 3 [READONLY] Bits Per Pixel 24 Brightness 0 Contrast 0 Current Intent 0 [READONLY] Pixels Per Line 637 [READONLY] Number of Lines 877 [READONLY] Preferred Format {B96B3CAA-0728-11D3-9D7B-0000F81EF32E} [READONLY] Item Size 1676864 Threshold 110 Format {B96B3CAA-0728-11D3-9D7B-0000F81EF32E} Media Type 128 [READONLY] Channels Per Pixel 3 [READONLY] Bits Per Channel 8 [READONLY] Planar 0 [READONLY] Bytes Per Line 1912 [READONLY] Buffer Size 65536 [READONLY] Access Rights 3 Compression 0 Photometric Interpretation 0 [READONLY] Lamp Warm up Time 90000 |
|
|
|
Aug 29 2011, 03:54 PM
Post
#9
|
|
|
UtterAccess Addict Posts: 164 From: Hamilton, NJ USA |
I'm finally back in the office and am trying this out, but am getting an error right off the bat.
Dim ComDialog As WIA.CommonDialog Complie Error: User-defined type not defined I assume I'm missing a Reference. If that's the case, do you know what I may need. I don't see any WIA or Common Dialog references in my list of refs. Is it called something else? |
|
|
|
Aug 29 2011, 06:34 PM
Post
#10
|
|
|
UtterAccess Editor Posts: 6,717 From: Capital District, NY, USA |
In bold in this post: http://www.utteraccess.com/forum/index.php...t&p=2147931
|
|
|
|
Aug 30 2011, 12:32 AM
Post
#11
|
|
|
UtterAccess Addict Posts: 164 From: Hamilton, NJ USA |
Oh geez, sorry about that. Thanks.
I got an error, but fixed it. The device I'm using is called "HP 6500 (network)" For some reason when the code is looping through the devices it wasn't finding it. So I'd get an error at Set dev = DevInfo.connect After stepping through I noticed the code was picking up the scanner as "HP 6500 (NET)" Not sure if that's important or not. I just changed DEVNAME to "HP 6500 (NET)" and now it works. its not scanning the entire image for some reason so I have to mess around with it a little. But I am in complete awe that you were able to figure all that out in an afternoon. Thank you so much! |
|
|
|
Aug 30 2011, 03:42 AM
Post
#12
|
|
|
UtterAccess Editor Posts: 6,717 From: Capital District, NY, USA |
I think in my last post that had code I was listing all the properties... I had tried changing the Vertical and Horizontal Extent properties and had succeeded in shrinking the image down to the pixels sizes I entered.
I really didn't get much further than that, and don't doubt things tend to vary from one device mgfr to the next. I've also noticed that you need to have a relatively new scanner that recognizes WIA - apparently a handfull within the last 5 or 6 years or so are still TWAIN only. >> I am in complete awe that you were able to figure all that out in an afternoon << Me too! Unfortunately I had to drop the project for more pressing things, but it's definately in my list of things to get back to as soon as I can. Cheers, |
|
|
|
Aug 30 2011, 03:25 PM
Post
#13
|
|
|
UtterAccess Addict Posts: 164 From: Hamilton, NJ USA |
I'm trying to convert the jpg that is created to a pdf. Perhaps you can clue me in on a problem. I found a piece of code that is close to what I need and I've tweaked it little for my needs, but I there's a problem with it. Here's part of the code
CODE Dim a Dim my_pdfMaker As AdobePDFMakerForOffice.pdfMaker Dim pdfSettings As AdobePDFMakerForOffice.ISettings Set my_pdfMaker = Nothing ' locate PDFMaker object For Each a In Application.COMAddIns If InStr(UCase(a.Description), "PDFMAKER") > 0 Then Set my_pdfMaker = a.Object '<---NOT WORKING Exit For End If Next If my_pdfMaker Is Nothing Then MsgBox "Cannot Find PDFMaker add-in", vbOKOnly, "" Exit Sub End If should I be using something other than .Object? I don't get an error, but the my_pdfMaker does not set to anything. Stepping through, the code does go through that line, but it doesn't set for some reason. Any clues? |
|
|
|
Aug 30 2011, 03:38 PM
Post
#14
|
|
|
UtterAccess Editor Posts: 6,717 From: Capital District, NY, USA |
It seems as thought it should work, but I don't see how that code is converting anything.
Try removing the .Object, and just setting a CODE Set my_pdfMaker = a
|
|
|
|
Aug 30 2011, 03:41 PM
Post
#15
|
|
|
UtterAccess Addict Posts: 164 From: Hamilton, NJ USA |
type mismatch
|
|
|
|
Aug 30 2011, 03:51 PM
Post
#16
|
|
|
UtterAccess Editor Posts: 6,717 From: Capital District, NY, USA |
I'm not familiar with looping COM addins like that. Usually you would declare the variable and instaitate it:
CODE Dim my_pdfMaker As AdobePDFMakerForOffice.pdfMaker Dim pdfSettings As AdobePDFMakerForOffice.ISettings Set my_pdfMaker = New AdobePDFMakerForOffice.pdfMaker Set pdfSettings = my_pdfMaker.Settings 'or whatever Not sure, won't have a bit to look into it until later, maybe tomorrow |
|
|
|
Aug 30 2011, 07:05 PM
Post
#17
|
|
|
UtterAccess Editor Posts: 6,717 From: Capital District, NY, USA |
Is PDFMaker a "codable" object that is distributed with the intent to use from VBA (or another COM language)? From what I'm seeing, it seems that PDF Maker is an addin for the Office suite that is used to make pdfs, but through their UI rather than programmatically.
I'm finding it quite difficult to find any information on the object, and the code you found suggests that it's an addin rather than a control. Adobe doesn't seem to be giving out any information on it either - in fact one (informal) reference says that he may have recalled some lawsuit based due to reverse engineering the COM addin. Generally object model information is readily available, even for lesser known objects - formal and informal. The ins and outs of this PDFMaker though - even the MVPs responding in other discussions are saying yea, it'd be nice if Adobe gave up the model so it could be used, and the little pieces that some people have squeezed out don't seem to be doing a lot of good. ref: http://groups.google.com/group/microsoft.p...2e664b877d7c689 |
|
|
|
Aug 31 2011, 09:31 AM
Post
#18
|
|
|
UtterAccess Addict Posts: 164 From: Hamilton, NJ USA |
I found a post on Adobe's website that actually has the code I had picked up elsewhere. http://forums.adobe.com/thread/286431
From all the other topics I'm seeing, from the releases of Acrobat 7 to current releases the Object has been removed rendering AdobePDFMakerForOffice basically useless. Oh well. Thanks again for looking into it. Perhaps I'll stumble upon a workaround. |
|
|
|
Aug 31 2011, 10:02 AM
Post
#19
|
|
|
UtterAccess Editor Posts: 6,717 From: Capital District, NY, USA |
I can't say enough good things about Adobe (absolutely saturated with scarasm)
Take a look at the second reply in that link... I suspect the link that the poster had referenced was to some terms or other statement from Adobe saying that this isn't allowed, although it's a dead link at this point, but seems to go in hand with adobe's stance on it and the other smiliar mentions elsewhere. Unfortunately, I can't even find a jpg -> pdf (programmatic) converter, short of printing the image to the adobe printer (if you happen to have Acrobat that works), and even then in order to do it without having to choose the file save name etc from the standard dialog is a major, major task. Stephen Lebans has a module that convers reports to PDF, which works by outputting the report to a snapshot file, then converting the snapshot, so I looked for jpg -> snapshot converters as well, with even less luck than jpg -> pdf. I never really knew why devlopers tend to hate working with PDFs until fairly recently as they're extremely popular on the user end, but if you try programming with them, you're bound to be pulling your hair out. I'm not impressed, to say the least - nothing but problems, and reading through the adobe forums with people trying to find answers is depressing. Poor people... (IMG:style_emoticons/default/shrug.gif) |
|
|
|
Aug 31 2011, 02:53 PM
Post
#20
|
|
|
UtterAccess Addict Posts: 164 From: Hamilton, NJ USA |
I did a little workaround that isn't totally automated at this point but does the job I want it to.
I changed the file format to bitmap. For some reason Acrobat couldn't open the JPEG format. It will open any other jpg on my computer just not the one created from the function. I also tried a tiff file with the same negative results. Const WIA_FORMAT_BMP = "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}" I added strFormNumber as a passed variable. This is the number of the tracking form that I have entered into Access and am now scanning. I use this as the file name. Dim strFile As String I added this code to the bottom of the function CODE strFile = "C:\Users\John\Pictures\Test_Scan\" & strFormNumber & ".bmp" img.SaveFile strFile Shell "C:\Program Files (x86)\Adobe\Acrobat 7.0\Acrobat\Acrobat.exe" + " " + strFile, 3 Kill strFile Opening the bitmap with acrobat automatically puts it in pdf form. I just close Acrobat and its prompts to save. And bam! I've got my pdf. The code then deletes the bitmap it created. |
|
|
|
![]() ![]() |
|
Go to Top · Lo-Fi Version | Time is now: 22nd May 2013 - 11:11 AM |