<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Sentiment &#187; Excel</title>
	<atom:link href="http://wouter.shush.com/category/it-stuff/microsoft/excel/feed" rel="self" type="application/rss+xml" />
	<link>http://wouter.shush.com</link>
	<description>About Life, the Universe, and Everything...</description>
	<lastBuildDate>Tue, 07 Feb 2012 19:27:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Excel automation with Powershell</title>
		<link>http://wouter.shush.com/2007/08/excel-automation-with-powershell?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=excel-automation-with-powershell</link>
		<comments>http://wouter.shush.com/2007/08/excel-automation-with-powershell#comments</comments>
		<pubDate>Thu, 02 Aug 2007 16:05:19 +0000</pubDate>
		<dc:creator>wooter</dc:creator>
				<category><![CDATA[Excel]]></category>
		<category><![CDATA[How-To]]></category>
		<category><![CDATA[IT]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[regional settings]]></category>
		<category><![CDATA[scripting guy]]></category>
		<category><![CDATA[VBScript]]></category>

		<guid isPermaLink="false">http://wouter.shush.com/wordpress/2007/09/16/excel-automation-with-powershell/</guid>
		<description><![CDATA[Powershell is Microsoft&#8217;s answer of decent shell scripting on the Windows platform. Equipped with the functions you would expect from VBScript, it behaves as an Unix shell script would. All commands can work from the command line, or as a &#8230; <a href="http://wouter.shush.com/2007/08/excel-automation-with-powershell">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><!--:en--><a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx">Powershell</a> is Microsoft&#8217;s answer of decent shell scripting on the Windows platform.  Equipped with the functions you would expect from VBScript, it behaves as an Unix shell script would.  All commands can work from the command line, or as a set of instructions in a file.</p>
<p>Adding to the basic shell functions one would expect, such as file and directory manipulation, reading and writing text files, manipulating strings with regular expressions, Powershell also integrates with .NET 2.0 and higher to communicate with other functions, such as accessing the System.Windows.Forms object to make neat forms equipped with all the controls you would expect in a compiled application, or in this case: manipulating Excel workbooks.</p>
<p>Using Excel together with PowerShell can be very easy.  Too bad the documentation is sparse on this topic, and the official documentation makes no mention of a workaround that is needed when using Excel with PowerShell on a machine which is not configured with regional settings set to EN-us.<!--:--><span id="more-6"></span><!--:en--></p>
<p>We start off creating an Excel object, as described in the Scripting Guy post:</p>
<pre lang="powershell" line="1">$objExcel = New-Object -comobject Excel.Application
$objExcel.Visible = $True
$objWorkbook = $objExcel.Workbooks.Add()
$objWorksheet = $objWorkbook.Worksheets.Item(1)
$objWorksheet.Cells.Item(1,1) = "A value in cell A1."
$objWorkbook.SaveAs("C:ScriptsTest.xls")
$objExcel.Quit()</pre>
<p>This little bit wil start Excel, add a worksheet, inset a string in cell A1, and save the Excel worksheet to a file, before quitting.  Unless your machine is not set up with regional settings EN-us.  Then you are greeted with the next message:</p>
<p><code>Exception calling "Add" with "0" argument(s): "Old format or invalid type library. (Exception from HRESULT: 0x80028018<br />
(TYPE_E_INVDATAREAD))"<br />
At line:1 char:39<br />
+ $objWorkbook = $objExcel.Workbooks.Add( &lt; &lt;&lt;&lt; ) </code></p>
<p>When looking for this Error, you will run into threads regarding other scripting and programming languages.  Most notably, KB320369 has some information.</p>
<p>Ok, there is a workaround. See what this code does:</p>
<pre lang="powershell" line="1">function Invoke([object]$m, [string]$method, $parameters)
{
$m.PSBase.GetType().InvokeMember(
$method, [Reflection.BindingFlags]::InvokeMethod, $null, $m, $parameters,$ciUS)
} 

$ciUS = [System.Globalization.CultureInfo]'en-US'

$objExcel = New-object -com Excel.Application
$objExcel.visible = $True
$ci = [System.Globalization.CultureInfo]'en-us'
$objWorkbook = Invoke $objExcel.Workbooks Add
$objWorksheet = $objWorkbook.Worksheets.Item(1)
$objWorksheet.Cells.Item(1,1) = "A value in cell A1."
Invoke $objWorkbook SaveAs "C:ScriptsTest.xls" > $null
Invoke $objWorkbook Close 0 > $null
$objExcel.Quit()</pre>
<p>Still an error!</p>
<p><code>Exception setting "Item": "Exception from HRESULT: 0x800A03EC"<br />
At C:scriptingtest.ps1:15 char:25<br />
+ $objWorksheet.Cells.Item( &lt; &lt;&lt;&lt; 1,1) = "A value in cell A1." </code></p>
<p>Modify the above code a bit, with this line instead of line 14:</p>
<pre lang="powershell" line="14">$objWorksheet.Cells.Item(1,1).FormulaLocal = "A value in cell A1." </pre>
<p>The result?  An Excel-file with data!</p>
<p>If you want to open an Excel-file instead of creating a new one, use the following as a replacement for line 12:</p>
<pre lang="powershell" line="12">$objWorkbook = Invoke $objExcel.Workbooks Open"c:ScriptsTest.xls" </pre>
<p>And if you want to save the Excel file to the existing name, add this before line 16:</p>
<pre lang="powershell" line="16">Invoke $objWorkbook Save > $null </pre>
<p><!--:--></p>
]]></content:encoded>
			<wfw:commentRss>http://wouter.shush.com/2007/08/excel-automation-with-powershell/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced

Served from: wouter.shush.com @ 2012-02-08 13:39:24 -->
