• Outlook User
  • New Outlook app
  • Outlook.com
  • Outlook Mac
  • Outlook & iCloud
  • Developer
  • Microsoft 365 Admin
    • Common Problems
    • Microsoft 365
    • Outlook BCM
    • Utilities & Addins

Use VBA to Scan to Email

Slipstick Systems

› Developer › Code Samples › Use VBA to Scan to Email

Last reviewed on October 3, 2023     11 Comments

A user was looking for a way to insert scanned images into email, to make it easier for an elderly relative to scan and send images, using a one touch function to scan and send.
scan to email using vba

You can do this using the Windows Image Acquisition Library to call up the scanner then save the scan and insert it into a new message, all from VBA. You can set the scanner options (DPI and photo size) using VBA or bring up the scanner options dialog. Although the scanner dialog adds a second click, it easy to adjust the options for each scan,.

This code in the macro embeds the attachment in the message body.

 .Attachments.Add strFilePath, 1, 0
  .HTMLBody = "

Here is the picture...

" & _ "" ' can set height also, if uniform. if not, set one value

If you want to attach the image but not embed it, change the code to this:

 .Attachments.Add strFilePath
  .HTMLBody = "

See the attachment...

"

To use this macro, paste the code into a new module then open Tools > References to set a reference to the Microsoft Windows Image Acquisition Library v2.0 object library. If it's not listed in references, you'll need to click Browse and find it at C:\WINDOWS\System32\wiaaut.dll.

Sub ScanToEmail()

Dim wiaImg As New WIA.ImageFile
Dim wiaDialog As New WIA.CommonDialog
Dim wiaScanner As WIA.Device
Dim strFilePath As String
Dim strFilename As String

Set wiaScanner = wiaDialog.ShowSelectDevice

With wiaScanner.Items(1)
' Scammer Options you can set
 '   .Properties("6146").Value = 1 '4 is Black-white,gray is 2, color 1 (Color Intent)
'    .Properties("6147").Value = 100 'dots per inch/horizontal
'    .Properties("6148").Value = 100 'dots per inch/vertical
'    .Properties("6149").Value = 0 'x point where to start scan
'    .Properties("6150").Value = 0 'y-point where to start scan
'
' Paper sizes. This is a 4x6 photo.
    .Properties("6151").Value = 400 'horizontal DPI x inches wide
    .Properties("6152").Value = 600 'vertical DPI x inches tall
    
' Available formats
' wiaFormatBMP, wiaFormatPNG, wiaFormatGIF, wiaFormatJPEG, wiaFormatTIFF
' Change file file extension in strFilename to match format
Set wiaImg = wiaScanner.Items(1).Transfer(wiaFormatJPEG)

' Make sure its saved as correct format, not bmp
If wiaImg.FormatID <> wiaFormatJPEG Then
    Dim IP 'As New ImageProcess
    Set IP = CreateObject("Wia.ImageProcess")
    
    IP.Filters.Add IP.FilterInfos("Convert").FilterID
    IP.Filters(1).Properties("FormatID").Value = wiaFormatJPEG
    Set wiaImg = IP.Apply(wiaImg)
End If

End With

'Set the filename
strFilename = "Scan-" & Format(Now, "yyyymmddhhmm") & ".jpg"
' set the file path
strFilePath = Environ$("TEMP") & "\" & strFilename ' set temporary file

' Check if filename exists, if so, delete before saving new
    If Dir(strFilePath) <> "" Then Kill strFilePath
      wiaImg.SaveFile strFilePath 'save into temp file
    DoEvents

' Create the message and insert
Dim olMsg As MailItem
Set olMsg = Application.CreateItem(olMailItem)
With olMsg
  .Subject = "Attached is the scan"
' This is embedding the image
  .Attachments.Add strFilePath, 1, 0
  .HTMLBody = "

Here is the picture...

" & _ "" ' can set height also, if uniform. if not, set one value .Display End With Set wiaImg = Nothing Set wiaScanner = Nothing End Sub

 

Show the Scanner Options dialog

