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
Shekhar Gadewar 16Shekhar 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 :

User-added image

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>




SarfarajSarfaraj
Hi Shekhar

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.
Shekhar Gadewar 16Shekhar Gadewar 16
Oh, Thanks Akram.
But I have also checked that.

Didnt find any.

Moreover, PDF  file is working fine.
Only Word file is making the issue.
Avijit Chakraborty 11Avijit Chakraborty 11
https://www.salesforce.com/us/developer/docs/pages/Content/pages_styling_content_type.htm . see this use content type 
shiv@SFDCshiv@SFDC
Remove return type from -- public PageReference saveWord()



shiv@SFDCshiv@SFDC
public with sharing class WordAttachementGeneratorController{

  public ID parentId {get;set;}
  public String WordName {get;set;}

  public 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;

  }

}
And if you want to redirect to user on anohter page. You can oncomplete event.