function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MJ09MJ09 

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:

 

application/vnd.ms-word

application/rtf

vnd.ms-word.document.macroEnabled.12

 

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?

 

MJ09MJ09

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" 
	renderAs="{!CASE($CurrentPage.parameters.type,
	             'pdf', 'pdf',
	             'word', '',
	             '')}" 
	contentType="{!CASE($CurrentPage.parameters.type,
	             'pdf', 'application/pdf',
	             'word', 'application/ms-word#test.doc',
	             '')}" 
	>
<style type="text/css">
.myStyle {color:red;padding:10px;margin:20px;}
</style>
	<apex:pageMessages />
	<!-- <table style="width:100%;"><tr><td> -->
		Contact: <apex:outputText value="{!contact.Name}" styleClass="myStyle" />
	<!-- </td></tr></table> -->
</apex:page>

 

 

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

 

?id=YOUR_CONTACT_ID&type=word

 

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?

 

raj123raj123

 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

 

Raj.