+ Start a Discussion
lil_rangerlil_ranger 

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?  

M.sfM.sf

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"/>
<body>
//All your code Goes Here
</body>
</html>
</apex:page>


In class Add the following

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

 

Explanation:

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.