How to change the defalut windows printer from ax

Hi All,

Is there any way can we change the default windows printer through code using WINAPI class or any code to a specific printer.

this printer is not fixed, what ever the user can select we need to replace the default printer of windows with this printer.

and after our goal acomplished we need to revert back the orginal default printer.

Thanks in Advance

Nihar.

winspool.drv contains SetDefaultPrinter function - you could call it either directly by DLLFunction class or via P/Invoke in .NET class library. Another option is to use WMI - you might be even able to reuse some existing solution.

Hi Martin,

Thanks for the reply, but the requirement is in Ax 2012 and the requirement is like the user is printing packing slip while posting. That time they want to print one pdf which is attached to the batch through document handaling. Here i am using the code which i know that we can print any document from outside ax stored in some drive. that is WINAPI::SHellExecute(filename,’’,’’,‘Print’);. this is printing thedocument to the default printer set by the user always , not to the printer set by the user in print job setting where the packing slip is printing. So here i am getting the printer but need to change the printer to default so that it will print the docment there simultaneously with packing slip. after printing the report we can revert back the defalut printer to the original before.

the code i am using is

DocuRef docuRef;

DocuType docuType;

PrintJobSettings pjs;

container

printjobset;

SalesFormLetter cmaSalesFormLetter;

SRSPrintDestinationSettings printSettings;

int

i,defaultPrinter,counter;

str 500

printer,printer1,printerDetails,portname,printerDriver;

cmaSalesFormLetter = cmaSalesFormLetter_PackingSlipShipCreate::newPackingSlipShipCreate();

cmaSalesFormLetter.getLast();

printSettings =

new

SRSPrintDestinationSettings(cmasalesFormLetter.printerSettingsFormletter());

sysPrintOptions =

new

sysPrintOptions();

sysPrintOptions.setPrintJobSettings(SysPrintOptions::newPrintJobSettingsOnServer());

printer1 = printSettings.printerName();

pjs =

new

PrintJobSettings();

//pjs.setTargetPrintMedium::Printer);

for(i = 1

;i<pjs.getNumberOfPrinters();i++)

{

printer = pjs.getPrinter(i);

if

(printer == printSettings.printerName())

{

defaultPrinter = i;

portname = pjs.printerPortName();

printerDriver = pjs.printerDriverName();

break

;

}

}

counter++;

sysPrintOptions.setPrinter(defaultPrinter);

if

(_batch.RecId)

{

while select

docuRef

where

docuRef.RefCompanyId==_batch.company() &&

docuRef.RefTableId==_batch.TableId &&

docuRef.RefRecId==_batch.RecId

join

docuType

where

docuType.TypeId==docuRef.TypeId &&

docuType.DocuStructureType==DocuStructureType::cmaCert

{

if

(docuRef.RecId)

{

{

printerDetails =

strFmt(’ t/ “%1” “%2” “%3” “%4”’,

docuref.completeFilename(),

printSettings.printerName(),p

rinterDriver,portname);

WinAPI::shellExecute(docuref.completeFilename(),’’,’’

, #ShellExePrint);

Thanks

NIhar

Hi,

You can try the following code, I defined a specific printer and send it to this printer: in the report’s init() method:

str printer;

;

printer = ‘\\path_to_your_printer_with_printer_name’;

this.deviceName(printer);

this.setTarget(printmedium::printer);

super();

hope it helps

regards,

Thomas

Hi Thomas,

This can work in init method of the report. But this not the report printing this i am printing an pdf saved out side of the ax stored in local device.

and the code is executing in the class directly after print of packingslip.

if any other idea please provide me.

Nihar

Hi,

You can try the following, it prints a report in a pdf file in a specified path without warning.

report = new ReportRun(new Args(ReportStr(your_report)));
report.query().interactive(false);
report.report().interactive(false);
report.args().caller(null);

report.args().parm(your_report_parm);

report.printJobSettings().setTarget(PrintMedium::File);
report.printJobSettings().format(PrintFormat::PDF);
report.printJobSettings().warnIfFileExists(false);
report.printJobSettings().fileName(strfmt("%1%2",path,filename));

report.run();

hope it helps

regards,

Thomas

Hi Thomas,

Thanks for the reply, this is what i am not lookin for. I know this code to send a report directly to the pdf and save there.what I am looking, in a Packing slip the batch is attached and a pdf document ( which is available out side of ax) is attached to the batch through document handaling. i just want to get the printjob setting of the packing slip report and print directly the pdf to that printer after the original report prints.For example while creating packing slip i have given that the packing slip will print directly to xxxxx printer and i want after print of the packing slip that document should print to the xxxxx printer. i think this will provide a better idea waht i am looking for. if not please reply me i will explain you more.

Thanks

Nihar

Hi,

If I understand, you want to print automatically a document attached to a packingSlip when this one is printed, so you can add thefollowing method in your report and call it in the fetch() method:

void printLinkedDocuments()
{
  #WinAPI
  DocuRef docuRef;//table that stores attached docs
  ;
  while select docuRef
  where docuRef.RefCompanyId == yourTable.dataAreaId && docuRef.RefTableId == tableNum(yourTable) && docuRef.RefRecId == yourTable.RecId
  {
    if(docuRef.RecId)
    {
      if (element.printJobSettings().getTarget() == PrintMedium::Printer)//here's your printJobSettings
      {
        WinAPI::shellExecute(docuRef.completeFileName(),element.printJobSettings().printerPrinterName(),‘’,#ShellExePrint);
      }
    }    
  }
} 
Hope it helps, if it solves your problem please mark as verified
regards,

Thomas

Hi Thomas,

Again thanks for your quick response.

I beleive ,#ShellExePrint only prints the document to the difalut printer set to the user. the code piece which you have provide i have tried that way also but its not printing. I reaserched a lot in in google what i find that after getting the printer name form the printjobsetting we need to change that printer to the default printer for that user from Ax then after printing the doc we need to change the original printer to default printer. How to change the printer to defalut form Ax i am not finding any solution. If you have any idea please provide me.

Thanks in Advance.

Nihar

I forgot to mention this is ax 2012 and the report uses DP classes

I am doing something similar. You should set the .PDF’s that are linked in DocuRef into the PrintMgmtDocType PrintMgmtNode and assign them to which module they run under. PrintMgmtNode_* PrintMgmtHierarchy_*

Additional tables you will need to link are DocuRef, DocuType, DocuValue in Docuref, the PackingSlip reference will be noted in the RefTableId and RefRecId pointing to the WMSOrder table reference to which the associated docs are attached.

Hope that helps someone

Hi Martin,

May I request you to help with example for changing default printer setting by X++ using winspool.drv.

Thanks & Regards,
Sandeep