+ Start a Discussion

Rendering VF as Word Document

Is there a way to automatically open up the Word document with the "Print" layout instead of "Web" layout?


I am using Chrome, but some of the users use IE9.  Is there a way to open the Word document immediately, like it does when rendering as a pdf?  


By Default MS word will take the contents in Text/Html Format.


Include the following code in VF Page.

<apex:page controller="TestMSWord" contentType="application/msWord#msword.doc" >
<html xmlns:w="urn:schemas-microsoft-com:office:word">
<apex:outputText value="{!PrintView}" escape="false"/>
//All your code Goes Here

In class Add the following

public class TestMSWord{
    public String getPrintView()
        '<!--[if gte mso 9]>' +
            '<xml>' +
            '<w:WordDocument>' +
            '<w:View>Print</w:View>' +
            '<w:Zoom>100</w:Zoom>' +
            '<w:DoNotOptimizeForBrowser/>' +
            '</w:WordDocument>' +
            '</xml>' +



Add <html> tag with attributes since it should understand the schema is of word
Add <body> tag so that it will allow html tags like <h1>, <ul>,<li> etc
Add <Outputext> from class since directly vf page will not allow to save the html content with <w:> tag.This will enable print view by default.