Hope this scenario will be very interesting for everyone!! I have a scenario where the user needs to download the word document by clicking a link on page.

This word document is dynamically generated from visualforce by fetching values from the record.

Everything works fine. But the problem is when I try to open the downloaded document in MsWord (2007 or 2010), It opens in web layout by default. But I need to open the document in print layout / print view.

I searched and found that the below code snippet works well for all kinds of technology like php, python, etc..

// Code snippet

<!--[if gte mso 9]>
  <w:view w:val="print" />
  <w:zoom w:percent="100" />

But it didnt work with visualforce. I placed this code snippet inside the tag and right below the tag as per Office XML reference. But I didnt get a positive result.

Is there anyway to achieve this in visualforce??

  • 5
    Can you verify (for example force open your generated word doc in Notepad) that the conditional comment is in the document? Visualforce used to delete all conditional comments (workaround was to use entities for &lt; &gt; signs) and they fixed it only recently in Winter'13. Check page 197 of na1.salesforce.com/help/doc/en/… and API version of your page.
    – eyescream
    Nov 27 '12 at 17:02

The snippet you've got is correct, but as you say it won't save in a Visualforce page.

Instead you can great a read only property in your vf controller like so:

public String getWordPrintViewXML()
    // doesn't need to be on multiple lines, it's just for readability
    return '<!--[if gte mso 9]>' +
        '<xml>' +
        '<w:WordDocument>' +
        '<w:View>Print</w:View>' +
        '<w:Zoom>100</w:Zoom>' +
        '<w:DoNotOptimizeForBrowser/>' +
        '</w:WordDocument>' +
        '</xml>' +

... and then use <apex:outputText> with escape="false" to insert it into the page. Normally you should avoid the use of escape="false" as it can be used in nasty ways for injection attacks, but this is a pretty valid use case since we're using a fixed string:

<!-- Page code -->
<apex:outputText value="{!WordPrintViewXML}" escape="false"/>

Please Refer The following. You Need to add HTMl attributes



If you want to download page as word file use

contenttype = application/msword#FileName.doc

in visualforce page tag

and see this Answer to generate downloadable page.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.