{"id":708,"date":"2016-03-03T16:24:00","date_gmt":"2016-03-03T16:24:00","guid":{"rendered":"http:\/\/borncity.com\/win\/?p=708"},"modified":"2022-02-28T12:41:26","modified_gmt":"2022-02-28T11:41:26","slug":"sscanning-in-word-20132016-part-i","status":"publish","type":"post","link":"https:\/\/borncity.com\/win\/2016\/03\/03\/sscanning-in-word-20132016-part-i\/","title":{"rendered":"Scanning in Word 2013\/2016 &ndash; Part I"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" style=\"float: left; margin: 0px 10px 0px 0px; display: inline;\" src=\"http:\/\/www.borncity.com\/blog\/wp-content\/uploads\/2012\/07\/Office1.jpg\" width=\"55\" height=\"60\" align=\"left\" \/>[<a href=\"http:\/\/www.borncity.com\/blog\/2016\/03\/03\/scanfunktion-in-word-20132016-nachrsten-teil-1\/\" target=\"_blank\" rel=\"noopener noreferrer\">German<\/a>]Microsoft has <a href=\"https:\/\/support.office.com\/en-US\/article\/Scan-documents-B4AE150E-319F-4E18-B27B-418F1D690823\" target=\"_blank\" rel=\"noopener noreferrer\">removed scanning<\/a> 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.<\/p>\n<p><!--more--><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/ssl-vg03.met.vgwort.de\/na\/793ff6fc6adb44ee98d188ea60a26eca\" alt=\"\" width=\"1\" height=\"1\" \/>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 \u2013 The technical reference, Microsoft Press Germany). So I haven't polished the macro solution too much.<\/p>\n<h3>VBA, WIA, Twain, what I need to know?<\/h3>\n<p>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.<\/p>\n<p>Microsoft Windows Image Acquisition Library v2.0 &#8211; provided in Windows 7, 8, 8.1 and 10 \u2013 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 \u2013 which means: The scanner vendor needs to provide compatible WIA drivers for the used Windows version.<\/p>\n<blockquote><p>The TWAIN interface supported from several scanner vendors isn't suitable for our purposes. There is a <em>mdlTwain<\/em> library available at <a href=\"https:\/\/web.archive.org\/web\/20140831191743\/http:\/\/www.freevbcode.com\/imagesvr_ce\/184390\/source\/0306\/mdltwain.zip\" target=\"_blank\" rel=\"noopener noreferrer\">mdlTwain.zip<\/a> and within my German blog post is a <a href=\"http:\/\/www.borncity.com\/blog\/2013\/02\/04\/scannen-in-word-2013-teil-2\/#comment-15507\" target=\"_blank\" rel=\"noopener noreferrer\">comment with a code snippet<\/a> to access a TWAIN scan interface.<\/p><\/blockquote>\n<h3>A brief introduction to WIA scan<\/h3>\n<p>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).<\/p>\n<p><a href=\"https:\/\/web.archive.org\/web\/20181226122955\/http:\/\/e85.imgup.net\/Scan028aa4.jpg\"><img decoding=\"async\" src=\"https:\/\/web.archive.org\/web\/20181226122955\/http:\/\/e85.imgup.net\/Scan028aa4.jpg\" alt=\"\" \/><\/a><\/p>\n<p>Selecting a device and confiming OK button invokes the WIA dialog box to access a scanner.<\/p>\n<p><a href=\"https:\/\/web.archive.org\/web\/20181226123508\/http:\/\/o41.imgup.net\/Scan03a680.jpg\"><img decoding=\"async\" src=\"https:\/\/web.archive.org\/web\/20181226123508\/http:\/\/o41.imgup.net\/Scan03a680.jpg\" alt=\"\" \/><\/a><\/p>\n<p>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.<\/p>\n<blockquote><p>Not supported are multi page scans, because I don't have such a device \u2013 and it seems, that WIA doesn't support this feature. Multi page scans are <a href=\"http:\/\/stackoverflow.com\/questions\/17008480\/scan-multiple-pages-with-adf-scanner-using-vba\">dicussed here<\/a> \u2013 maybe it's helpful for further reading.<\/p>\n<p>BTW: The dialog boxes shown above are still in German, because I use a German Windows 10 \u2013 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.<\/p><\/blockquote>\n<h3>Invoke the VBA development environment<\/h3>\n<p>Now it's time to invoke the VBA development environment in Word 2016 (it works also in Word 2013).<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/web.archive.org\/web\/20181226050511\/http:\/\/p40.imgup.net\/VBA01c7b8.jpg\" \/><\/p>\n<p>1. Go to <em>View <\/em>tab, and select <em>Macros \u2013 View Macros<\/em>.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/web.archive.org\/web\/20181226054505\/http:\/\/x86.imgup.net\/VBA02eb62.jpg\" \/><\/p>\n<p>2. Select <em>Normal.dotm (global template) <\/em>in <em>Macros in<\/em>, add a macro name \"Scan\" into <em>Macro name <\/em>text box, enter an optional description and hit the <em>Create <\/em>button.<\/p>\n<p>The Visual Basic development environment with an empty macro \"Scan\" might be visible (see below).<\/p>\n<h3>Important: Set a reference to WIA library<\/h3>\n<p>Before we enter the macro code, it's important, to create a reference to the WIA class library used in VBA.<\/p>\n<p>1. Opens <em>Tools <\/em>menu an select command <em>References<\/em>.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/web.archive.org\/web\/20181226060043\/http:\/\/g27.imgup.net\/VBA03294a.jpg\" \/><\/p>\n<p>2. Check <em>Microsoft Windows Image Acquisition Library v2.0<\/em> in dialog box <em>References <\/em>and confirm OK button.<\/p>\n<p>Then the library <em>wiaaut.dll<\/em> will be referenced within the VBA project, so we can access the classes provided by this DLL.<\/p>\n<h3>Add the macro code<\/h3>\n<p>After rerencing the WIA library, we are ready to insert the necessary VBA code into the <em>Scan() <\/em>module. I've prepared the following VBA code for this purposes.<\/p>\n<pre>' Scan for Word 2013\/2016\r\n' Author: G\u00fcnter Born www.borncity.de www.borncity.com\/blog\r\n' Implements a Scan function in Word 2013\/2016\r\n\r\nSub Scan()\r\n'\r\n' Scan Macro, to be invoked in Word\r\n'\r\n  On Error Resume Next\r\n     Dim objCommonDialog As WIA.CommonDialog\r\n     Dim objImage As WIA.ImageFile\r\n     Dim strDateiname\r\n     ' instantiate Scan WIA objects\r\n     Set objCommonDialog = New WIA.CommonDialog\r\n     Set objImage = objCommonDialog.ShowAcquireImage\r\n      \r\n     ' set temporary file\r\n     strDateiname = Environ(\"temp\") &amp; \"\\Scan.jpg\"\r\n     \r\n     If Not objImage Is Nothing Then\r\n       Kill strDateiname 'delete old file\r\n       ' save into temp file\r\n       objImage.SaveFile strDateiname \r\n       ' insert into document\r\n       Selection.InlineShapes.AddPicture strDateiname \r\n       Set objImage = Nothing\r\n     End If\r\n     Set objCommonDialog = Nothing\r\n \r\n   ' MsgBox strDateiname  ' test output\r\nEnd Sub\r\n<\/pre>\n<p>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.<\/p>\n<blockquote><p>You can cute an paste the macro code shown above into the code windows. Or you can download the file <a href=\"http:\/\/www.borncity.com\/blog\/wp-content\/uploads\/2016\/03\/ScanV2.zip\">ScanV2.zip<\/a>, unzip the ZIP archive and import the .BAS file into the VBA editor windows using menu command File\/Import.<\/p><\/blockquote>\n<p>After pushing the <em>Save as <\/em>button you may close the VBA development environment window.<\/p>\n<p><strong>Articles:<br \/>\n<\/strong><a href=\"https:\/\/borncity.com\/win\/2016\/03\/03\/sscanning-in-word-20132016-part-i\/\">Scanning in Word 2013\/2016<\/a> \u2013 Part I<br \/>\n<a href=\"https:\/\/borncity.com\/win\/2016\/03\/04\/scanning-in-word-20132016-part-ii\/\">Scanning in Word 2013\/2016<\/a> \u2013 Part II<\/p>\n<p><a href=\"https:\/\/borncity.com\/win\/2013\/02\/04\/how-to-add-a-scan-function-to-word-2013\/\">How to add a scan function to Word 2013<\/a><br \/>\n<a title=\"http:\/\/www.borncity.com\/blog\/2013\/02\/04\/scanning-in-word-2013\/\" href=\"http:\/\/www.borncity.com\/blog\/2013\/02\/04\/scanning-in-word-2013\/\" target=\"_blank\" rel=\"noopener noreferrer\">Scanning in Word 2013<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>[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.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[37,30,206],"class_list":["post-708","post","type-post","status-publish","format-standard","hentry","category-office","tag-scan-feature","tag-tip","tag-word-20132016"],"_links":{"self":[{"href":"https:\/\/borncity.com\/win\/wp-json\/wp\/v2\/posts\/708","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/borncity.com\/win\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/borncity.com\/win\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/borncity.com\/win\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/borncity.com\/win\/wp-json\/wp\/v2\/comments?post=708"}],"version-history":[{"count":0,"href":"https:\/\/borncity.com\/win\/wp-json\/wp\/v2\/posts\/708\/revisions"}],"wp:attachment":[{"href":"https:\/\/borncity.com\/win\/wp-json\/wp\/v2\/media?parent=708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/borncity.com\/win\/wp-json\/wp\/v2\/categories?post=708"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/borncity.com\/win\/wp-json\/wp\/v2\/tags?post=708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}