function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
gsickalgsickal 

Encoding/Decoding Word documents the Office Toolkit and Base64

I have wrritten an addin using the Office Tookit that takes a Word document and saves it as an Attachment in the salesforce system.  The problem is, when I set the body field as the Word document, it isn't encoded in Base64 (the API docs say the client app is responsible for encoding and decoding the binary attachments as Base64 when sending or receiving objects including Attachments, MailMerge templates, and S-Controls.
 
I have googled and searched the Microsoft and MSDN pages and haven't found anything yet.  Does anyone know of any converter or have any VBA code that will decode/encode a Word document back and forth between Base64?  Thanks
Ron HessRon Hess
found one
http://www.motobit.com/tips/detpg_Base64Encode/
gsickalgsickal
Thanks very much Ron.  I figured out from reading this article that writing a base64 encode/decode function in vba is a very bad thing since it is horribly inefficient and slows exponentially as the filesize increases.  No wonder no one has written one for vba.  That is why the site you mentioned above even said they have a c++ implementation.  I also found a public domain c++ implementation on sourceforge, so what I will probably end up doing is writing a com wrapper around this c++ code that can be called from with vba.  Here is the the sourceforge link I found: http://libb64.sourceforge.net/  Again, thank you very much for your help.
SuperfellSuperfell
I thought the office toolkit did the base64 stuff for you, just pass it an array of bytes.
gsickalgsickal

I would have thought it worked too, since using the regular API the SOAP layer takes care of this for you.  But... when I tried it from code, i.e.

    '// create a new attachment
    Dim oAttachment As SObject3
    Set oAttachment = oSession.CreateObject("Attachment")
   
    '// set attachment properties from some values in the document
    oAttachment("ParentId") = parentId
    oAttachment("OwnerId") = ownerId
    oAttachment("Name") = ActiveDocument.Name
    
    '// we're responsible for Base64 encoding/decoding
    oAttachment("Body") = ActiveDocument
   
    '// save the attachment to salesforce
    oAttachment.Create

The attachment got saved to salesforce fine, but when I tried to view it, it was garbage.  After re-reading the API docs for the Toolkit, I came across this last bullet point in the section near the front of the manual called "Comparing the Office Toolkit and the Web Services API":

  • The Office Toolkit represents data in objects as COM data types, as described in Field Types. Note that, for Base64 type data (such as Attachments), client applications are responsible for the conversion of Base64 data between binary and String formats.
  • Well, there you have it.  Now I had to figure out a way to encode the Word document to base 64 (it works automatically via SOAP when you attach a normal attachment through the salesforce ui, just not through the office toolkit), and decode it back when I retrieved attachments.  Anyway, it's gotten a little more complicated now because I have to create a COM object in C++ that does the actual base64 encoding/decoding that I can call from with VBA for Word...

warrenlesterwarrenlester

If you have already found a solution to this, I found this one:

http://download.paipai.net/texts/midCrypt.htm

It is designed for use in ASP(.NET) but should also work in VBA.

 

Warren Lester

gsickalgsickal
Thanks very much for the suggestion.  I ended up writing a custom COM dll in c.net that gets installed on the client and does the base64 encoding/decoding very fast.  The c code to do this is pretty much public domain, the c.net project was what i really added to do this, then I could call the dll from within VB code like a normal COM call.  Thanks again
RickNTARickNTA

Old thread, but you guys helped me out (and I've gotten lots of help from Ron Hess over the years) so here's something that may help others.  I stumbled across what seems to be an elegant and quick solution:

 

http://www.nonhostile.com/howto-encode-decode-base64-vb6.asp

 

He uses MS XML and an XML DOM document to convert from a byte array to Base64.  We were already using MS XML in our app so this fit well.  Still in initial testing but so far works well and seems pretty fast (haven't done detailed timing but the entire process to create the array, convert it and upload to an Opp Attachment is about 4 sec for a 150 KB PDF, and my machine isn't real fast).

 

He also has a simple proc to populate the byte array needed for conversion:

 

http://www.nonhostile.com/howto-read-write-byte-arrays-files-vb6.asp

 

I literally pasted both these into my VBA (Access) code with no changes and they work perfectly for PDF, Word, and Excel so far.

 

Good luck to all!