Rotate selected images in a folder using VBA and I

2019-08-22 10:25发布

问题:

I want to check a folder if a rotated version of my images exists, if it doesn't I want to use ImageMagick to automatically do it for me (or another program if it would be a better option). This is my code:

Dim imageDomain As String
Dim imagePath As String
Dim rotatePath As String
Dim rotateBool As Boolean
Dim imageRotator As Integer
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("SELECT Afbeelding FROM Products")

imageDomain = CurrentProject.Path & "\folder\"

If Not (rs.EOF And rs.BOF) Then
    rs.MoveFirst
    Do Until rs.EOF = True

        If Not IsNull(rs!Afbeelding) Then
            rotatePath = imageDomain & "rotate\" & rs!Afbeelding
            rotateBool = Len(Dir(rotatePath))

            If Not rotateBool Then
                imagePath = imageDomain & rs!Afbeelding
                imageRotator = Shell("cmd.exe /c convert.exe -rotate ""270"" " & imagePath & " " & rotatePath)
            End If
        End If

        rs.MoveNext
    Loop
Else
    MsgBox "There are no records in the recordset."
End If

rs.Close
Set rs = Nothing

The code gives an overflow error at the shell command. How can I selectively rotate the images I want with ImageMagick using VBA? Using a batch file would rotate all images in the folder?

回答1:

The COM+ object did the trick.

Dim img As Object
Set img = CreateObject("ImageMagickObject.MagickImage.1")

In the loop:

imageRotator = img.Convert(imagePath, "-rotate", "270", rotatePath)