Ryan
2004-08-26 09:57:50 UTC
I'm having a problem with automating some printing to a fax driver. My
code below details the two main functions. One to send the document,
the other to change the driver (which works nicely).
The problem I have is that when I send another document, they merge
together so that the second document shows both it's own detail, plus
the detail of the one(s) before it. The more I send, the more shows on
each fax.
One way I found is to close the app which terminates the documents
being sent, but this is far from ideal. I've tried printer.begindoc
and printer.enddoc as well as printer.abort, but I've obviously
missing something vital here as it doesn't work. What am I doing wrong
or missing ?
How can I make sure that each print I send out has the previous
information cleared out ?
TIA
Ryan
procedure TfrmTrack.SendFax;
var
strDefault : string;
strMsg : string;
begin
strMsg := edtMsgDetails.Text;
// Get the default printer
strDefault := Printer.Printers[Printer.PrinterIndex];
// Switch to the Castelle Driver
SetDefaultPrinter('Castelle DCX Driver');
// Now print the message without previewing
if edtMsgSubject.Text <> '' then
rptFax.lblTitle.Caption := edtMsgSubject.Text
else
rptFax.lblTitle.Caption := 'Fax from Commissions';
rptFax.MemFax.Lines.Add('Commissions Fax sent on behalf of ' +
frmMain.strUserName);
rptFax.MemFax.Lines.Add(CRLF);
rptFax.MemFax.Lines.Add(strMsg);
rptFax.MemFax.Lines.Add(CRLF);
rptFax.MemFax.Lines.Add('Regards');
rptFax.MemFax.Lines.Add(CRLF);
rptFax.MemFax.Lines.Add(frmMain.strUserName);
rptFax.Print;
// Then switch back to the default printer
SetDefaultPrinter(strDefault);
end;
procedure TfrmTrack.SetDefaultPrinter(PrinterName: String);
{Function to allow the default printer to be changed given the name of
the printer}
var
I: Integer;
Device : PChar;
Driver : Pchar;
Port : Pchar;
HdeviceMode: Thandle;
aPrinter : TPrinter;
bFoundPrinter : Boolean;
strPrinterName : string;
begin
bFoundPrinter := False;
Printer.PrinterIndex := -1;
GetMem( device, 255);
GetMem( Driver, 255);
GetMem( Port, 255);
aPrinter := TPrinter.create;
for I := 0 to Printer.printers.Count-1 do
begin
strPrinterName := Printer.Printers[i];
// Use line below to work out names of printers if debugging
// MessageDlg(strPrinterName, mtInformation, [mbOK], 0);
if strPrinterName = PrinterName then
begin
bFoundPrinter := True;
aprinter.printerindex := i;
aPrinter.getprinter( device, driver, port, HdeviceMode);
StrCat(Device, ',');
StrCat(Device, Driver );
StrCat(Device, Port );
WriteProfileString('windows', 'device', Device );
StrCopy( Device, 'windows' );
SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0,
Longint(@Device));
end;
end;
FreeMem( device, 255);
FreeMem( Driver, 255);
FreeMem( Port, 255);
aPrinter.free;
if bFoundPrinter = False then
MessageDlg('Unable to change default printer to ' + PrinterName,
mtWarning, [mbOK], 0);
end;
code below details the two main functions. One to send the document,
the other to change the driver (which works nicely).
The problem I have is that when I send another document, they merge
together so that the second document shows both it's own detail, plus
the detail of the one(s) before it. The more I send, the more shows on
each fax.
One way I found is to close the app which terminates the documents
being sent, but this is far from ideal. I've tried printer.begindoc
and printer.enddoc as well as printer.abort, but I've obviously
missing something vital here as it doesn't work. What am I doing wrong
or missing ?
How can I make sure that each print I send out has the previous
information cleared out ?
TIA
Ryan
procedure TfrmTrack.SendFax;
var
strDefault : string;
strMsg : string;
begin
strMsg := edtMsgDetails.Text;
// Get the default printer
strDefault := Printer.Printers[Printer.PrinterIndex];
// Switch to the Castelle Driver
SetDefaultPrinter('Castelle DCX Driver');
// Now print the message without previewing
if edtMsgSubject.Text <> '' then
rptFax.lblTitle.Caption := edtMsgSubject.Text
else
rptFax.lblTitle.Caption := 'Fax from Commissions';
rptFax.MemFax.Lines.Add('Commissions Fax sent on behalf of ' +
frmMain.strUserName);
rptFax.MemFax.Lines.Add(CRLF);
rptFax.MemFax.Lines.Add(strMsg);
rptFax.MemFax.Lines.Add(CRLF);
rptFax.MemFax.Lines.Add('Regards');
rptFax.MemFax.Lines.Add(CRLF);
rptFax.MemFax.Lines.Add(frmMain.strUserName);
rptFax.Print;
// Then switch back to the default printer
SetDefaultPrinter(strDefault);
end;
procedure TfrmTrack.SetDefaultPrinter(PrinterName: String);
{Function to allow the default printer to be changed given the name of
the printer}
var
I: Integer;
Device : PChar;
Driver : Pchar;
Port : Pchar;
HdeviceMode: Thandle;
aPrinter : TPrinter;
bFoundPrinter : Boolean;
strPrinterName : string;
begin
bFoundPrinter := False;
Printer.PrinterIndex := -1;
GetMem( device, 255);
GetMem( Driver, 255);
GetMem( Port, 255);
aPrinter := TPrinter.create;
for I := 0 to Printer.printers.Count-1 do
begin
strPrinterName := Printer.Printers[i];
// Use line below to work out names of printers if debugging
// MessageDlg(strPrinterName, mtInformation, [mbOK], 0);
if strPrinterName = PrinterName then
begin
bFoundPrinter := True;
aprinter.printerindex := i;
aPrinter.getprinter( device, driver, port, HdeviceMode);
StrCat(Device, ',');
StrCat(Device, Driver );
StrCat(Device, Port );
WriteProfileString('windows', 'device', Device );
StrCopy( Device, 'windows' );
SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0,
Longint(@Device));
end;
end;
FreeMem( device, 255);
FreeMem( Driver, 255);
FreeMem( Port, 255);
aPrinter.free;
if bFoundPrinter = False then
MessageDlg('Unable to change default printer to ' + PrinterName,
mtWarning, [mbOK], 0);
end;