+ Start a Discussion
Sowmya TG 4Sowmya TG 4 

How to encode mail merge word document in Apex?

We have done customization for mail merge. It is working for xml, pdf formats. But for word document it is giving document body something like this "UEsDBBQABgAIAAAAIQDfpNJsWgEAACAFAAATAAgCW0NvbnRlbnRfVHlwZXNdLnhtbCCiBAIooAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" and our logic is not working.

Below is our code:
List<Document> d1=[select id, Name, body from Document where id = '0151q000000BP1I'];
String str = EncodingUtil.base64Encode(d1[0].body);

Please suggest any work around for this.
chathura ranasinghe 10chathura ranasinghe 10
Hi Sowmya

Did you save the document body as BLOB ? if yes then you can use EncodingUtil Class. 

Please follow the links : 

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_encodingUtil.htm

Some similar scenarios : 

https://stackoverflow.com/questions/13028577/convert-the-blob-content-of-my-word-document-into-string-in-apex-controller
https://developer.salesforce.com/forums/?id=906F0000000AzjVIAS
https://www.salesforcetutorial.com/sending-email-with-attached-document-by-using-apex/

if the links help you, Please mark this as the Best answer.

Thanks
Chathura