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
vbnmvbnm 

render VF page as word document

Hi,

 

I have requirement like, I need to render a visual force as Word Document format with Password protected.

Right now in visual force, we are having attribute in page like 'renderas='PDF''. In the same we have to display VF page in Word document format with password protected.

Is it possible to do this? Please let me know is there any alternative solution for this requirement?

Best Answer chosen by Admin (Salesforce Developers) 
prageethprageeth

Hi Vbnm;

   You can get a Word output by adding contentType attribute in to the <apex: page tag.

But I don't know about password protection.

 

<apex: page contentType="application/msWord"> 

All Answers

prageethprageeth

Hi Vbnm;

   You can get a Word output by adding contentType attribute in to the <apex: page tag.

But I don't know about password protection.

 

<apex: page contentType="application/msWord"> 

This was selected as the best answer
vbnmvbnm
Thanks Prageeth..
BWagsBWags

Just a tip when doing contentType=application/msword.  To get the document to open in word when you are viewing salesforce in Internet Explorer I've found you also need to add cache=true, otherwise it just hangs and word never opens.  So it would be:

 

<apex: page contentType="application/msWord" cache="true"> 

CTU007CTU007

I can add footer/header and page count in PDF, but when I output to word, those are lost.

 

So how to keep those in word?

BWagsBWags

CTU007,

 

Have a look at this, Turn VF Page into a Word doc and see if it helps explain it.

CTU007CTU007

Thanks, BWags, I tried but it only displays footer on one(the last) page, do I need to put the footer div code on every page?

 

BWagsBWags

CTU007,

 

You only need to put the footer div code on the last page, making sure to set the style to "mso-element:footer".  Then just use the CSS as described in the post I linked to.

 

 

To put the header and footer into the doc there is no “GOOD” solution that I can find unfortunately.  The best solution I’ve managed to discover still leaves you with an extra “header/footer” on the last page of your document which you have to erase.

You can follow the steps here.  The problem with this hack is that you have to put the header/footer somewhere in your documents body to be able to use it as a header/footer.  I put it at the end of the document on the last page so it’s sort of out-of-the-way.  Now I should mention that I only added a footer, no header but, this is basically the code I used.

CSS:

@page Section1 {
mso-footer:f1;
}
div.Section1 {
page:Section1;
}
p.MsoFooter, li.MsoFooter, div.MsoFooter {
mso-pagination:widow-orphan;
}

HTML/VISUALFORCE:
<div class=”Section1″>
—YOUR REGULAR CONTENT HERE—
<br clear=”all” style=”page-break-before:always” />
<div style=”mso-element:footer”>
<p>
<hr />
<table>
<tr>
<td colspan=”2″ style=”text-align:center;”>
Page&nbsp;
</td>
</tr>
<tr>
<td style=”width:80%”>
&nbsp;
</td>
<td style=”width:20%”>
<img width=”124″ height=”54″ src=”YourImageUrlHere”>
</td>
</tr>
</table>
</p>
</div>
</div>

 

By doing this when the page opens in Word, it "should" add the footer to the bottom of every page.  Hope it helps, although I know from working with Salesforce/Word that it can be a real pain to get everything working properly.

 

CTU007CTU007

Thanks for the reply, I checked and found out some "" are displayed as ? and that cause the problem.

 

Now I corrected it, and the footer is actually displaying on top of my pages as header, and there is not page # there.

 

I will play with it to find out why.

CTU007CTU007

I finally get it to work... the footer div was missing the id "f1" ....

And I added one header as well...

 

Thanks, this will be veryful for my users to generate NDAs since mail merge is not working with office 2010.

BWagsBWags

No problem!  Glad I could help and I'm glad you were able to get it working.

Jon Mountjoy_Jon Mountjoy_

Could someone write up a definitive version of this with a full Visualforce page code sample and submit it to the Force.com Cookbook?  http://developer.force.com/cookbook - Let's bake it!

ParinParin
Hi, I have managed to overcome the problem of repeating header and footer in the generated ms word file. Please let me know how can i post the code for it. You can contact me on my email id.
ummi bppummi bpp
Dear 
CTU007
Please send me code to add header to all word document pages using Visual force.

I tried but i am gettig header in only single (first page).