[SalesForce] Repeated Header and Footer when Visualforce Page Is rendered as MS Word

Hey People I got this code from Stack Exchange only But It's Not what I'm looking for, I feel here header and Footer are static, Please somebody guide me to make running header and footer when i render page as MS Word

<apex:page sidebar="false"
           showChat="false"
           showHeader="false"
           contentType="application/msword#Test.doc" 
           cache="true">

<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<style>
    @page Main {
        mso-header:h1;
        mso-footer:f1;
    }
    div.Main{
        page:Main;
    }
    p.MyFoot, li.MyFoot, div.MyFoot{
        mso-pagination:widow-orphan;
        tab-stops:center 216.0pt right 432.0pt;
    }
    p.MyHead {

    }
</style>
</head> 

<div class="Main">
    <div style="mso-element:header" id="h1">
        <p class="MyHead">Header Text</p>
    </div>
    <div style="mso-element:footer" id="f1">
        <p class="MyFoot">
            <span style='mso-field-code:" FILENAME "'> </span>
            <span style='mso-tab-count:2'></span>
            Page <span style='mso-field-code:" PAGE "'></span> of <span style='mso-field-code:" NUMPAGES "'></span>
        </p>
    </div>
</div>

</apex:page>

Best Answer

I think the issue you are having is that the 'template' for the header and footer gets displayed in the main content. Otherwise the header and footer do display correctly - at least they did for me. I found the solution to remove the 'template' in another post. Check it out here

 <apex:page sidebar="false" contentType="application/msword" cache="true">
    <html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:word' xmlns='<a target="_blank" href="http://www.w3.org/TR/REC-html40'" rel="nofollow">http://www.w3.org/TR/REC-html40'</a>>
        <head>
            <style>
                p.MsoHeader, li.MsoHeader, div.MsoHeader{
                    margin:0in;
                    margin-top:.0001pt;
                    mso-pagination:widow-orphan;
                    tab-stops:center 3.0in right 6.0in;
                }
                p.MsoFooter, li.MsoFooter, div.MsoFooter{
                    margin:0in;
                    margin-bottom:.0001pt;
                    mso-pagination:widow-orphan;
                    tab-stops:center 3.0in right 6.0in;
                }
                @page Section1{
                    size:8.5in 11.0in; 
                    margin:0.5in 0.5in 0.5in 0.5in;
                    mso-header-margin:0.5in;
                    mso-header:h1;
                    mso-footer:f1; 
                    mso-footer-margin:0.5in;
                    mso-paper-source:0;
                }
                div.Section1{
                    page:Section1;
                }
                /*Below is the style to prevent the repetition of header and Footer.. Cheers!!!*/
                table#hrdftrtbl{
                    margin:0in 0in 0in 9in;
                }        
            </style>
        </head>

        <body>
            <!-- Content -->
            <div class="Section1"><!--Section1 div starts-->
                <!-- Page 1 starts -->
                <br/>
                Page 1 Content

                <br clear="all" style="page-break-before:always" />


                <!-- Page 2 Starts -->
                <br/>
                Page 2 Content

                <br clear="all" style="page-break-before:always" />

                <!-- Page 3 Starts -->
                <br/>
                Page 3 Content

                <!--Header and Footer Starts-->
                <table id='hrdftrtbl' border='1' cellspacing='0' cellpadding='0'>
                    <tr>
                        <td>
                            <!--Header-->
                            <div style='mso-element:header' id="h1" >
                                <p class="MsoHeader">
                                    <table border="1" width="100%">
                                        <tr>
                                            <td width="30%">
                                                Left <br/>Header
                                            </td>
                                            <td align="center" width="40%">
                                                Center<br/> Header
                                            </td>
                                            <td align="right" width="30%">
                                                Right<br/>Header
                                            </td>
                                        </tr>
                                    </table>
                                </p>
                            </div>
                        </td>

                        <td>
                            <!--Footer-->
                            <div style='mso-element:footer' id="f1">
                                <p class="MsoFooter">
                                    <table width="100%" border="1" cellspacing="0" cellpadding="0">
                                        <tr>
                                            <td width="30%">
                                                Left<br/>Footer
                                            </td>
                                            <td align="center" width="40%">
                                                Center<br/>Footer
                                            </td>
                                            <td align="right" width="30%">
                                                Page <span style='mso-field-code: PAGE '></span> of <span style='mso-field-code: NUMPAGES '></span>
                                            </td>
                                        </tr>
                                    </table>
                                </p>
                            </div>
                        </td>
                    </tr>
                </table>
            </div><!--Section1 div ends-->
        </body>
    </html>
</apex:page>
Related Topic