<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://geeks.netindonesia.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Incoherent Rambling : Excel</title><link>http://geeks.netindonesia.net/blogs/jimmy/archive/tags/Excel/default.aspx</link><description>Tags: Excel</description><dc:language>en</dc:language><generator>CommunityServer 2007.1 (Build: 20917.1142)</generator><item><title>How to Change the Active Printer to Specific Printer in Excel Using .NET and How the Heck Can I Find the Right Printer Name and Port Combination that Excel Wants?</title><link>http://geeks.netindonesia.net/blogs/jimmy/archive/2011/02/25/how-to-change-the-active-printer-to-specific-printer-in-excel-using-net-and-how-the-heck-can-i-find-the-right-printer-name-and-port-combination-that-excel-wants.aspx</link><pubDate>Fri, 25 Feb 2011 15:38:00 GMT</pubDate><guid isPermaLink="false">5cc3a90d-ac9a-472a-8983-30514957434c:192149</guid><dc:creator>Jimmy Chandra</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://geeks.netindonesia.net/blogs/jimmy/rsscomments.aspx?PostID=192149</wfw:commentRss><comments>http://geeks.netindonesia.net/blogs/jimmy/archive/2011/02/25/how-to-change-the-active-printer-to-specific-printer-in-excel-using-net-and-how-the-heck-can-i-find-the-right-printer-name-and-port-combination-that-excel-wants.aspx#comments</comments><description>&lt;p&gt;A client of mine was having a problem trying to automate Excel to print to a specific (non default) printer.&amp;nbsp; They know which printer they want to use, but are having trouble trying to get Excel to understand it.&lt;/p&gt;  &lt;p&gt;Excel’s &lt;strong&gt;&lt;a title="Excel Application ActivePrinter property" href="http://msdn.microsoft.com/en-us/library/ff822927.aspx" target="_blank"&gt;Application.ActivePrinter&lt;/a&gt;&lt;/strong&gt; property is quite picky about what it will accept.&amp;nbsp; The right pattern to feed to the property is something like&lt;em&gt; &lt;/em&gt;“&lt;strong&gt;&lt;em&gt;PrinterName&lt;/em&gt; on&lt;/strong&gt;&lt;em&gt;&lt;strong&gt; PortName&lt;/strong&gt;:&lt;/em&gt;”&lt;em&gt;.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;For example, if you want to print to Microsoft XPS Document Writer, you need to do something like this:&lt;/p&gt;  &lt;pre class="csharpcode"&gt;var excel = &lt;span class="kwrd"&gt;new&lt;/span&gt; Excel.Application();
var workbook = excel.Workbooks.Open(&lt;span class="str"&gt;&amp;quot;....&amp;quot;&lt;/span&gt;);
var worksheet = workbook.ActiveSheet;

excel.ActivePrinter = “Microsoft XPS Document Writer on ne01:”;
worksheet.PrintOut();

excel.Quit();&lt;/pre&gt;

&lt;p&gt;And this of course assuming that the selected printer (&lt;strong&gt;Microsoft XPS Document Writer&lt;/strong&gt;) is on port &lt;strong&gt;ne01&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Getting the right name for the printer is somewhat trivial.&amp;nbsp; You can poke around the &lt;strong&gt;Devices and Printers&lt;/strong&gt; control panel item on Windows and see their name. &lt;/p&gt;

&lt;p&gt;The problem is… the port name information is not so obvious to find.&lt;/p&gt;

&lt;p&gt;So, how would you know where to get the port name?&lt;/p&gt;

&lt;p&gt;Trying to enumerate the attached printers as described by &lt;a title="Is there a .NET way to enumerate all available network printers?" href="http://stackoverflow.com/questions/1018001/is-there-a-net-way-to-enumerate-all-available-network-printers/1018061" target="_blank"&gt;this StackOverflow question&lt;/a&gt; does not gives you the right port name… See the sample code and output below…&lt;/p&gt;

&lt;p&gt;The Code:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;
&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Management;

&lt;span class="kwrd"&gt;namespace&lt;/span&gt; ConsoleApplication1
{
    &lt;span class="kwrd"&gt;class&lt;/span&gt; Program
    {
        &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main(&lt;span class="kwrd"&gt;string&lt;/span&gt;[] args)
        {
            var oquery = &lt;span class="kwrd"&gt;new&lt;/span&gt; ObjectQuery(&lt;span class="str"&gt;&amp;quot;SELECT * FROM Win32_Printer&amp;quot;&lt;/span&gt;);
            var mosearcher = &lt;span class="kwrd"&gt;new&lt;/span&gt; ManagementObjectSearcher(oquery);
            var moc = mosearcher.Get();            

            &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (var o &lt;span class="kwrd"&gt;in&lt;/span&gt; moc)
            {
                var pdc = o.Properties;

                &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (var p &lt;span class="kwrd"&gt;in&lt;/span&gt; pdc)
                {
                    &lt;span class="kwrd"&gt;if&lt;/span&gt; (p.Name == &lt;span class="str"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt; || p.Name == &lt;span class="str"&gt;&amp;quot;PortName&amp;quot;&lt;/span&gt;)
                    {

                        Console.WriteLine(&lt;span class="str"&gt;&amp;quot;{0}: {1}&amp;quot;&lt;/span&gt;, p.Name, o[p.Name]);
                    }
                }
            }
        }
    }
}&lt;/pre&gt;

&lt;p&gt;The result:&lt;/p&gt;

&lt;p&gt;&lt;font face="Courier New"&gt;Name: Xerox WorkCentre 3119 Series
    &lt;br /&gt;PortName: USB001

    &lt;br /&gt;Name: Send To OneNote 2010

    &lt;br /&gt;PortName: nul:

    &lt;br /&gt;Name: Microsoft XPS Document Writer

    &lt;br /&gt;PortName: XPSPort:

    &lt;br /&gt;Name: CAMPUS-R101-DC236

    &lt;br /&gt;PortName: 192.168.100.21

    &lt;br /&gt;Name: CAMPUS-R217-DC236

    &lt;br /&gt;PortName: 192.168.100.22

    &lt;br /&gt;Name: Fax

    &lt;br /&gt;PortName: SHRFAX:

    &lt;br /&gt;Name: \\TIGER\Canon Inkjet MP140 series

    &lt;br /&gt;PortName: USB001&lt;/font&gt;&lt;/p&gt;



&lt;p&gt;&lt;em&gt;You just have to trust me the port name here is not the port name that Excel wants to use.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;So, where else can you get this particular information?&lt;/p&gt;

&lt;p&gt;After looking around the web, I found &lt;a href="http://www.xtremevbtalk.com/showthread.php?t=258994" target="_blank"&gt;this post&lt;/a&gt; that gives some clues on how to do this from VBA that can easily be adapted for .NET.&lt;/p&gt;

&lt;p&gt;It turns out that the key to this is to poke around in the registry. in particular &lt;strong&gt;HKCU\Software\Microsoft\Windows NT\CurrentVersion\Devices&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So I came up with the code below to test it and lo and behold.&amp;nbsp; It works perfectly.&amp;nbsp; Basically I enumerate through all the devices in that particular registry location, did some string manipulation on the port (remove the &lt;strong&gt;winspool,&lt;/strong&gt; prefix from the port name) and concatenate the stripped version of the port name with the printer / device name to form the &lt;strong&gt;PrinterName on PortName:&lt;/strong&gt; combination that is required by Excel.&lt;/p&gt;



&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;
&lt;span class="kwrd"&gt;using&lt;/span&gt; Microsoft.Win32;
&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Runtime.InteropServices;
&lt;span class="kwrd"&gt;using&lt;/span&gt; Excel = Microsoft.Office.Interop.Excel;

