Discussion:
Printer.EndDoc help needed - can't clear previous job
(too old to reply)
Ryan
2004-08-26 09:57:50 UTC
Permalink
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;
Markku Nevalainen
2004-08-26 11:34:55 UTC
Permalink
Post by Ryan
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' );
Is your Fax printer some hidden device that is not visible as standard
Windows printer? Or why you have to do that much of work with the printer
driver?

With the usual Windows printers you do not even create new Printer
instances as you do above. Just pass the wanted PrinterIndex number to
the Delphi's standard, automatically existing Printer, and that's all.

Of course this won't be for much help if for some reason you do
have to load the driver that way. But still, do you need to create a new
printer instance each time?

Yet I am no expert on printer arena at all. I have used Turbo Power's
free Faxing components, and the developer does not need to know
anything about loading printer drivers etc.

Markku Nevalainen
Ryan
2004-08-27 07:44:54 UTC
Permalink
Nope, the fax printer is just a normal printer as such, so basically
it's a case of changing the default printer for my needs. I included
the code in case it was needed, but believe the fix is needed in the
first procedure. The reason I do this is because the users can have a
variety of (unknown) printers set up and the default won't be the one
I want. It's an easy way of doing this. All it does is change to the
printer (fax) needed and then change back again afterwards which only
takes a couple of seconds.

Besides, the problem isn't in this as I can change the printer and
print. The only problem I have is getting the printer to realise that
a job has finished and next time a new one is going to be sent.

R
Post by Markku Nevalainen
Post by Ryan
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' );
Is your Fax printer some hidden device that is not visible as standard
Windows printer? Or why you have to do that much of work with the printer
driver?
With the usual Windows printers you do not even create new Printer
instances as you do above. Just pass the wanted PrinterIndex number to
the Delphi's standard, automatically existing Printer, and that's all.
Of course this won't be for much help if for some reason you do
have to load the driver that way. But still, do you need to create a new
printer instance each time?
Yet I am no expert on printer arena at all. I have used Turbo Power's
free Faxing components, and the developer does not need to know
anything about loading printer drivers etc.
Markku Nevalainen
Markku Nevalainen
2004-08-27 08:54:09 UTC
Permalink
Post by Ryan
Besides, the problem isn't in this as I can change the printer and
print. The only problem I have is getting the printer to realise that
a job has finished and next time a new one is going to be sent.
That _may_ have something to do with it that you create new Printer
instance on every run.

Try not to create anything. Just use the standard Printer that is
always ready and available when you have Printers Unit listed in
your Uses clause.

Markku Nevalainen
Ryan
2004-08-27 14:43:31 UTC
Permalink
Found a simple way around this. Dropped the second procedure as I can
work without it. Needs QRPRNTR in the uses clause.

procedure TfrmTrack.SendFax;
var
strDefault : string;
strMsg : string;

begin
strMsg := edtMsgDetails.Text;
// Get the default printer
strDefault := Printer.Printers[Printer.PrinterIndex];
Application.CreateForm(TrptFax, rptFax);
// Switch to the Castelle Driver
rptFax.PrinterSettings.PrinterIndex :=
QRPrinter.Printers.IndexOf('Castelle DCX Driver');
// Now print the message without previewing
if edtMsgSubject.Text <> '' then
rptFax.lblTitle.Caption := edtMsgSubject.Text
else
rptFax.lblTitle.Caption := 'Fax from CBFA 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;
rptFax.PrinterSettings.PrinterIndex :=
QRPrinter.Printers.IndexOf(strDefault);
rptFax.Destroy;
// Then switch back to the default printer
end;
Post by Markku Nevalainen
Post by Ryan
Besides, the problem isn't in this as I can change the printer and
print. The only problem I have is getting the printer to realise that
a job has finished and next time a new one is going to be sent.
That _may_ have something to do with it that you create new Printer
instance on every run.
Try not to create anything. Just use the standard Printer that is
always ready and available when you have Printers Unit listed in
your Uses clause.
Markku Nevalainen
Loading...