Insert network printers automatically

In my current upgrade project I need to upgrade a solution I did for Classic NAV where I used the “‘Windows Script Host Object Model’.WshNetwork” Automation object to add a network printer automatically.

I looked a round and did not find any dotnet object capable of adding a network printer.  However, there was a c# code I found that was able to do this.  So, I created a NAV Add-in with this code

[code lang=”csharp”]using System;
using System.Runtime.InteropServices;

namespace NAVPrinterAdd_in
{
public static class PrinterControls
{
[DllImport("winspool.drv")]
static extern bool AddPrinterConnection(string pName);

public static bool AddNetworkPrinter(string networkPrinterPath, ref string networkMessage)
{
bool result;
try
{
result = AddPrinterConnection(networkPrinterPath);
networkMessage = "";
}
catch (Exception e)
{
result = false;
networkMessage = e.Message;
}
return result;
}
}
}

[/code]

The Add-in is attached. Next I created a Codeunit in NAV.

AddPrinter

And used in Codeunit 1, FindPrinter trigger.

codeunit1

Now I can make sure that the correct printer is installed for the client and also for the server when printing from NAS session.

NAVPrinterAdd-in

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.