Note that there are some explanatory texts on larger screens.

plurals
  1. PORetrieve port name for excel.ActivePrinter
    text
    copied!<p>Hi for the print method to work on excel i need the printername and the port name like this: "printer On NE3" just as an example. The problem is, is that I use a virtual printer and the port is different, pretty much every time i need to use it.</p> <p>The program needs to run in the background, so open dialog boxes to choose is not an option. I have also tried to retrieve the port name from WMI with this:</p> <pre><code> protected string FindPrinterWithPort(string printerName) { StringBuilder query = new StringBuilder(); query.Append("SELECT * FROM Win32_Printer "); query.Append("WHERE DeviceID = \""+printerName+"\""); ObjectQuery objectQuery = new ObjectQuery(query.ToString()); var searcher = new ManagementObjectSearcher(objectQuery); foreach (ManagementObject mo in searcher.Get()) { return printerName +" On "+ mo["PortName"]; } return string.Empty; } </code></pre> <p>The portname i receive doing this gives me the full path to the virtual printer program. I previously used the methods below to change printer for excel, and i alway knew they should be changed eventually, the code is neither fast nor good, but it worked for a while, until I made it multi-threaded and its just a big blockade. Because i need to lock these methods not to change the default printer in windows.</p> <pre><code> private bool SetPrinterForExcel(string printerName){ string[] ports = new string[]{"Ne00:", "Ne01:", "Ne02:", "Ne03:", "Ne04:", "Ne05:", "Ne06:", "Ne07:", "Ne08:", "Ne09:", "Ne10:", "Ne11:", "Ne12:", "Ne13:", "Ne14:", "Ne15:", "Ne16:", "LPT1:", "LPT2:", "File:", "SMC100:"}; foreach (string port in ports) { string printerWithPort = printerName + " On " + port; bool success = SetAndTestPrinter(printerWithPort); if(success) return true; } return false; } private bool SetAndTestPrinter(string printerWithPort) { try { excel.ActivePrinter = printerWithPort; return true; } catch { return false; } } </code></pre> <p>Any Idea how to retrieve the port, or is there a way from WMI query i can get the correct port.</p> <p>Thanks in advance</p> <p>Edit:</p> <p>I have not gotten it work getting, what i have done to print now is change the default printer on windows and then just do the print method on my excel object. I Changed it by using:</p> <pre><code> [DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)] public static extern bool SetDefaultPrinter(string Name); </code></pre> <p>And i lock it method and print. I have thought about trying to do a lock and save the ActivePrinter, even tho i believe performance will properly just as bad. Because the lock will stay until the default printer is changed, which takes sometimes up to 1 second, in a multi-threaded envirorment thats is a very long lock.</p> <pre><code> string printernamewithPort = string.Empty; lock (ActivePrinterLock) { SetDefaultPrinter(printername); printernamewithPort = excel.ActivePrinter; } foreach (Worksheet worksheet in workbook.Worksheets) { worksheet.PageSetup.PaperSize = XlPaperSize.xlPaperA4; worksheet.PageSetup.Orientation = XlPageOrientation.xlPortrait; worksheet.PageSetup.FitToPagesWide = 1; worksheet.PageSetup.FitToPagesTall = false; worksheet.PrintOutEx(ActivePrinter: printernamewithPort,Collate: true, Preview: false, PrintToFile: false); } </code></pre> <p>What i dunno is that, if the default printer changes right away from another thread, if it looses the port given to the virtual printer. I have not done proper testing of this. In normal circumstances it seems that my Method FindPrinterWithPort would work.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload