Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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]>
<xml>
<w:WordDocument>
  <w:view w:val="print" />
  <w:zoom w:percent="100" />
  <w:DoNotOptimizeForBrowser/>
  <w:DoNotHyphenateCaps/>
  <w:PunctuationKerning/>
</w:WordDocument>
</xml>
  <![endif]-->

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??

share|improve this question
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
add comment

3 Answers 3

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:

//controller
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>' +
        '<![endif]>';
}

... 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"/>
share|improve this answer
add comment

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.

Thanks

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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