Friday 8 August 2014

Timeouts on Windows Shell Commands

The common problem of using external apps through running by Windows Script Host Shell like: WshShell.RUN('ping microsoft.com',TRUE,TRUE) is that the code performing is stopps if the app malfunction and doesn't finish which causes freeze in NAV. The following example shows how to use defined timeout to prevent freezes while running any external apps or scripts by Windows Shell:

Instead of code:
WshShell.Run(Commandline,WindowStyle,WaitOnReturn);
use custom function:
ExecCommand(Commandline,Setup."Processing Timeout");

Function's signature:

PROCEDURE ExecCommand(CommandString : Text[1024];Timeout : BigInteger) : Boolean

VAR
      WshShell : Automation 'Windows Script Host Object Model'.WshShell;
      WshExec : Automation 'Windows Script Host Object Model'.WshExec;
      TimeStep : Integer;
      WindowStyle : Integer;
      ElapsedTime : Integer;
      WaitOnReturn : Boolean;
      ExecTimedOut : Boolean;

CODE:
CREATE(WshShell);
TimeStep := 1000;
WindowStyle := 0;
WaitOnReturn := FALSE;
WshExec := WshShell.Exec(CommandString);
WHILE (FORMAT(WshExec.Status) = '0') AND ((ElapsedTime <= Timeout) OR (Timeout = 0)) DO BEGIN
  SLEEP(TimeStep);
  ElapsedTime += TimeStep;
END;
ExecTimedOut := FORMAT(WshExec.Status) = '0';
IF ExecTimedOut THEN
  WshShell.Run(STRSUBSTNO('taskkill /PID %1',WshExec.ProcessID),WindowStyle,WaitOnReturn);
       
IF ExecTimedOut THEN BEGIN
  // Here logs can be created
END;
EXIT(NOT ExecTimedOut);

Printing documents on user's behalf from remote machine



In this post I will describe real requirements case of our customer (pharmaceutical company).

http://nalpharma.com/wp-content/uploads/2014/01/6.jpgGenerally the requirement was to improve printing performance by changing the way documents were printed through running print from remote Print Server. In previous configuration printing application was ran on RDP server where Role Tailored Client was installed, then the app sent a PDF document of medical specifications from Print Server remote machine (pretty large size) to user's default printer. To improve performance there could be an option to create a job queue and run NAS and Print Server which would send documents from queue directly to printer, but the following limitation made unsuitable such a nice option: the printer device had sophisticated security system which collected sent documents by all users to a queue, and only after user came to it and used an electronic security badge to perform authorization all the documents sent on user's behalf were printed. So after some research the following solution was applied: printing utility was ran on Printing Server instead of RDP server by using Windows Powershell Command:

where in argument list print device name and path to required document were passed.

So in the case it's absolutely required to run remote printing process on behalf of the user this solution can be applied due to it's simplicity and quite good performance and security.

Wednesday 6 August 2014

Error while interacting with Excel "Old format or invalid type library"

Error while interacting with Excel "Old format or invalid type library"

While using Microsoft Office Excel interaction in NAV 2013 you may get the following error message if Language in Excel options doesn't match regional settings on the machine where NAV is running:



Error text:
Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD))

The simplest way to fix the problem is to set reginal setting and language in Excel to equal values.