(Reposted due to undeletable spam comments)
So I have created a web part that is a calulation based on various fields in various lists on the site. It displays a table full of data and some text stuff. The client wanted to be able to print just what could be seen in the web part and not all the nice looking stuff around it.
It took me a while to figure this out and it was with the help of this link:
http://personalweb.about.com/od/copypastejavascripts/a/404_3scripts_3.htm
So I was lucky in the fact that my output was just a stream of html so I put div tags around the stuff I wanted to print like this - <div id='printPart'> print this bit</div>
and then just added the code to the head of the page (must do this in FrontPage) where the webpart was like this:
<script>
function printSpecial()
{
var html = '<HTML>\n<HEAD>\n';
if (document.getElementsByTagName != null)
{
var headTags = document.getElementsByTagName("head");
if (headTags.length > 0)
html += headTags[0].innerHTML;
}
html += '\n</HEAD>\n<BODY>\n';
var printReadyElem = document.getElementById("printPart");
if (printReadyElem != null)
{
html += printReadyElem.innerHTML;
}
else
{
alert("Could not find the printPart div");
return;
}
html += '\n</BODY>\n</HTML>';
var printWin = window.open("","printSpecial");
printWin.document.open();
printWin.document.write(html);
printWin.document.close();
printWin.print();
}
</script>
It all needs a bit of fine tuning but in essence it works a treat!