locked
Access VBA - WIA scan : whatever format I request, I always get a bitmap image RRS feed

  • Question

  • Hello,

    I use a function to scan a document.

    This function works, but there is one major issue : whatever the format I choose, I get only a bitmap image (My scanner is perfectly able to produce Tiff images with others tools like Photoshop).

    Way to check the effective file format : opening "image.jpg" with Photoshop. If the file is not in JPEG format, Photoshop raises an error.
    Another way to check effective image format is *nix command "file" :
    ScannedImage_B96B3CAB.bmp:  PC bitmap, Windows 3.x format, 864 x -1100 x 24
    ScannedImage_B96B3CAE.jpg:  PC bitmap, Windows 3.x format, 864 x -1100 x 24
    ScannedImage_B96B3CAF.png:  PC bitmap, Windows 3.x format, 864 x -1100 x 24
    ScannedImage_B96B3CB0.gif:  PC bitmap, Windows 3.x format, 864 x -1100 x 24
    ScannedImage_B96B3CB1.tiff: PC bitmap, Windows 3.x format, 864 x -1100 x 24
    aRealTiffImage.tiff: TIFF image data, little-endian    (as opposite exemplae)

    I tried with three other scanners: the issue remains.

    It's important for me to get a native TIFF image because my scanner is duplex : it can produce a TIFF image that contain both sides of paper. With any other format, I get only one side scan.

    Versions : Windows 7 SP1 64 bits - Access 2010 32 bits - WIA 2.0

    Can anyone help me ?

    Sorry for my poor English ;-)

    Thanks

    Public Function ScanAnImage(ByVal MyScanner As String, RequestedFormat As String) As String
    ' --- returns filename or ""
    
        Dim ImageFormat As String
        Dim OutputFile As String
        Dim DevMgr As WIA.DeviceManager
        Dim ii As Integer
        Dim DevInfo As WIA.DeviceInfo
        Dim dev As WIA.Device
        Dim img As WIA.ImageFile
        
        ' ---
        ScanAnImage = ""
        
        ' --- requested format
        Select Case RequestedFormat
        Case "tiff"
            ImageFormat = wiaFormatTIFF
        Case "gif"
            ImageFormat = wiaFormatGIF
        Case "bmp"
            ImageFormat = wiaFormatBMP
        Case "jpg"
            ImageFormat = wiaFormatJPEG
        Case "png"
            ImageFormat = wiaFormatPNG
        Case Else
            GoTo ByeBye
        End Select
        
        ' ---
        OutputFile = Environ("TEMP") & "\ScannedImage_" & Mid(ImageFormat, 2, 8) & "." & RequestedFormat
        With New FileSystemObject
            If .FileExists(OutputFile) Then .DeleteFile OutputFile
        End With
        
        ' ---
        Set DevMgr = New WIA.DeviceManager
        For ii = 1 To DevMgr.DeviceInfos().Count
            Debug.Print "DevMgr.DeviceInfos(" & ii & ").Properties!Name.Value = " & DevMgr.DeviceInfos(ii).Properties!Name.Value
            If DevMgr.DeviceInfos(ii).Properties!Name.Value = MyScanner Then Set DevInfo = DevMgr.DeviceInfos(ii)
        Next ii
        
        ' ---
        Set dev = DevInfo.Connect
            
        ' ---
        Set img = dev.Items(1).Transfer(ImageFormat)
        
        ' --- check image format
        Debug.Print "img.FormatID=" & img.FormatID
        Debug.Print "ImageFormat =" & ImageFormat
        If img.FormatID = ImageFormat Then
            Debug.Print "==> is requested format"
        Else
            Debug.Print "==> is NOT requested format"
        End If
        
        ' ---
        img.SaveFile OutputFile
        
        ScanAnImage = OutputFile
    
    ByeBye:
        Set DevMgr = Nothing
        Set DevInfo = Nothing
        Set dev = Nothing
        Set img = Nothing
    
    End Function

    • Edited by x222 Friday, September 1, 2017 4:18 PM spelling
    Friday, September 1, 2017 4:14 PM