Link to home
Start Free TrialLog in
Avatar of Eileen Murphy
Eileen MurphyFlag for United States of America

asked on

VBA Base64 output assistance needed

Hey Experts! I am implementing a UPS and a FedEx Rest App into a VBA/Access project. I have the code pretty much done and it's working - grabbing the tracking #'s and the shipment details, etc. The Base64 decoding to generate the labels is killing me. 


I have the Base64 sample/modules/bas code, etc. I have no idea how to take the results of that and save it to a file/pdf/image whatever so I can print the shipping labels. 


Any ideas are welcome. Thanks all.


By the way -- I will share the results of the VBA app integration once I have it completed. Others may find it useful. 


Jim Dettman - if you see this - I will def send that to you. 

Avatar of Ron Malmstead
Ron Malmstead
Flag of United States of America image

Base64 is a 'String' representation of 'Bytes'.

So essentially, what you need to do, is to convert the string back into a byte array, and output that to file.  I'm not familiar with the FedEx or UPS api, but you will need to know the intended file type (ie .PNG), in order to save the with proper extension.


Base64Encode/Decode

https://www.codestack.net/visual-basic/algorithms/data/encoding/base64/


Save bytes to a file

https://www.codestack.net/visual-basic/algorithms/fso/io/write-binary-file/






Avatar of Eileen Murphy

ASKER

Thanks Ron. I think I saw this but am going to give it a try. This is all new to me - so wish me luck :) I'll keep you posted.


**UPDATE** -- still struggling with this. 

Here is where I'm at. Anyone know why I'm getting the below error???


'Decode Base64 string into a byte array

Public Function DecodeBase64(ByVal Base64String As String) As Byte()

    With CreateObject("MSXML2.DOMDocument").createElement("b64")

        .DataType = "bin.base64"

        .Text = Base64String

        DecodeBase64 = .nodeTypedValue

    End With

End Function

--------------------------------------------------------------------------------------------------------------------

'Take a byte array and write to a file

Public Sub WriteByteArrayToFile(FileData() As Byte, ByVal FilePath As String)

    Dim FileNumber As Long: FileNumber = FreeFile

    Open FilePath For Binary Access Write As #FileNumber

    Put #FileNumber, 1, FileData

    Close #FileNumber

End Sub

--------------------------------------------------------------------------------------------------------------------

Function TESTIT()

Dim strbyte As String


 strbyte = "8o8I5mlgs7sWrxzDtfFhrjSadZAiCsX/lo4GbzImgTDliIIPRuhxYLe7OZqcG6aJXbVXA17MWZrDBrSKZ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IaYR7ELc5hkV4KxW/3xZxrIPH2IVW4EbogV/8DHO+V+jJavFubi3h1/CCrfYpt5lIZ+vOLq1n1wm4Ar3kIcxej2JKklMRzSh0t57oJtmIIkqL84CQHdAtYAd09wlxbD412ZvZ4o2myOaDaWItRPn1dtonCbq93Y6VHKr0CaizHUV+6mMA40jDydLD/PGAJusdws4zgAg6B+il6Q4UDAXoBp3tIQfzNvAqbP0DCtlVXSQZxIPwgjLQVdAPwgvCQFh7QC2PZWfPNQAAeAOEFwCgM7gawBTcQoMGsHgBADqD8AIAdAbhBQDoDMILANAZhBcAoDMILwBAZ0iRAQCKsBEwiMULANCZ/10dqEOzcW04AAAAAElFTkSuQmCC"


WriteByteArrayToFile DecodeBase64(strbyte), "c:\UPS\Picture.png"


This generates an error: "Error parsing.... [above string] as bin.base64 datatype.


--------------------------------------------------------------------------------------------------------------------    

ORIGINAL Function TESTIT()  UPS returns the above -- very long string of bytes - whatever -- the below sample does not. It did write a file to the folder - but it was blank... 

'WriteByteArrayToFile ( DecodeBase64("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR4nGP6zwAAAgcBApocMXEAAAAASUVORK5CYII="), "c:\UPS\Picture.png"


End Function


<<This generates an error: "Error parsing.... [above string] as bin.base64 datatype. >>


 Something in the string does not decode properly...are you sure you got the entire string?


Jim.

Hey Jim! I do - UPS returns it and I grab it... It works with a test string of A/N text  -- provided with the test code I found online -- This string is 1500+ characters long so I wondered if that was it.... 


ASKER CERTIFIED SOLUTION
Avatar of Jim Dettman (EE MVE)
Jim Dettman (EE MVE)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

Hey all -- I'll be working on this tomorrow so I'll keep you posted. THANK YOU!

I noticed there are spaces in the string, which normally it does not have when base64 encoded.


Can you post the string in its entirety in a code block comment?

The string seems very small to be complete.

Also, the file type is confirmed to be PNG?

Hey there Ron -- I would prefer the output to be pdf so it can be saved/viewed easily by all users...


