VBA – Reading a Base64 Element into XML and using as Byte Array

Ok, more VBA

Getting a response back as Base64, but when trying to convert it from XML to binary data just having issues.. coming back as ASCII which converts wacked.

What you need to do:

Dim MyInfo As MSXML2.IXMLDOMNodeList
Set MyInfo = xmlDoc.getElementsByTagName(“MyBase64Element”)
MyInfo .Item(0).DataType = “bin.base64″

Dim image() As Byte
image = MyInfo .Item(0).nodeTypedValue

then you can use it in a byte array and convert to an image or whatever datatype you need.

The key here is overriding the type (probably Variant/String by default) to “bin.base64″ and then making sure to use the “nodeTypedValue”

This one threw me for a loop for a little while :)

About these ads

2 thoughts on “VBA – Reading a Base64 Element into XML and using as Byte Array”

  1. Hi,

    Great post, thanks very much.

    Now the inevitable begging part:

    Do you happen to have a solution for doing it the other way around?

    ie I need to send image and other filetypes (eg PDF) as base64 encoding via XML to a MySQL 5 database.

    As I don’t want to hold the images in Access (there are 5,000+!), I thought that as the DOMDocument was being created, when I reached an Image or File part of the export, I could go to the file on the filesystem, import the file as an object, and then add the Base64 encoding value to as an XML node, then destroy the object.

    That’s my theory. I have no idea how to implement it though!

    Any pointers would be welcome :)

    Like

  2. Aha! I found teh answer myself:

    I just need to use the following:

    nodImage.DataType = “bin.base64″
    nodImage.nodeTypedValue = rsTables!FileOLE.Value

    where nodImage is an IXMLDOM Element and the FileOLE field is an OLE Object (just for testing purposes – I’ll load it from file in the final version)

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s