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
Ganesh RajputGanesh Rajput 

send visualforce page as word document attachment.

I have a requirement to send visualforce(Include Images from the content document ) page attachment as pdf and as a word document. I am done with PDF but stuck at the second requirement want to send it as a word document file.

Controller 
public class CargoQuoteDocController {
public Quote__c quoteObj{get;set;}
    public User user{get;set;}
    public String todysDate{get;set;}
    public String DepartDate{get;set;}
    public List<String> ImageURLs{get;set;}
    public static String Thank_You_Message{get;set;}
    public static String PRICE_INCLUDES{get;set;}
    public static String PRICE_DOES_NOT_INCLUDE{get;set;}
    public String quo;  
    public String D_Date{get;set;}
    public String A_Date{get;set;}
    public ACType__c acType{get;set;}
    public Quote__c quote{get;set;}
    public Opportunity oppo{get;set;}
    public List<String> imgIds = new List<String>();
        
    public void setValues(){
        ImageURLs = new List<String>();
        //List<String> imgIds = new List<String>();
        quo = apexPages.currentPage().getParameters().get('recId');
        PRICE_DOES_NOT_INCLUDE = apexPages.currentPage().getParameters().get('segThree');//quoteTerms.PRICE_DOES_NOT_INCLUDE__c;
        Thank_You_Message = apexPages.currentPage().getParameters().get('segOne');//quoteTerms.Thank_You_Message__c;
        PRICE_INCLUDES = apexPages.currentPage().getParameters().get('segTwo');//quoteTerms.PRICE_INCLUDES__c;
        Integer length = Integer.valueOf(apexPages.currentPage().getParameters().get('length'));
        
        if(length>0){
            for(Integer i=0; i<length;i++){
                imgIds.add(apexPages.currentPage().getParameters().get('img'+i));
            }
        }
        
        for(String img:imgIds){
            ImageURLs.add(URL.getSalesforceBaseUrl().toExternalForm()+'/sfc/servlet.shepherd/version/download/'+img);  	
        }
        
        for(String img:ImageURLs){
            System.debug('ImageURLs == '+img);  
        }
        System.debug(quoteObj);
        
        user = [SELECT Id, Name,Email, MobilePhone FROM User WHERE Id=:UserInfo.getUserId()];
        
        try{
            System.debug('quote == '+quo);
            this.quote = [SELECT ID, Name, Opportunity__c, Aircraft__c, AircraftType__c,Contact_Name__c, Contact_Email__c,
                          (SELECT Id, From__r.Name, To__r.Name,Airway_Time__c,Duration__c,DepartDate__c,DepartTime__c,
                           Temp_Duration__c, ArriveTime__c, ArriveDate__c,PAX__c,CGO__c FROM Quote_Sectors__r) FROM Quote__c WHERE id=:quo];
            this.oppo = [SELECT Id, Name, ContactId, OppID__c, Depart_Date__c, Cargo_lbs__c  
                         FROM Opportunity WHERE Id =: quote.Opportunity__c];  
            this.acType = [SELECT Id,Name, Load__c, Manufacturer__c,Volume__c,MaxKGS__c FROM ACType__c 
                           WHERE Id=:quote.AircraftType__c];
        }catch(Exception e){
            System.debug('Exception == '+e);
        }
        DateTime dT = System.now();
            todysDate = dT.month()+'/'+dT.day()+'/'+dT.year();
        if(oppo.Depart_Date__c != null){
            DateTime dT2 = oppo.Depart_Date__c;
            DepartDate = dT2.month()+'/'+dT2.day()+'/'+dT2.year(); 
            
        }
    }
}
Visualforce Page
<apex:page controller="CargoQuotePdfController" action="{!setValues}" sidebar="false"
           showHeader="false" contentType="application/vnd.msWord" cache="true">
    <center>
        <html>
            <body>
                <div class="slds-m-around--large">
                    <table width="700">
                        <td>
                            <center>
                                <!--Letter Heade-->  
                                <div class="header">
                                    <center>
                                        <!--<img src="{!$Resource.LetterHead}" width="700" hight="80"/>-->
                                    </center>
                                </div>
                                <!--To From Detils -->
                                <table width="700px">
                                    <tr>
                                        <td style="padding-bottom: 10px; width: 75%;text-align:left;">
                                            <h3>CHARTER FLIGHT</h3><br/>
                                            <b>TRIP # {!oppo.OppID__c}</b><br/>
                                            <p>QUOTATION # {!quote.Name}</p> 
                                        </td>
                                        <td style="padding-bottom: 10px; width: 30%;text-align:left;">
                                            DATE: {!todysDate}<br/><br/>
                                            TO: {!quote.Contact_Name__c}<br/>
                                            FROM: {!user.Name}
                                        </td>
                                    </tr>
                                </table>
                            </center>
                            <!--Thank you Message-->
                            <p>
                                <b>Dear {!quote.Contact_Name__c},</b><br/>
                            </p>
                            <div>
                                <div rows="5" cols="80" name="comment" form="usrform">
                                    {!Thank_You_Message}
                                </div>
                            </div>
                            <!--Iternanery-->
                            <p >
                                <br/><b>Proposed Itinerary</b><br/>
                            </p>
                            <table border="1px;"  width="700" >
                                <tr>
                                    <th style="padding-bottom: 10px; width: 15%; background-color:#4db8ff; color:white;"><center>Depart Date/Time</center></th>
                                    <th style="padding-bottom: 10px; width: 20%; background-color:#4db8ff; color:white;"><center>FROM</center></th>
                                    <th style="padding-bottom: 10px; width: 20%; background-color:#4db8ff; color:white;"><center>TO</center></th>
                                    <th style="padding-bottom: 10px; width: 15%; background-color:#4db8ff; color:white;"><center>Arrive<br/>Date/time</center></th>
                                    <th style="padding-bottom: 10px; width: 15%; background-color:#4db8ff; color:white;"><center>Duration</center></th>
                                    <th style="padding-bottom: 10px; width: 15%; background-color:#4db8ff; color:white;"><center>CGO</center></th>
                                </tr>
                                <apex:repeat value="{!quote.Quote_Sectors__r}" var="queSec" id="theRepeat">
                                    <tr>
                                        <td style="padding-bottom: 10px; width: 15%; background-color:#cbd6d3;"><center>{!queSec.DepartDate__c}<br/>{!queSec.DepartTime__c}</center></td>
                                        <td style="padding-bottom: 10px; width: 20%; background-color:#cbd6d3;"><center>{!queSec.From__r.Name}</center></td>
                                        <td style="padding-bottom: 10px; width: 20%; background-color:#cbd6d3;"><center>{!queSec.To__r.Name}</center></td>
                                        <td style="padding-bottom: 10px; width: 15%; background-color:#cbd6d3;"><center>{!queSec.ArriveDate__c}<br/>{!queSec.ArriveTime__c}</center></td>
                                        <td style="padding-bottom: 10px; width: 15%; background-color:#cbd6d3;"><center>{!queSec.Temp_Duration__c}</center></td>
                                        <td style="padding-bottom: 10px; width: 15%; background-color:#cbd6d3;"><center>{!queSec.CGO__c}</center></td>
                                    </tr>
                                </apex:repeat>
                            </table>
                            <br/><br/>
                            <!--Images-->
                            <b>
                                Aircraft Image <br/><br/>
                            </b>
                            <div style="margin-left:50px;width:700;min-height:300px">
                                <apex:repeat value="{!ImageURLs}" var="image" id="Images">
                                    <!--<img src="{!image}" style="width:200px; hight:200px"/>-->
                                    <apex:image url="{!image}" style="width:200px; hight:200px"/>
                                </apex:repeat>
                            </div>
                            <br/><br/>
                            <!--Price payload-->
                            <table border="1px" width="700">
                                <tr>
                                    <td rowspan="2" style="padding-bottom: 10px; width: 50%;margin-left:20px;">
                                        Aircraft: {!acType.Load__c} <br/>&nbsp;&nbsp;
                                        {!acType.Manufacturer__c} <br/>&nbsp;&nbsp;
                                        {!acType.Name}</td>
                                    <td style="margin-left:20px;">PAYLOAD: {!acType.MaxKGS__c}</td>
                                </tr>
                                <tr>
                                    <td style="margin-left:20px;">VOLUME: {!acType.Volume__c}</td>
                                </tr>
                                <tr>
                                    <td colspan="2" style="margin-left:20px;">PRICE:</td>
                                </tr>
                            </table>
                            <br/>
                            <div>
                                <div rows="5" cols="80" name="comment" form="usrform">
                                    {!PRICE_INCLUDES}
                                </div>
                            </div>
                            <br/>
                            <div>
                                <div rows="5" cols="80" name="comment" form="usrform">
                                    {!PRICE_DOES_NOT_INCLUDE}
                                </div>
                            </div><br/>
                            Please let me know if you require any additional information.<br/>
                            Best Regards,
                            
                            <br/><br/><br/>
                            
                            <p>
                                {!user.Name}<br/>
                                {!user.MobilePhone}<br/>
                                {!user.Email}
                            </p>
                        </td>
                    </table> 
                </div>
                <br/>
            </body>
        </html>  
    </center>  