If you don't want or need a fully automated macro, or want a dialog so you can make adjustments, use the following code sample. It brings up a dialog with options for scan type, resizing and quality.
scanner options dialog

Tip: Use the resizing handles to adjust the scan size and drag the gray box to re-position the cropped area.

Sub Scan()
  Dim objCommonDialog As WIA.CommonDialog
  Dim objImage As WIA.ImageFile
  Dim strFilePath As String
  ' instantiate Scan WIA objects
  Set objCommonDialog = New WIA.CommonDialog
  Set objImage = objCommonDialog.ShowAcquireImage
  strFilePath = Environ$("TEMP") & "\Scan.bmp" ' set temporary file
  If Not objImage Is Nothing Then
    If Dir(strFilePath) <> "" Then Kill strFilePath
    objImage.SaveFile strFilePath 'save into temp file
    DoEvents

Dim olMsg As Outlook.MailItem


Set olMsg = Application.CreateItem(olMailItem)
With olMsg
.Subject = Now
    .Attachments.Add strFilePath
    .Display
End With

End If
  
End Sub

How to use the macros on this page

First: You need to have macro security set to the lowest setting, Enable all macros during testing. The macros will not work with the top two options that disable all macros or unsigned macros. You could choose the option Notification for all macros, then accept it each time you restart Outlook, however, because it's somewhat hard to sneak macros into Outlook (unlike in Word and Excel), allowing all macros is safe, especially during the testing phase. You can sign the macro when it is finished and change the macro security to notify.

To check your macro security in Outlook 2010 and newer, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, look at Tools, Macro Security.

After you test the macro and see that it works, you can either leave macro security set to low or sign the macro.

The macros on this page should be placed in a module.

Open the VBA Editor by pressing Alt+F11 on your keyboard.

To put the code in a module:

  1. Right click on Project1 and choose Insert > Module
  2. Copy and paste the macro into the new module.

Set a reference to other Object Libraries
If you receive a "User-defined type not defined" error, you need to set a reference to another object library.

  1. Go to Tools, References menu.
  2. Locate the Microsoft Windows Image Acquisition Library v2.0 object library in the list and add a check mark to it. If it's not listed, you'll need to click Browse and find it at C:\WINDOWS\System32\wiaaut.dll.
    set a reference to WIA

More information as well as screenshots are at How to use the VBA Editor

More Information

FormatID Constants
Windows Image Acquisition (WIA)
Office 2016 - Excel Macro to Insert Scanned Image direct from Scanner into Worksheet with VBA

Use VBA to Scan to Email was last modified: October 3rd, 2023 by Diane Poremsky
Post Views: 32

Related Posts:

  • Format Images in Outlook Email
  • Use VBA to read fields in attached messages
  • Print Contacts and Contact Photos
  • Resize images in an Outlook email

About Diane Poremsky

A Microsoft Outlook Most Valuable Professional (MVP) since 1999, Diane is the author of several books, including Outlook 2013 Absolute Beginners Book. She also created video training CDs and online training classes for Microsoft Outlook. You can find her helping people online in Outlook Forums as well as in the Microsoft Answers and TechNet forums.

