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

Posted on 2012-09-12
Medium Priority
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
  • 3
  • 2
LVL 40

Expert Comment

ID: 38398541
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

ID: 38400351

The function works fine.  

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

Expert Comment

ID: 38401055
You like to have structures from XML library in vba?

Author Comment

ID: 38401733
For the project the algorythm is what I was looking for to endcode and decode.
LVL 40

Accepted Solution

als315 earned 2000 total points
ID: 38401971
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 http://www.motobit.com/tips/detpg_Base64/
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?

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re using QODBC to update QuickBooks data from Microsoft® Access but Access is not showing the updated data, you could have set up QODBC incorrectly.
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

864 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