Yo guardo en un fichero INI las impresoras que se van a utilizar en la aplicación, configurable por el usuario, a continuación los fragmentos de código que utilizo.
Código Delphi
[-] Case nModelo of
1:begin
Application.CreateForm(TfrLptFactura, frLptFactura);
frLptFactura.LptFactura.PrinterSettings.PrinterIndex:=GetImpresora(Impresora1);
if n = 1 then
frLptFactura.LptFactura.PreviewModal
else
frLptFactura.LptFactura.Print;
frLptFactura.Close;
end;
Function GetImpresora(Impre:String):Integer;
Var
nCont:Integer;
lSearch:Boolean;
cNewLIne:String;
begin
lSearch:=False;
cNewLine:=Chr(10)+Chr(13);
For nCont:=0 to Printer.Printers.Count - 1 do
begin
if Pos(impre, Printer.Printers[nCont]) <> 0 then
begin
lSearch:=True;
Result:=nCont;
Break;
end;
end;
if lSearch = False then
begin
ShowMessage('Impresora no encontrada.'+impre+cNewLine+'Se utilizará la predeterminda.');
Result:=-1
end;
end;
Procedure Obtener_Impresoras();
begin
MiCarpeta:=ExtractFilePath(Application.ExeName);
IniPrinters:=MiCarpeta+'MZprn.ini';
Try
Fichero := TIniFile.Create(IniPrinters);
Impresora1:=Fichero.ReadString ('Facturas', 'String','Error');
Impresora2:=Fichero.ReadString ('Albaranes', 'String','Error');
Impresora3:=Fichero.ReadString ('Ruta', 'String','Error');
Impresora4:=Fichero.ReadString ('Listados', 'String','Error');
Except
ShowMessage('No se pudo acceder al fichero '+Fichero.FileName );
end;
end;
[Facturas]
String=\\FACTURACION\EPSON LQ-590 ESC/P 2 Ver 2.0
[Albaranes]
String=\\FACTURACION\EPSON LQ-590 ESC/P 2 Ver 2.0
[Ruta]
String=KONICA MINOLTA C360SeriesPCL
[Listados]
String=KONICA MINOLTA C360SeriesPCL
procedure TfrImpresoras.SpeedButton5Click(Sender: TObject);
var
lCrear:Boolean;
begin
lCrear:=True;
if FileExists(IniPrinters) then
begin
if Application.MessageBox('El fichero ya existe.¿Seguro que desea sobreescribirlo?','Atención',mb_OkCancel +
mb_IconQuestion)= idOk then
begin
DeleteFile(IniPrinters);
lCrear:=True;
end
else
begin
ShowMessage('Opción cancelada por el usurio. El fichero no se creo.');
lCrear:=False;
end;
end
else
lCrear:=True;
if lCrear = True then
begin
Try
Fichero := TIniFile.Create (IniPrinters); Etiqueta:='Facturas';
Fichero.WriteString(Etiqueta,'String',EdFacturas.Text);
Etiqueta:='Albaranes';
Fichero.WriteString(Etiqueta,'String',EdAlbaranes.Text);
Etiqueta:='Ruta';
Fichero.WriteString(Etiqueta,'String',EdRuta.Text);
Etiqueta:='Listados';
Fichero.WriteString(Etiqueta,'String',EdListados.Text);
Fichero.Free; Obtener_Impresoras();
Except
ShowMessage('Atención:Se produjo un error, el fichero INI no se grabó.');
end;
end;
end;
Un Saludo.