Scanning in Word 2013/2016 – Part I

[German]Microsoft has removed scanning from Word 2013/2016. This blog post describes, how to add a scan button to Word 2016 (works also in Word 2013) using a VBA-Macro.


Advertising

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 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).


Advertising

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.

Articles:
Scanning in Word 2013/2016 – Part I
Scanning in Word 2013/2016 – Part II

How to add a scan function to Word 2013
Scanning in Word 2013


Advertising

This entry was posted in Office and tagged , , . Bookmark the permalink.

28 Responses to Scanning in Word 2013/2016 – Part I

  1. Treeant34 says:

    Could you break this out a lot more; for someone that has zero VBA or Macro creating skills?

    I just want to be able to use my scanner in Word 2013

    Unfortunately the bozos at Micro$oft don't take the end users into consideration ever.

  2. Ron Visser says:

    I used your VBA code but my problem is it scans in legal paper format .
    My question " Is it possible to set the scanner in A5 paper format before scan ,
    And add then the jpg to my document .

    • guenni says:

      Sorry, till now I haven't found a way to change the paper format via the WIA API. But it could be my fault – I created the solution as a "quick & dirty" template (reusing my 'rusty' VBA knowlegde from 2000).

      • Ron Visser says:

        Thanks for the information.
        I will google to find a sollution.
        Till now i find anything that is usefull.

  3. D Hardisty says:

    Seems very, unnecessarily, complicated – I have a macro scanner in word 2013 which, unfortunately does not work in Word 2016

  4. Carlos Ferreira says:

    I got this message error:
    Compile error:
    Syntax error

    strDateiname = Environ("temp") & "\Scan.jpg"

    thank you

    • guenni says:

      Check for appropriate quotation marks – the blog software changes quotations.

      • Tash says:

        please can you give us the entire code that works? I am receiving same error.

        Many thanks

      • Clifton says:

        Does anyone have the correct string of text for this particular line? I'm not fluent in coding so I don't know what "Check for appropriate Quotation marks" means? Any help is appreciated.

        • guenni says:

          It's the " for instance on your keyboard. The problem I have: The blog software change it from to other characters and then the users will receive a compile error.

  5. Vincent says:

    Super !
    Cela fonctionne parfaitement.
    Un grand merci.

  6. Danny says:

    You were right about the quotes – Word replaced the apostrophes with opening single quotes. I had to use "Insert Symbol" from the menu to make it stop autocorrecting this, but I got it corrected before I tried to copy and paste into the macro, so it wasn't a debugging hassle. Also, for those who don't know any programming at all, you have to replace the automatically generated text in the macro — "Sub Scan()" and "End Sub" should only appear once when you're done. I fixed it at once, and it worked on the first try. Thank you very much for sharing!

    • VF says:

      I am also getting the same syntax error the others got. Can someone please post the correct text? I also do not have a Symbol in the Insert menu

  7. Christopher says:

    Anybody knows the code to add duplex scanning?

  8. Salmon Petrus says:

    Scan result bigger than original image size.
    In my case, ID Card
    What is the code to scan in original size ?

    Right now, I use macro as below:

    Sub InsertScanPicSetWrapBehind()
    Dim shp As Shape
    On Error Resume Next
    WordBasic.InsertImagerScan
    Set shp = ActiveDocument.InlineShapes(ActiveDocument.InlineShapes.Count).ConvertToShape
    With shp
    .WrapFormat.Type = wdWrapBehind
    End With
    Set shp = Nothing
    End Sub

    Thank you

  9. Alan Stinchcombe says:

    Why would Microsoft imagine that users:
    (1) would no longer want to be able to scan into a document?
    (2) would not want to be able to interact with the full range of scanner settings?

    Ho hum!

  10. David French says:

    Thank you. Works for me – Win 7 64 bit, Windows 2016 32 bit.

  11. Salmon Petrus says:

    Hi Alan

    I use Word 2010
    Now I'm using your code.
    With your code, how to scan image directly ?
    without need to click scan button
    or even without showing dialog box, for I just have one scanner.
    I've add a line below your code with SendKeys "~", True 'to hit Enter. It's not work. I also tried with SendKeys "%s", True 'to hit scan button with ALT+S. It's not work too…

    ' instantiate Scan WIA objects
    Set objCommonDialog = New WIA.CommonDialog
    Set objImage = objCommonDialog.ShowAcquireImage
    SendKeys "~", True

    Many thanks.

    • guenni says:

      In Office 2007/2010 use the build in scan feature. I've described it within my German blog post Scannen unter Word 2007/2010. Google translate will help.

      Create a macro with the following code:

      Sub Scan()
      On Error Resume Next
      WordBasic.InsertImagerScan
      End Sub

      and assign it to a button – or define a shortcut. Then you will have a scan button in Word.

      That's what you proposed in a previous post. I'm not sure, if Sendkey can be used for this purpose. The solution given above needs the dialog boxes. But you are able to study the documentation of the methodes used within the macros above and try, if it's possible to initialize all data necessary to invoke a scan successfully using another methode. I've never tried that nor I'm able to test that in an environment with Word 2010.

  12. Liberty says:

    Hi I'm trying to insert the scanner button as a macro on office 2019 following the guide, but unfortunately when I start the macro I always get the error "Comilation error: user defined type not defined". Could you help me or give me another solution or guide?

    thanks

  13. Shreyansh says:

    May God Bless You Sir for this nice work.

  14. Cris says:

    Hello I've problem with this code, i set up everythng and i got this error:
    user-defined type not defined. Do You know what is it??

    • guenni says:

      Simply a typo, mabe the ´….' in comments, that are typed as '…' from the blog software.

  15. Željko Mišković says:

    Dear Mr. Günter, I would like to thank you immensely for donating a script in Visual Basic for inserting document scanning into the newer generation Word application> 2013. This is a desirable feature that has disappeared over time in newer editions of the Office suite, so we older users, over 20 years of use since the first scanners, are wondering what are the reasons for such moves. Inserting any documents that are important for preserving and storing them in digital form is a great boon and will never disappear as a necessity. So I can't help but be surprised that someone has so irresponsibly and recklessly left out this option. Your well-intentioned work is therefore invaluable to anyone unfamiliar with the benefits of using macros within Office and the Visual Basic programming language. Best regards from Croatia, Željko Mišković, B.Sc. electrical engineer

  16. James says:

    statement objCommonDialog As WIA.CommonDialog gives "user-defined type not defined" can you advise pls ? mfG/Regards

Leave a Reply

Your email address will not be published. Required fields are marked *

Note: Please note the rules for commenting on the blog (first comments and linked posts end up in moderation, I release them every few hours, I rigorously delete SEO posts/SPAM).