how to convert xml base64 string gif/jpg/png image file using vba

Posted on 2012-09-12
Last Modified: 2012-09-16
I am not sure why this is not working?

I am using vba:

If have a base64 gif string (here's the excerpt):

R0lGODlhsAQIB/cAAAAAAI  ... <more here> ... u+I1r3rdK1/76te/Ajawgh1jQAAAOw==

I attempted to create a binary file by loading the xml above into a string: base64_string

   dim btArr() As Byte
   dim base64_string as string

   base64_string=<xml from above>
   btArr = base64_string

   Open 'image.gif" For Binary As #1
   Put #1, 1, Base64decode(btArr)
   Close #1

But when I try to see the image by opening the file, it says the image format is invalid.  

What else do I need to do to make the file a  gif file?
Question by:kperelman
    LVL 39

    Expert Comment

    Code from this page:
    works for me.
    Sub save_gif()
    Dim A As String
    A = "R0lGODlhEAAOALMAAOazToeHh0tLS/7LZv/0jvb29t/f3//Ub//ge8WSLf/rhf/3kdbW1mxsbP//mf///yH5BAAAAAAALAAAAAAQAA4AAARe8L1Ekyky67QZ1hLnjM5UUde0ECwLJoExKcppV0aCcGCmTIHEIUEqjgaORCMxIC6e0CcguWw6aFjsVMkkIr7g77ZKPJjPZqIyd7sJAgVGoEGv2xsBxqNgYPj/gAwXEQA7"
    Open "c:\tmp\image.gif" For Binary As #1
       Put #1, 1, DecodeBase64(A)
    Close #1
    End Sub
    Private Function DecodeBase64(ByVal strData As String) As Byte()
        Dim objXML As MSXML2.DOMDocument
        Dim objNode As MSXML2.IXMLDOMElement
        ' help from MSXML
        Set objXML = New MSXML2.DOMDocument
        Set objNode = objXML.createElement("b64")
        objNode.DataType = "bin.base64"
        objNode.Text = strData
        DecodeBase64 = objNode.nodeTypedValue
        ' thanks, bye
        Set objNode = Nothing
        Set objXML = Nothing
    End Function

    Open in new window

    You need reference to Microsoft XML library (I've used 6.0)

    Author Comment


    The function works fine.  

    Is there an algorythm written in a vba function that does the same thing?
    LVL 39

    Expert Comment

    You like to have structures from XML library in vba?

    Author Comment

    For the project the algorythm is what I was looking for to endcode and decode.
    LVL 39

    Accepted Solution

    I don't know what is Base64decode in your code, but with function from Antonin Foller's site:
    this code also working without problems:
    Dim A As String
    A = "R0lGODlhEAAOALMAAOazToeHh0tLS/7LZv/0jvb29t/f3//Ub//ge8WSLf/rhf/3kdbW1mxsbP//mf///yH5BAAAAAAALAAAAAAQAA4AAARe8L1Ekyky67QZ1hLnjM5UUde0ECwLJoExKcppV0aCcGCmTIHEIUEqjgaORCMxIC6e0CcguWw6aFjsVMkkIr7g77ZKPJjPZqIyd7sJAgVGoEGv2xsBxqNgYPj/gAwXEQA7"
    Open "c:\tmp\image.gif" For Binary As #1
       Put #1, 1, Base64Decode(A) ' Antonin Foller's function
    Close #1

    Open in new window

    And your original code also working. May be your base64 string is wrong or you've used some other function?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
    Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
    Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now