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
michaael_lmichaael_l 

Accessing word document hosted on Salesforce from S-control with activex control

Hello everybody
I have a problem with accesing a ms word document from within S-control

     var w=new ActiveXObject('Word.Application');
var docText;
var documentUrl="/servlet/servlet.FileDownload?file=01570000000ZGR2";
var obj;
if (w != null)
    {
    w.Visible = true;
    w.Documents.Open(documentUrl);

I get "file is corrupted" error, but if i download and open the file that URL points to it works fine...
Anybody now a way around it?
gsickalgsickal
documents are stored in binary format in salesforce. when you use the ajax toolkit with scontrols, you need to encode and decode documents when you store and retrieve them from salesforce.  in this case you need to decode your attachment (document) first before you can view it.  this is not needed when you simply click on view and download from the file from the salesforce ui because the ui takes care of the decoding for you behind the scenes.
michaael_lmichaael_l
Ok. But does it mean that I have to download document to local drive(which I don't think is possible with java script) and then decode it or can it be done "in air"? I would  appreciate any code samples.
gsickalgsickal
You can get the id of the attachment in the scontrol and pass this to a piece of code written as an activex control that runs in the browser.  this code could be a .net program that uses the office toolkit to access the salesforce api the same way the ajax toolkit does,  then your .net code would retrieve the attachment (since .net can run without browser security restrictions) to your desktop and could decode it using common base64 decoding algorithms.
michaael_lmichaael_l
Thanks for explanations. Does that means that there's  no way to open the Word file from within S-control only? I mean if you look at the code snippet above you'll see that what I need to do is actually just open the file and populate some table cells in it. Ideally it just open the file from S-control and do some stuff with it...
gsickalgsickal
The problem is base64 encoding.  The document your code points to is encoded on the salesforce server in base64 binary format.  So your code in the scontrol opens word using the Open method no problem, but this method assumes the document is unencoded and hence gets confused when it opens.  if you base64 decode it first in the scontrol (but this is horriblty slow and inefficient in javascript) and then use your code to open the document it will work.  Word's open command requires a physical document localion so you typically would save the document locally to your disk, decode it, and then open it.  That's where you run into problems since javascript won't let you do this (save the document to disk).