Consulting

Results 1 to 4 of 4

Thread: VBA Combine PNG/JPG to an animated Gif

  1. #1

    VBA Combine PNG/JPG to an animated Gif

    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.

  2. #2
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,158
    Location
    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

    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
    Last edited by georgiboy; 05-20-2022 at 05:42 AM.
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved

    Excel 365, Version 2401, Build 17231.20084

  3. #3
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,158
    Location
    I have created an updated version in the attached, supports: jpg, png & gif.

    As stated before it relies on the install of 'ImageMagick'
    Attached Files Attached Files
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved

    Excel 365, Version 2401, Build 17231.20084

  4. #4
    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 sizes to keep your GIF file manageable!
    Good luck with your VBA adventure!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •