You need to sign in to do that
Don't have an account?
Shekhar Gadewar 16
Error while creating word file in Visualforce
Hi.
I am trying to create a word file by visualforce.
I am trying to attach it as an attachement for an accout record.
Everything is working fine, the file is getting generated and getting attached to current account record.
But when I try to open the word file for reading.
it's saying :
Can some guide, where I am making the mistake?
Thanks in advance.
=================
Here is the code I ma using:
I am trying to create a word file by visualforce.
I am trying to attach it as an attachement for an accout record.
Everything is working fine, the file is getting generated and getting attached to current account record.
But when I try to open the word file for reading.
it's saying :
Can some guide, where I am making the mistake?
Thanks in advance.
=================
Here is the code I ma using:
<apex:page controller="WordAttachementGeneratorController"> <apex:sectionHeader title="Word Example" description="Example of how to attach a Word to a record."/> <apex:form > <apex:pageBlock title="Word Input"> <apex:pageBlockButtons > <apex:commandButton action="{!saveWord}" /> </apex:pageBlockButtons> <apex:pageMessages /> <apex:pageBlockSection > <apex:pageBlockSectionItem > <apex:outputLabel value="File Name" for="pdfName"/> <apex:inputText value="{!WordName}" id="pdfName"/> </apex:pageBlockSectionItem> <apex:pageBlockSectionItem > <apex:outputLabel value="Account ID" for="id"/> <apex:inputText value="{!parentId}" id="id"/> </apex:pageBlockSectionItem> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>
public with sharing class WordAttachementGeneratorController{ public ID parentId {get;set;} public String WordName {get;set;} public PageReference saveWord() { PageReference pdf = Page.PdfGeneratorTemplate; // create the new attachment Attachment attach = new Attachment(); // the contents of the attachment from the pdf Blob body; try { // returns the output of the page as a PDF body = pdf.getContent(); } catch (VisualforceException e) { body = Blob.valueOf('Some Text'); } attach.Body = body; // add the user entered name attach.Name = WordName ; attach.IsPrivate = false; // attach the pdf to the account attach.ParentId =parentId ; insert attach; // send the user to the account to view results return new PageReference('/'+parentId); } }
<apex:page standardController="Account" contentType="application/msword#sfdcsrini.doc" cache="true"> <html xmlns:w="urn:schemas-microsoft-com:office:word"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <body> <apex:outputText title="Welcome to word"/> <br/> <br/> <apex:pageBlock > <div style="text-align:left" > Hello! </div> </apex:pageBlock> </body> </html> </apex:page>
You dont have any issue in your code. It seems to me your problem is either your word version or your antivirus. Please consult your System admin.
But I have also checked that.
Didnt find any.
Moreover, PDF file is working fine.
Only Word file is making the issue.