</apex:page>

and this code to send email
 
Messaging.EmailFileAttachment attach2 = new Messaging.EmailFileAttachment();
           	pagereference pageRef2 = Page.CargoQuoteDoc;
            pageRef2.setRedirect(true);
            pageRef2.getParameters().put('recId',recId);
            pageRef2.getParameters().put('segOne',Thank_You_Message);
            pageRef2.getParameters().put('segTwo',PRICE_INCLUDES);
            pageRef2.getParameters().put('segThree',PRICE_DOES_NOT_INCLUDE);
            pageRef2.getParameters().put('length',String.valueOf(length));
            Integer j=0;
            for(String img :imgIds){
                pageRef.getParameters().put('img'+j,img);
                j=j+1;
            }
            
            for(String img :imgIds){
                System.debug('img == '+img);
                
            }
            
            Blob body;//= pageRef.getContent();
            
            if(Test.isRunningTest()) { 
                body = blob.valueOf('Unit.Test');
            } else {
                body = pageRef.getContent();
            }
            attach2.setFileName('Cargo Quote.doc');
            attach2.setBody(body);

when I render visual force page it download on the local system it looks fine with textual content(Images not displaying ) but when I send it as an attachment and open it, it Open in unreadable formatted





%PDF-1.4
%âãÏÓ
2 0 obj <</ColorSpace[/CalRGB<</Gamma[2.2 2.2 2.2]/WhitePoint[0.95043 1 1.09]/Matrix[0.41239 0.21264 0.01933 0.35758 0.71517 0.11919 0.18045 0.07218 0.9504]>>]/Intent/Perceptual/Subtype/Image/Height 137/Filter/FlateDecode/Type/XObject/DecodeParms<</Columns 1131/Colors 3/Predictor 15/BitsPerComponent 8>>/Width 1131/Length 19127/BitsPerComponent 8>>stream
x^í½€Å™÷=&Ïl�V9
$HŒ�³á






Please provide some guidence for this issue 
Best Answer chosen by Ganesh Rajput
Ganesh RajputGanesh Rajput
I did a very silly mistake in code of mail sending 
use 
line(25) 

body = pageRef.getContent();

Instead of

body = pageRef2.getContent();

it's working fine but images still have problem
:)
 

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Ganesh,

Can you try using renderAS=application/msWord#MyDocument.doc

I hope this helps, looking forward for your response.

Regards,
Anutej
Ganesh RajputGanesh Rajput
I already try this getting this error
'Unsupported value application/vnd.msWord for <apex:page renderAs> encountered.'
Ganesh RajputGanesh Rajput
I did a very silly mistake in code of mail sending 
use 
line(25) 

body = pageRef.getContent();

Instead of

body = pageRef2.getContent();

it's working fine but images still have problem
:)
 
This was selected as the best answer