Comments

  1. Jack says

    January 20, 2023 at 5:18 am

    This really helped me a lot today, thank you very much.

    Reply
  2. Michael says

    October 15, 2019 at 8:43 pm

    Where should the code snippit at the start be placed?

    Before/after/......

    "
    Sub ScanToEmail()

    Dim wiaImg As New WIA.ImageFile
    Dim wiaDialog As Ne......
    "

    MMMMMM

    Reply
    • Diane Poremsky says

      October 15, 2019 at 10:06 pm

      The first code code snippet (to embed) is in the macro - if you want to attach the file not embed it, you need to replace that code in the macro with the second snippet.

      Reply
      • Michael says

        October 16, 2019 at 8:56 am

        Thanks Diane, I overlooked that.
        One problem on my side however is that only a very small portion of the original is scanned (I guess about 1/4)...
        Is it possible to make this "paper size" flexible or at least ask for a size before scanning starts. Sorry..... ;)

      • Diane Poremsky says

        October 16, 2019 at 11:52 pm

        Yes. This part of the code controls the size and some scanning options.
        With wiaScanner.Items(1)
        ' Scammer Options you can set
        ' .Properties("6146").Value = 1 '4 is Black-white,gray is 2, color 1 (Color Intent)
        ' .Properties("6147").Value = 100 'dots per inch/horizontal
        ' .Properties("6148").Value = 100 'dots per inch/vertical
        ' .Properties("6149").Value = 0 'x point where to start scan
        ' .Properties("6150").Value = 0 'y-point where to start scan
        '
        ' Paper sizes. This is a 4x6 photo.
        .Properties("6151").Value = 400 'horizontal DPI x inches wide
        .Properties("6152").Value = 600 'vertical DPI x inches tall

      • Michael says

        October 18, 2019 at 8:43 am

        Thanks again Diane!
        I saw the piece of code, but hoped there would be some kind of "trick" to insert a user dialog, where the (approximate) measurements of the object to be scanned could be set. Also there are many scanners with an autodetect system, but I presume programming that would be far too complicated.

      • Diane Poremsky says

        October 18, 2019 at 10:42 am

        This code opens a dialog and i can use the resizing handles to adjust it.

        Sub Scan()
        Dim objCommonDialog As WIA.CommonDialog
        Dim objImage As WIA.ImageFile
        Dim strFilePath As String
        ' instantiate Scan WIA objects
        Set objCommonDialog = New WIA.CommonDialog
        Set objImage = objCommonDialog.ShowAcquireImage
        strFilePath = Environ$("TEMP") & "\Scan.bmp" ' set temporary file
        If Not objImage Is Nothing Then
        If Dir(strFilePath) <> "" Then Kill strFilePath
        objImage.SaveFile strFilePath 'save into temp file
        DoEvents

        Dim olMsg As Outlook.MailItem

        Set olMsg = Application.CreateItem(olMailItem)
        With olMsg
        .Subject = Now
        .Attachments.Add strFilePath
        .Display
        End With

        End If

        End Sub

      • Michael says

        October 23, 2019 at 6:35 pm

        Wonderful work, Diane. It works really fine. Thank you for all your trouble.

      • Ali Jaffar says

        February 21, 2021 at 12:36 am

        where to insert code fordialog with options for scan type, resizing and quality.

      • Diane Poremsky says

        February 21, 2021 at 8:16 pm

        The example I posted in these comments is in the first macro in the article

      • Ali Jaffar says

        February 21, 2021 at 9:56 pm

        Hi Diane

        thanks for your Reply. Code works but I can not convert files to JPEG or PDF Formant.

Leave a Reply Cancel reply

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

Visit Slipstick Forums.
What's New at Slipstick.com

Latest EMO: Vol. 31 Issue 5

Subscribe to Exchange Messaging Outlook






Support Services

Do you need help setting up Outlook, moving your email to a new computer, migrating or configuring Office 365, or just need some one-on-one assistance?

Our Sponsors

CompanionLink
ReliefJet
  • Popular
  • Latest
  • Week Month All
  • Jetpack plugin with Stats module needs to be enabled.
  • Sync Issues and Errors with Gmail and Yahoo accounts
  • Error Opening iCloud Appointments in Classic Outlook
  • Opt out of Microsoft 365 Companion Apps
  • Mail Templates in Outlook for Windows (and Web)
  • Urban legend: Microsoft Deletes Old Outlook.com Messages
  • Buttons in the New Message Notifications
  • Move Deleted Items to Another Folder Automatically
  • Open Outlook Templates using PowerShell
  • Count and List Folders in Classic Outlook
  • Google Workspace and Outlook with POP Mail
Ajax spinner

Recent Bugs List

Microsoft keeps a running list of issues affecting recently released updates at Fixes or workarounds for recent issues in classic Outlook (Windows).

For new Outlook for Windows: Fixes or workarounds for recent issues in new Outlook for Windows .