&lt;span class="kwrd"&gt;namespace&lt;/span&gt; ConsoleApplication1
{
    &lt;span class="kwrd"&gt;class&lt;/span&gt; Program
    {
        &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main(&lt;span class="kwrd"&gt;string&lt;/span&gt;[] args)
        {
            var excelApp = &lt;span class="kwrd"&gt;new&lt;/span&gt; Excel.Application();
            excelApp.Visible = &lt;span class="kwrd"&gt;true&lt;/span&gt;;

            var workbook = excelApp.Workbooks.Add();
            var worksheet = workbook.ActiveSheet;

            var key = Registry.CurrentUser;
            var subkey = key.OpenSubKey(&lt;span class="str"&gt;@&amp;quot;Software\Microsoft\Windows NT\CurrentVersion\Devices&amp;quot;&lt;/span&gt;);

            var printerNames = subkey.GetValueNames();

            &lt;span class="rem"&gt;//Should be able to complete this loop without throwing an exception&lt;/span&gt;
            &lt;span class="rem"&gt;//if all the names matches to what Excel is expecting.&lt;/span&gt;
            &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (var printerName &lt;span class="kwrd"&gt;in&lt;/span&gt; printerNames)
            {
                var excelPrinterName = ConvertToExcelPrinterFriendlyName(printerName);
                Console.WriteLine(excelPrinterName);

                excelApp.ActivePrinter = excelPrinterName;
            }

            GC.Collect();
            GC.WaitForPendingFinalizers();

            Marshal.FinalReleaseComObject(worksheet);

            workbook.Close(&lt;span class="kwrd"&gt;false&lt;/span&gt;);
            Marshal.FinalReleaseComObject(workbook);

            excelApp.Quit();
            Marshal.FinalReleaseComObject(excelApp);
        }

        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; ConvertToExcelPrinterFriendlyName(&lt;span class="kwrd"&gt;string&lt;/span&gt; printerName)
        {
            var key = Registry.CurrentUser;
            var subkey = key.OpenSubKey(&lt;span class="str"&gt;@&amp;quot;Software\Microsoft\Windows NT\CurrentVersion\Devices&amp;quot;&lt;/span&gt;);

            var &lt;span class="kwrd"&gt;value&lt;/span&gt; = subkey.GetValue(printerName);
            &lt;span class="kwrd"&gt;if&lt;/span&gt; (&lt;span class="kwrd"&gt;value&lt;/span&gt; == &lt;span class="kwrd"&gt;null&lt;/span&gt;) &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; Exception(&lt;span class="kwrd"&gt;string&lt;/span&gt;.Format(&lt;span class="str"&gt;&amp;quot;Device not found: {0}&amp;quot;&lt;/span&gt;, printerName));

            var portName = &lt;span class="kwrd"&gt;value&lt;/span&gt;.ToString().Substring(9);  &lt;span class="rem"&gt;//strip away the winspool, &lt;/span&gt;
 
            &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt;.Format(&lt;span class="str"&gt;&amp;quot;{0} on {1}&amp;quot;&lt;/span&gt;, printerName, portName);;
        }
    }
}&lt;/pre&gt;



