+ Start a Discussion

Render a VF page as ms-word

How can I get a VF page to render in such a way that I can open it in Word?


I know that renderAs supports only PDF, but per this blog article (http://blog.sforce.com/sforce/2008/12/visualforce-to-excel.html), I've been playing with contentType. I've tried a few contentTypes:






The first two (especially when used with an #filename.doc suffix) create content that can be opened in Word, but the Word document contains HTML tags.


The second one came from http://www.iana.org/assignments/media-types/application/index.html which has a list of valid media types. Word wouldn't open anything created with that content type.


How can I get a VF page to generate content that can be opened in MS Word?



Makes very little sense to me, but if I wrap the body of the VF page inside an HTML table tag, I can successfully open the result in Word, without the HTML tags appearing in the content. 


Here's an example:



<apex:page standardController="Contact" standardStylesheets="true" 
	             'pdf', 'pdf',
	             'word', '',
	             'pdf', 'application/pdf',
	             'word', 'application/ms-word#test.doc',
<style type="text/css">
.myStyle {color:red;padding:10px;margin:20px;}
	<apex:pageMessages />
	<!-- <table style="width:100%;"><tr><td> -->
		Contact: <apex:outputText value="{!contact.Name}" styleClass="myStyle" />
	<!-- </td></tr></table> -->



Save the above in a VF page named Sample. Load the page with querystring parameters like this:




It'll open a Word document, but you'll see the HTML tags in the content. Then remove the comments (that is, add the table tags), and you'll see that it renders correctly -- almost. The padding and margin aren't respected in the Word document.


So now I've got 2 more questions:


1. Why do I have to put my content inside <table> tags?


2. How can I generate a Word document that respects my formatting, like padding and margin attributes?



 Hi MJ Kahn 


Were you able to remove the html tags from the word document  , can you share how did you it.


Thanks in advance