Here is the actual string...


8o8I5mlgs7sWrxzDtfFhrjSadZAiCsX/lo4GbzImgTDliIIPRuhxYLe7OZqcG6aJXbVXA17MWZrDBrSKZl43ZpNUZBzWmIKAuPZsXinibVwJyhniU5jOkEB0x2NHIlqx9DLvsH6hEbs5GpYXrweaL1Gp219997vHb0GzSh3oLCExXMCG7SIaqixRqchnSqQlESgCdbOpzFUz1i2hfd6orjaRPK16bg6SPebiKvdFgVSnR0IPsCycq0ya3YkpPF83JzTgY4Oo5ESxguwyYGrITN7q/kYpomZYYnQAVbWsGRupvdgnI6lCCeLCM6fCESv53QootMAukF/A+HsW7xhisc93IFC0B61cA2DO1AoLz58gKOohjnEc93fJ313OfLJw62ypNnWGT1wE6yAq1BjvUmzk03kOcqYNgMtLJYCgGCqCyhyja3mali7NpqsFostF6IZBovnBxY4uYCimCNevQGJd6FMik7Xh2ucMFaPOtWivdxCg0xOr1xL u + OEzZPSXZ9HQbAASf0dNVeh/IY936JyHs9ezMlI0/mM6PTYf0fGjOiV4VcPWCVK7naqh6EPIX1yQNHuWFNzh1K8Uyii2ol7Gkk2snOnPFy+Cjg5AeTIsrzimv33iGbkONU93KXPIfMMHftY9775wLG7obP2EfNnidM5qK7oJE9rKTpcy7SVQ254ldDPgVYCxbUTYQwzAdy10f748Q/f87pKfDQ3YlIXNHVT7r17fojCCVtxKhA7xBdeV6FbadAsFsC28t7029F/s0mmHztSMYI3CKNVahNHn9Is5sf7JHNo9OuH1ndibXTt6rBee8/62P93OvD9M0nPs90qbQlpM9io63A46YkeQLKOKkDOHCVFnWivOm8OuXAXSGfrcPyjuQo3y8Jwi1kNtQee2Y6IankpYHAMAEe9u7l6/m+SK3PudWOb642+UsveSXAniXzUyw8D4PohqO/PFPJJOck3CbdNspbpo2oF5GckZ41+mxg2tki6nR0sqmgwC0Juw+hbeZhTe5 xU8PvatK6t7U2/pt9xp09lJwAwQABsmiGooUpn57Ldq8oXv8WsvYscBCOLgMphtoIN1z7Rdz7uf1lpVt27vAkiPYhwvyc6YFJmwUM4TCxxvZmWFdvrZjeoY49y7tBx3Y0V3W65wk3j2cmZTuJMJ7rdMuGSCb5jqv5JUEWNkIaYR7ELc5hkV4KxW/3xZxrIPH2IVW4EbogV/8DHO+V+jJavFubi3h1/CCrfYpt5lIZ+vOLq1n1wm4Ar3kIcxej2JKklMRzSh0t57oJtmIIkqL84CQHdAtYAd09wlxbD412ZvZ4o2myOaDaWItRPn1dtonCbq93Y6VHKr0CaizHUV+6mMA40jDydLD/PGAJusdws4zgAg6B+il6Q4UDAXoBp3tIQfzNvAqbP0DCtlVXSQZxIPwgjLQVdAPwgvCQFh7QC2PZWfPNQAAeAOEFwCgM7gawBTcQoMGsHgBADqD8AIAdAbhBQDoDMILANAZhBcAoDMILwBAZ0iRAQCKsBEwiMULANCZ/10dqEOzcW04AAAAAElFTkSuQmCC

Jim testing your suggestion now...

I tried the code Jim - and am not getting the error (so thank you!) - I tried to save the output as png, bmp, and jpg - see attached. I tried an online decode and get no image - just more unreadable text.... see attached.Picture.bmpPicture.jpgPicture.png


Can't open them -- obviously am doing something wrong -- UGH....

Hey Ron -- I copied the string in the above response -- I went through in subsequent testing to ensure there were no spaces -- 


<<I tried the code Jim - and am not getting the error (so thank you!) - I tried to save the output as png, bmp, and jpg - see attached. I tried an online decode and get no image - just more unreadable text.... see attached >>


 According to the API you are calling, what is it supposed to return?


Jim.

I believe it is a png. I was just trying other types. 

<<I believe it is a png. I was just trying other types.  >>


The API should spell out what it is returning.  To save it as anything else, you'd need a conversion process.    Double check what the API is supposed to be returning and save it as that.  If it's still not working, then something is not right.


If you post the URL for the API and the call you are making, we can look at it and possibly offer some ideas on what might be wrong.


Jim.

Hey Jim - this is the base URL (a sandbox on UPS) https://wwwcie.ups.com

I'll be working on this some more later today. I appreciate all you've done to help so far.