Outlook for Mac Recent issues: Fixes or workarounds for recent issues in Outlook for Mac

Outlook.com Recent issues: Fixes or workarounds for recent issues on Outlook.com

Office Update History

Update history for supported Office versions is at Update history for Office

Outlook Suggestions and Feedback

Outlook Feedback covers Outlook as an email client, including Outlook Android, iOS, Mac, and Windows clients, as well as the browser extension (PWA) and Outlook on the web.

Outlook (new) Feedback. Use this for feedback and suggestions for Outlook (new).

Use Outlook.com Feedback for suggestions or feedback about Outlook.com accounts.

Other Microsoft 365 applications and services




New Outlook Articles

Sync Issues and Errors with Gmail and Yahoo accounts

Error Opening iCloud Appointments in Classic Outlook

Opt out of Microsoft 365 Companion Apps

Mail Templates in Outlook for Windows (and Web)

Urban legend: Microsoft Deletes Old Outlook.com Messages

Buttons in the New Message Notifications

Move Deleted Items to Another Folder Automatically

Open Outlook Templates using PowerShell

Count and List Folders in Classic Outlook

Google Workspace and Outlook with POP Mail

Newest Code Samples

Open Outlook Templates using PowerShell

Count and List Folders in Classic Outlook

Insert Word Document into Email using VBA

Warn Before Deleting a Contact

Use PowerShell to Delete Attachments

Remove RE:, FWD:, and Other Prefixes from Subject Line

Change the Mailing Address Using PowerShell

Categorize @Mentioned Messages

Send an Email When You Open Outlook

Delete Old Calendar Events using VBA

VBA Basics

How to use the VBA Editor

Work with open item or selected item

Working with All Items in a Folder or Selected Items

VBA and non-default Outlook Folders

Backup and save your Outlook VBA macros

Get text using Left, Right, Mid, Len, InStr

Using Arrays in Outlook macros

Use RegEx to extract message text

Paste clipboard contents

Windows Folder Picker

Custom Forms

Designing Microsoft Outlook Forms

Set a custom form as default

Developer Resources

Developer Resources

Developer Tools

VBOffice.net samples

SlovakTech.com

Outlook MVP David Lee

Repair PST

Convert an OST to PST

Repair damaged PST file

Repair large PST File

Remove password from PST

Merge Two Data Files

Sync & Share Outlook Data

  • Share Calendar & Contacts
  • Synchronize two computers
  • Sync Calendar and Contacts Using Outlook.com
  • Sync Outlook & Android Devices
  • Sync Google Calendar with Outlook
  • Access Folders in Other Users Mailboxes

Diane Poremsky [Outlook MVP]

Make a donation

Mail Tools

Sending and Retrieval Tools

Mass Mail Tools

Compose Tools

Duplicate Remover Tools

Mail Tools for Outlook

Online Services

Calendar Tools

Schedule Management

Calendar Printing Tools

Calendar Reminder Tools

Calendar Dates & Data

Time and Billing Tools

Meeting Productivity Tools

Duplicate Remover Tools

Productivity

Productivity Tools

Automatic Message Processing Tools

Special Function Automatic Processing Tools

Housekeeping and Message Management

Task Tools

Project and Business Management Tools

Choosing the Folder to Save a Sent Message In

Run Rules on messages after reading

Help & Suggestions

Submit Outlook Feature Requests

Slipstick Support Services

Buy Microsoft 365 Office Software and Services

Visit Slipstick Forums.

What's New at Slipstick.com

Home | Outlook User | Exchange Administrator | Office 365 | Outlook.com | Outlook Developer
Outlook for Mac | Common Problems | Utilities & Addins | Tutorials
Outlook & iCloud Issues | Outlook Apps
EMO Archives | About Slipstick | Slipstick Forums
Submit New or Updated Outlook and Exchange Server Utilities

Send comments using our Feedback page
Copyright © 2026 Slipstick Systems. All rights reserved.
Slipstick Systems is not affiliated with Microsoft Corporation.