PDA

View Full Version : VBA Combine PNG/JPG to an animated Gif



biancoluke
05-19-2022, 01:35 AM
I have been searching on google on how to combine multiple PNG/JPG to an animated GIF using EXCEL VBA but found none. Can someone direct me in the right direction if this is possible?


Basically i have already done the function to read png/jpg files from a specific file but require a function to combine (with a seconds) and save as animated GIF.


Thanks in advance.

georgiboy
05-20-2022, 03:24 AM
Hi biancoluke,

Welcome to the forum.

Excel is a bit restricted when it comes to editing images, I did however find something you might want to try out.

If you download and install a free program called 'ImageMagick' (easy to find on Google) we can use a command line instruction to collate the seperate still GIF's into one animated GIF :yes

See code below:

Sub AnimateGIF()
Dim sPath As String, sCMD As String, sPRMs As String
Dim sFiles As String, sTarget As String, sRunString As String
Dim ConvertLoc As String, FrameDelay As Integer, GIFoutName As String
Dim WshShell As Object

sPath = "C:\Users\jbloggs\Desktop\test" ' < Folder where still GIF's to be converted reside
ConvertLoc = "C:\Program Files\ImageMagick-7.1.0-Q16-HDRI" ' < Install folder of ImageMagic
FrameDelay = 50 ' < Delay you want between frames of the GIF
GIFoutName = "CompletedGIF.gif" ' < Output GIF file name

sCMD = """" & ConvertLoc & "\convert"" "
sPRMs = "-delay " & FrameDelay & " -loop 0 "
sFiles = sPath & "\*.gif "
sTarget = sPath & "\" & GIFoutName
sRunString = sCMD & sPRMs & sFiles & sTarget
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run sRunString, 1
Set WshShell = Nothing
MsgBox "File: " & vbNewLine & sPath & "\" & GIFoutName & vbNewLine & "Was created"
End Sub

It will place tha animated GIF in the same location as the stills.

I did try to reference the dll file for the above program but it did not go well so i went with command line.

Hope this helps

georgiboy
05-23-2022, 02:18 AM
I have created an updated version in the attached, supports: jpg, png & gif.

As stated before it relies on the install of 'ImageMagick'

Mavenicet
02-28-2024, 03:54 AM
You're on the right track with your function to read the image files. To combine them into a GIF, you'll need to look into libraries or APIs that support image manipulation and GIF creation. There are several options out there, like ImageMagick or the .NET Framework's System.Drawing namespace.
Once you've got your images loaded, you can set the duration for each frame (in seconds) and save the result as an animated GIF. Don't forget to compress image (https://jpeg-optimizer.com/) sizes to keep your GIF file manageable!
Good luck with your VBA adventure!