&lt;p&gt;Hope this is useful for those who need it… Enjoy.&lt;/p&gt;
&lt;div class = "shareblock"&gt;&lt;strong&gt;Share this post: &lt;/strong&gt;&lt;a href="mailto:?body=Thought you might like this: http://geeks.netindonesia.net/blogs/jimmy/archive/2011/02/25/how-to-change-the-active-printer-to-specific-printer-in-excel-using-net-and-how-the-heck-can-i-find-the-right-printer-name-and-port-combination-that-excel-wants.aspx&amp;amp;subject=How to Change the Active Printer to Specific Printer in Excel Using .NET and How the Heck Can I Find the Right Printer Name and Port Combination that Excel Wants?" target="_blank" title="Send via email"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Mail.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="http://www.facebook.com/sharer.php?u=http://geeks.netindonesia.net/blogs/jimmy/archive/2011/02/25/how-to-change-the-active-printer-to-specific-printer-in-excel-using-net-and-how-the-heck-can-i-find-the-right-printer-name-and-port-combination-that-excel-wants.aspx&amp;amp;t=How+to+Change+the+Active+Printer+to+Specific+Printer+in+Excel+Using+.NET+and+How+the+Heck+Can+I+Find+the+Right+Printer+Name+and+Port+Combination+that+Excel+Wants%3f" target="_blank" title="Submit How to Change the Active Printer to Specific Printer in Excel Using .NET and How the Heck Can I Find the Right Printer Name and Port Combination that Excel Wants? to DotNetKicks"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Facebook.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="http://del.icio.us/post?url=http://geeks.netindonesia.net/blogs/jimmy/archive/2011/02/25/how-to-change-the-active-printer-to-specific-printer-in-excel-using-net-and-how-the-heck-can-i-find-the-right-printer-name-and-port-combination-that-excel-wants.aspx&amp;amp;title=How+to+Change+the+Active+Printer+to+Specific+Printer+in+Excel+Using+.NET+and+How+the+Heck+Can+I+Find+the+Right+Printer+Name+and+Port+Combination+that+Excel+Wants%3f" target="_blank" title="Submit How to Change the Active Printer to Specific Printer in Excel Using .NET and How the Heck Can I Find the Right Printer Name and Port Combination that Excel Wants? to del.icio.us"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Delicious.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="http://www.digg.com/submit?url=http://geeks.netindonesia.net/blogs/jimmy/archive/2011/02/25/how-to-change-the-active-printer-to-specific-printer-in-excel-using-net-and-how-the-heck-can-i-find-the-right-printer-name-and-port-combination-that-excel-wants.aspx&amp;amp;phase=2" target="_blank" title="Submit How to Change the Active Printer to Specific Printer in Excel Using .NET and How the Heck Can I Find the Right Printer Name and Port Combination that Excel Wants? to digg.com"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Digg.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;mkt=en-us&amp;amp;url=http://geeks.netindonesia.net/blogs/jimmy/archive/2011/02/25/how-to-change-the-active-printer-to-specific-printer-in-excel-using-net-and-how-the-heck-can-i-find-the-right-printer-name-and-port-combination-that-excel-wants.aspx&amp;amp;title=How+to+Change+the+Active+Printer+to+Specific+Printer+in+Excel+Using+.NET+and+How+the+Heck+Can+I+Find+the+Right+Printer+Name+and+Port+Combination+that+Excel+Wants%3f" target="_blank" title="Add How to Change the Active Printer to Specific Printer in Excel Using .NET and How the Heck Can I Find the Right Printer Name and Port Combination that Excel Wants? to Live Bookmarks"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Live.16.gif" border="0"&gt;&lt;/a&gt;&lt;/div&gt;&lt;img src="http://geeks.netindonesia.net/aggbug.aspx?PostID=192149" width="1" height="1"&gt;</description><category domain="http://geeks.netindonesia.net/blogs/jimmy/archive/tags/Excel/default.aspx">Excel</category><category domain="http://geeks.netindonesia.net/blogs/jimmy/archive/tags/Interops/default.aspx">Interops</category></item><item><title>Converting Excel Date to .NET DateTime</title><link>http://geeks.netindonesia.net/blogs/jimmy/archive/2006/05/31/Converting-Excel-Date-to-.NET-DateTime.aspx</link><pubDate>Tue, 30 May 2006 19:46:00 GMT</pubDate><guid isPermaLink="false">5cc3a90d-ac9a-472a-8983-30514957434c:9832</guid><dc:creator>Jimmy Chandra</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://geeks.netindonesia.net/blogs/jimmy/rsscomments.aspx?PostID=9832</wfw:commentRss><comments>http://geeks.netindonesia.net/blogs/jimmy/archive/2006/05/31/Converting-Excel-Date-to-.NET-DateTime.aspx#comments</comments><description>&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;Needed this for a project.&amp;nbsp; Couldn't figured out how to do it.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;After googling for sometime, found out how to convert Excel Date cell to .NET DateTime...&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;DateTime.FromOADate(_Enter_Your_Excel_Date_Value_Here_)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;Silly thing :)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;div class = "shareblock"&gt;&lt;strong&gt;Share this post: &lt;/strong&gt;&lt;a href="mailto:?body=Thought you might like this: http://geeks.netindonesia.net/blogs/jimmy/archive/2006/05/31/Converting-Excel-Date-to-.NET-DateTime.aspx&amp;amp;subject=Converting Excel Date to .NET DateTime" target="_blank" title="Send via email"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Mail.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="http://www.facebook.com/sharer.php?u=http://geeks.netindonesia.net/blogs/jimmy/archive/2006/05/31/Converting-Excel-Date-to-.NET-DateTime.aspx&amp;amp;t=Converting+Excel+Date+to+.NET+DateTime" target="_blank" title="Submit Converting Excel Date to .NET DateTime to DotNetKicks"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Facebook.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="http://del.icio.us/post?url=http://geeks.netindonesia.net/blogs/jimmy/archive/2006/05/31/Converting-Excel-Date-to-.NET-DateTime.aspx&amp;amp;title=Converting+Excel+Date+to+.NET+DateTime" target="_blank" title="Submit Converting Excel Date to .NET DateTime to del.icio.us"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Delicious.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="http://www.digg.com/submit?url=http://geeks.netindonesia.net/blogs/jimmy/archive/2006/05/31/Converting-Excel-Date-to-.NET-DateTime.aspx&amp;amp;phase=2" target="_blank" title="Submit Converting Excel Date to .NET DateTime to digg.com"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Digg.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;mkt=en-us&amp;amp;url=http://geeks.netindonesia.net/blogs/jimmy/archive/2006/05/31/Converting-Excel-Date-to-.NET-DateTime.aspx&amp;amp;title=Converting+Excel+Date+to+.NET+DateTime" target="_blank" title="Add Converting Excel Date to .NET DateTime to Live Bookmarks"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Live.16.gif" border="0"&gt;&lt;/a&gt;&lt;/div&gt;&lt;img src="http://geeks.netindonesia.net/aggbug.aspx?PostID=9832" width="1" height="1"&gt;</description><category domain="http://geeks.netindonesia.net/blogs/jimmy/archive/tags/.NET/default.aspx">.NET</category><category domain="http://geeks.netindonesia.net/blogs/jimmy/archive/tags/Excel/default.aspx">Excel</category><category domain="http://geeks.netindonesia.net/blogs/jimmy/archive/tags/Interops/default.aspx">Interops</category></item></channel></rss>