Auto Rotate Photos

Tophan

Registered User.
Local time
Today, 13:10
Joined
Mar 27, 2011
Messages
381
Good morning,

Following up on a question previously posted (and solved) in the Tables forum on how to import a file path batch, I have encountered a problem. Using the VBA code provided by @Pat Hartman and @theDBguy, I was able to import all file paths for photos from a Windows Explorer folder. However, I have noticed that any photo taken in portrait mode is rotated to landscape in the report. Is there a way to prevent this from happening?

I was scouring through the various threads on this website and saw something about WIA automation but I'm not sure where to insert that code and how it would affect the import code.

Thanking you in advance for any help provided.
 
copy this code in module:
Code:
Public Function WIA_RotateImage(sInitialImage As String, _
                                lRotAng As Long, _
                                Optional sOutputImage As String) As Boolean
    On Error GoTo Error_Handler

    Dim Img As Object, IP As Object
    Dim ext As String, tfSameFile As Boolean
    Set Img = CreateObject("WIA.ImageFile")
    Set IP = CreateObject("WIA.ImageProcess")

    IP.Filters.Add IP.FilterInfos("RotateFlip").FilterID
    IP.Filters(1).Properties("RotationAngle") = lRotAng

    Img.LoadFile sInitialImage
    Set Img = IP.Apply(Img)

    tfSameFile = (sOutputImage = "") Or (sInitialImage = sOutputImage)
    If tfSameFile Then
        ext = getFileExt(sInitialImage)
        sOutputImage = Replace$(sInitialImage, ext, "") & "_tmp$" & ext
    End If

    Img.SaveFile sOutputImage
    WIA_RotateImage = True
    
    If tfSameFile Then
        Kill sInitialImage
        Name sOutputImage As sInitialImage
    End If
Exit_Procedure:
    Exit Function

Error_Handler:
    WIA_RotateImage = False
    Resume Exit_Procedure
End Function


Private Function getFileExt(ByVal sImage As String) As String
Dim i As Integer
i = InStrRev(sImage, ".")
If i <> 0 Then
    getFileExt = Mid$(sImage, i)
End If
End Function

then check which image has wrong orientation (landscape) and run the function against that image.
example:

Code:
sImage = Environ$("userprofile") & "\pictures\ssms21_02.png"
Call WIA_RotateImage(sImage, 270)
 

Users who are viewing this thread

Back
Top Bottom