First of all, I need to confess, it hat been a while, since I wrote my last VBA book (Microsoft Word 97, Developer’s Kit – The technical reference, Microsoft Press Germany). So I haven’t polished the macro solution too much.
VBA, WIA, Twain, what I need to know?
Within this blog post I’m using a few technical terms. VBA stands for Visual Basic for Application, a Basic oriented programming language, available in Microsoft Office to write macros. VBA is object oriented, so it’s possible to instantiate objects from class libraries, and use methods and properties from these objects to access a scanner from applications like Word.
Microsoft Windows Image Acquisition Library v2.0 – provided in Windows 7, 8, 8.1 and 10 – enable us to access a scanner from VBA. Microsoft Windows Image Acquisition Library v2.0 provides a class library useable from VBA. But this requires that the WIA interface is supported in Windows – which means: The scanner vendor needs to provide compatible WIA drivers for the used Windows version.
The TWAIN interface supported from several scanner vendors isn’t suitable for our purposes. There is a mdlTwain library available at http://lmik.sweb.cz/mdlTwain.zip and within my German blog post is a comment with a code snippet to access a TWAIN scan interface.
A brief introduction to WIA scan
The VBA macro introduced here enables Word 2013 or Word 2016 to initiate a scan via a push button using the WIA interface. If several WIA sources are present, Windows asks to select a WIA device (iPad, Digicam, and Scanner for instance).
Selecting a device and confiming OK button invokes the WIA dialog box to access a scanner.
Within this dialog box we can select scan options and do a preview scan. Using the scan button invokes a scan, stores the scan into a temporary image file (jpeg) and inserts the scan into word at the current text cursor position.
Not supported are multi page scans, because I don’t have such a device – and it seems, that WIA doesn’t support this feature. Multi page scans are dicussed here – maybe it’s helpful for further reading.
BTW: The dialog boxes shown above are still in German, because I use a German Windows 10 – and it seems there are conflicts between Windows language packs and Office 2016 language packs. I wasn’t able to switch my Windows 10 account the English, although this has been the default language before I installed Office 2016 with multiple languages.
Invoke the VBA development environment
Now it’s time to invoke the VBA development environment in Word 2016 (it works also in Word 2013).
1. Go to View tab, and select Macros – View Macros.
2. Select Normal.dotm (global template) in Macros in, add a macro name “Scan” into Macro name text box, enter an optional description and hit the Create button.
The Visual Basic development environment with an empty macro “Scan” might be visible (see below).
Important: Set a reference to WIA library
Before we enter the macro code, it’s important, to create a reference to the WIA class library used in VBA.
1. Opens Tools menu an select command References.
2. Check Microsoft Windows Image Acquisition Library v2.0 in dialog box References and confirm OK button.
Then the library wiaaut.dll will be referenced within the VBA project, so we can access the classes provided by this DLL.
Add the macro code
After rerencing the WIA library, we are ready to insert the necessary VBA code into the Scan() module. I’ve prepared the following VBA code for this purposes.
' Scan for Word 2013/2016 ' Author: Günter Born www.borncity.de www.borncity.com/blog ' Implements a Scan function in Word 2013/2016 Sub Scan() ' ' Scan Macro, to be invoked in Word ' On Error Resume Next Dim objCommonDialog As WIA.CommonDialog Dim objImage As WIA.ImageFile Dim strDateiname ' instantiate Scan WIA objects Set objCommonDialog = New WIA.CommonDialog Set objImage = objCommonDialog.ShowAcquireImage ' set temporary file strDateiname = Environ("temp“) & "\Scan.jpg“ If Not objImage Is Nothing Then Kill strDateiname 'delete old file ' save into temp file objImage.SaveFile strDateiname ' insert into document Selection.InlineShapes.AddPicture strDateiname Set objImage = Nothing End If Set objCommonDialog = Nothing ' MsgBox strDateiname ' test output End Sub
The code invokes the WIA dialog box, calculates the path to a temporary user profile folder, invokes the scan function, stores the scan as an JPEG image and insert the scan image into the Word document.
You can cute an paste the macro code shown above into the code windows. Or you can download the file ScanV2.zip, unzip the ZIP archive and import the .BAS file into the VBA editor windows using menu command File/Import.
After pushing the Save as button you may close the VBA development environment window.