0

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?

June7
  • 19,874
  • 8
  • 24
  • 34
Arne Clicteur
  • 49
  • 1
  • 12
  • 1
    Note that ImageMagick has a COM+ object available. If you're going to use it in VBA, I suggest you use that over using `Shell` (since you can error trap more easily). See the [installation guide](https://www.imagemagick.org/script/ImageMagickObject.php). I might write up a quick guide using it later, currently don't have ImageMagick available to test. – Erik A Jan 04 '18 at 12:28

1 Answers1

0

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)
Arne Clicteur
  • 49
  • 1
  • 12