Link to home
Start Free TrialLog in
Avatar of alabamsterdam
alabamsterdam

asked on

Auto Print Word Document

OK, I have an invoice I want to automatically print when I post the order. No one else can print and there is a default printer. However, I am using cfheader and making it a word document. The following code works fine in a html document but does not auto print when I call to the invoice.cfm (ms word template)

<body>

Hello Kendra

<script language=JScript>
function doprint() {
document.body.insertAdjacentHTML("beforeEnd", "<object id='idWBPrint' width=0 height=0 classid='clsid:8856F961-340A-11D0-A96B-00C04FD705A2'></object>");
idWBPrint.ExecWB(6, -1);
idWBPrint.outerHTML = ""; }
if (window.self) window.doprint();
</script>

</body>

Anyone know of a way to auto print using this or something better? It has to be a word document though. Thanks
Avatar of Zyloch
Zyloch
Flag of United States of America image

Hi

Er...

<html>
<head>
<title>Test Word Print</title>
</head>
<body>
<?php
//Word Path
$word_file="someFile.doc";
$save_as="someFile.html";

$errmsg = "";
if ($w=new com("Word.Application")) {
   if ($w->Visible=1) {
      if ($w->Documents->Open($word_file)) {
         $w->Documents[1]->saveas($save_as, wbFormatHTML);
         $w->Quit();
      } else {
         $errmsg+="Could not open $word_file!<br>";
      }
   } else {
      $errmsg+="Could not set Word to visible state!<br>";
   }
} else {
   $errmsg+="Could not open Word. Make sure Word is installed on your machine.";
}
echo($errmsg);

if ($errmsg=="") {
   if ($fp=fopen($save_as,"r")) {
      $contents = fread($fp,filesize($fp));
      echo($contents);
   } else {
      echo("Error reading file $save_as!<br>");
      exit;
   }
} else {
   exit;
}

?>
<script language=JScript>
function doprint() {
document.body.insertAdjacentHTML("beforeEnd", "<object id='idWBPrint' width=0 height=0 classid='clsid:8856F961-340A-11D0-A96B-00C04FD705A2'></object>");
idWBPrint.ExecWB(6, -1);
idWBPrint.outerHTML = ""; }
if (window.self) window.doprint();
</script>
</body>
</html>

Regards,
Zyloch
Not the cleanest code, but should work if you have Word2000+ and PHP. If you have Word97 only, then you should change $save_as to someFile.txt and change vbFormatHTML to vbFormatText or vbFormatTextLineBreaks whichever one suits you better.

Regards
Oops, just noticed this was in ColdFusion, sorry about the PHP example...

Regards
ASKER CERTIFIED SOLUTION
Avatar of anandkp
anandkp
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial