Technical

How to generate a .doc file using Visualforce

Digital Partner

1130 views
09/05/2023
4 mins

Most of us are familiar with Word, from the Microsoft Office Suite, since we use it constantly for homework, essays, or work documents, and so there might come a time when you will find yourself wondering: “Is there a way to pick a few custom fields in Salesforce, put them on a page and generate a .doc file?”. The answer to that is YES! You can actually do it.

Step 1 – Create a Visualforce Page

In case you’ve never created one before, you can easily do it by navigating to the Dev Console, then go to File -> New -> Visualforce Page. Enter a name for your Visualforce Page and then click on “Ok”. 

Once your new page is created, you can continue working on it using the Dev Console or you can then proceed to download it on Visual Studio Code.

Step 2 – Add some attributes to the existing tags

You will notice that we have an <apex:page> tag at the beginning of our Visualforce Page. We’ll use this tag to specify some attributes that will indicate things such as the format of our page (in this case, a .doc file). 

Click on the <apex:page> tag, and right after the “page” word and before the “>” character, add the following attributes. Make sure to replace the words enclosed in [] with the ones that you need based on your context:

standardController=[Controller] contentType=”application/msword#FileName.doc” sidebar=”false” standardStylesheets=”false”

applyHtmlTag=”true” applyBodyTag=”false” showHeader=”false” renderAs=”html”

Don’t forget to save your Visualforce page by using the shortcut Ctrl + S.

Step 3 – Let’s add some content to that page

Now that we have those attributes in place, the next step is to add all the content that we wish that .doc file to contain. If you’ve never used a Visualforce page before, I recommend you to take a look at this Trailhead Unit, which will explain you some useful things such as how to display a field on a Visualforce page: https://trailhead.salesforce.com/content/learn/modules/visualforce_fundamentals

If you want to style the content and change things such as the font (Note: You can check which fonts are available here) by using some CSS, you can easily do it by following these steps:

  1. A. Right after the <apex:page [attributes> tag that we edited back in item 1, put a new tag called “<head>”.
  2. B. Inside that <head> tag, you need to add a <meta> tag and a <style> tag, which will contain your CSS. Here’s an example of one I recently used:
<head>

   <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
   <style type="text/css">  
      p {
      font-family:"Arial Unicode MS";
      font-size:11pt; 
      }
      span {
      font-family:"Arial Unicode MS";
      font-size:8pt;
      }
      h1 {
      font-family:"Arial Unicode MS";
      font-size:9pt;
      }
   </style>
</head>
  1. C. Save your Visualforce page by using the shortcut Ctrl + S.

Keep in mind that you can also use some HTML tags in your Visualforce Pages, such as <p> or <div>, to provide more customized looks depending on your needs. 

(An example of how your Visualforce Page might look like so far)

Step 4 – Preview it 

Watch our tutorial here

So now that we have our content in place, it’s a good idea to take a look at our work to confirm it’s rendering as expected. You can do that by clicking on the “Preview” button on the Dev Console where you have the Visualforce Page open, which will open a new tab on your browser, and then if your Visualforce Page requires a record to display data, you can add the record Id by adding “&Id=[RecordId]” at the end of the URL on that new tab. 

If everything is good, your .doc file should download right away.

Step 5 – Finished!

And that’s all! You can now take that handy Visualforce Page and add it to a record page by using a new button, or you can even go further and create an Apex Class that can automatically attach it to records, and whenever it’s accessed, it should start the download for that useful .doc file.

If you are interested in learning more about how to generate a .doc file using Visualforce, feel free to contact us, and our team of Salesforce experts will help you out!


Digital Partner

We are a Salesforce Nearshore Partner and we offer a win-win solution for Salesforce Partners and Non-profits in North America. We are proud of our Salvadoran team, amazing people creating outstanding Salesforce experiences.


Leave a Reply

Your email address will not be published. Required fields are marked *

Post comment

We’re here to share what we know. Get our news.

Receive notifications about our latest news, services, events and products.