<?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>MsHelp &#187; COM Interop</title>
	<atom:link href="http://mshelp.be/category/com-interop/feed" rel="self" type="application/rss+xml" />
	<link>http://mshelp.be</link>
	<description>Ins and Outs of Microsoft Dynamics CRM</description>
	<lastBuildDate>Fri, 06 Apr 2012 11:09:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>COM interop improvements with C#4.0</title>
		<link>http://mshelp.be/com-interop-improvements-with-c4-0-307.htm</link>
		<comments>http://mshelp.be/com-interop-improvements-with-c4-0-307.htm#comments</comments>
		<pubDate>Tue, 09 Feb 2010 22:46:27 +0000</pubDate>
		<dc:creator>Sven</dc:creator>
				<category><![CDATA[.NET 4.0]]></category>
		<category><![CDATA[COM Interop]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>

		<guid isPermaLink="false">http://mshelp.be/com-interop-improvements-with-c4-0-307.htm</guid>
		<description><![CDATA[I already did a blog post about this some time ago: Better Office COM interop thanks to named and optional parameters Here I talked mainly on how named and optional parameters make life easier for Office developers. This focus was more on that. But actually there are a some other points that didnâ€™t get enough [...]]]></description>
			<content:encoded><![CDATA[<p>I already did a blog post about this some time ago: <a href="http://mshelp.be/better-office-com-interop-thanks-to-named-and-optional-parameters-272.htm">Better Office COM interop thanks to named and optional parameters</a></p>
<p>Here I talked mainly on how named and optional parameters make life easier for Office developers. This focus was more on that. But actually there are a some other points that didnâ€™t get enough attention. Here are some other great improvements specifically for COM interop.</p>
<ul>
<li>No more meaningless ref parameters. </li>
<li>Better indexer support, property accessing. No specific get/set needed anymore. </li>
<li>No more need for casting, we can use dynamic binding thanks to the Dynamic class. </li>
</ul>
<p>I have found a screencast by <a href="http://blogs.msdn.com/samng">Sam Ng</a> who explains all this in about 10min.</p>
<p> <script src="http://msdn.microsoft.com/objectforward/default.aspx?type=VideoPlayer&amp;video=http%3A%2F%2Fdownload.microsoft.com%2Fdownload%2F1%2F8%2F3%2F18391C4D-DC57-45E8-A0D2-37F4919F34BE%2FHDI-MSDN-VCSharp-winvideo-ComInterop.wmv&amp;thumb=http%3A%2F%2Fi.msdn.microsoft.com%2Fee460939.CSharp4ComInterop_400X282(en-us%252cMSDN.10).png&amp;title=&amp;width=400&amp;height=400" type="text/javascript"></script><br />
<h5>Link to the original post â€“&gt; <a href="http://msdn.microsoft.com/en-us/vcsharp/ee460939.aspx">How Do I: COM Interop and Office in C# 4.0?</a></h5>
]]></content:encoded>
			<wfw:commentRss>http://mshelp.be/com-interop-improvements-with-c4-0-307.htm/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Better Office COM interop thanks to named and optional parameters</title>
		<link>http://mshelp.be/better-office-com-interop-thanks-to-named-and-optional-parameters-272.htm</link>
		<comments>http://mshelp.be/better-office-com-interop-thanks-to-named-and-optional-parameters-272.htm#comments</comments>
		<pubDate>Sat, 09 Jan 2010 13:42:30 +0000</pubDate>
		<dc:creator>Sven</dc:creator>
				<category><![CDATA[.NET 4.0]]></category>
		<category><![CDATA[COM Interop]]></category>

		<guid isPermaLink="false">http://mshelp.be/better-office-com-interop-thanks-to-named-and-optional-parameters-272.htm</guid>
		<description><![CDATA[In my last screencast I showed you how to work with two new features in C#4.0 which are named and optional parameters. Now in this post I will show you another way how you can benefit from these new features. Remember when you had to create these Microsoft Word document with the Office interop API [...]]]></description>
			<content:encoded><![CDATA[<p>In my last <a href="http://mshelp.be/screencast-net-4-0-named-and-optional-parameters-268.htm">screencast</a> I showed you how to work with two new features in C#4.0 which are named and optional parameters. Now in this post I will show you another way how you can benefit from these new features. </p>
<p>Remember when you had to create these Microsoft Word document with the Office interop API and had to pass all these tedious by ref parameters. Things you absolutely didnâ€™t care about! But anyway the Office COM Interop assemblies were expecting them. Thanks to named and optional parameters this will all go away!</p>
<p>So letâ€™s first start with adding a reference to the Microsoft Office Word Interop assembly.</p>
<p><a href="http://www.mshelp.be/img/BetterOfficeCOMinteropthankstonamedandop_CE43/image.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.mshelp.be/img/BetterOfficeCOMinteropthankstonamedandop_CE43/image_thumb.png" width="244" height="207" /></a> </p>
<p>Following is the code we had to write before we could use named and optional parameters.</p>
<p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:2d8e2899-89cc-437c-bc41-d94ce7bdafd5" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;overflow: auto;"><span style="color: #0000FF;">class</span><span style="color: #000000;"> Program
{
   </span><span style="color: #0000FF;">static</span><span style="color: #000000;"> </span><span style="color: #0000FF;">void</span><span style="color: #000000;"> Main(</span><span style="color: #0000FF;">string</span><span style="color: #000000;">[] args)
   {
       </span><span style="color: #0000FF;">object</span><span style="color: #000000;"> missing </span><span style="color: #000000;">=</span><span style="color: #000000;"> System.Reflection.Missing.Value;
       </span><span style="color: #0000FF;">object</span><span style="color: #000000;"> visible </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">true</span><span style="color: #000000;">;

       </span><span style="color: #008000;">//</span><span style="color: #008000;">Create instance of Word application</span><span style="color: #008000;">
</span><span style="color: #000000;">       Word.Application wordApp </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> Microsoft.Office.Interop.Word.Application();
       </span><span style="color: #008000;">//</span><span style="color: #008000;">Make the Word application visible</span><span style="color: #008000;">
</span><span style="color: #000000;">       wordApp.Visible </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">true</span><span style="color: #000000;">;
       </span><span style="color: #008000;">//</span><span style="color: #008000;">Create instance of Word document</span><span style="color: #008000;">
</span><span style="color: #000000;">       Word.Document wordDocument;

       </span><span style="color: #008000;">//</span><span style="color: #008000;">Add document to document collection of word instance</span><span style="color: #008000;">
</span><span style="color: #000000;">       wordDocument </span><span style="color: #000000;">=</span><span style="color: #000000;"> wordApp.Documents.Add(</span><span style="color: #0000FF;">ref</span><span style="color: #000000;"> missing, </span><span style="color: #0000FF;">ref</span><span style="color: #000000;"> missing, </span><span style="color: #0000FF;">ref</span><span style="color: #000000;"> missing,</span><span style="color: #0000FF;">ref</span><span style="color: #000000;"> visible);
       </span><span style="color: #008000;">//</span><span style="color: #008000;">Type text in our Word document</span><span style="color: #008000;">
</span><span style="color: #000000;">       wordApp.Selection.TypeText(</span><span style="color: #800000;">"</span><span style="color: #800000;">This is my dynamically created word document</span><span style="color: #800000;">"</span><span style="color: #000000;">);

       Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Done</span><span style="color: #800000;">"</span><span style="color: #000000;">);
       Console.In.ReadLine();

   }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>As you can see we had to define this missing object and pass it when we added our Word document to the documents collection of the Microsoft Word application instance. This doesnâ€™t mean that much but hey we had to add it anyway to make it work. Now with optional parameters this isnâ€™t needed anymore. We can just leave them out!</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:ff73f3c9-03f6-47aa-a967-21171c0c2d52" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;overflow: auto;"><span style="color: #000000;">wordDocument </span><span style="color: #000000;">=</span><span style="color: #000000;"> wordApp.Documents.Add(</span><span style="color: #0000FF;">ref</span><span style="color: #000000;"> visible);</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>Will this do the trick then? Well not really because now you have removed 3 missing objects and are left with the visible Boolean. To make it really work, we have to define which parameter the visible object represents. This can be done with the named parameter feature. Now the method call will look like this:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:a36b2d85-a81b-4e94-8610-501485058adf" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;overflow: auto;"><span style="color: #000000;">wordDocument </span><span style="color: #000000;">=</span><span style="color: #000000;"> wordApp.Documents.Add(Visible:</span><span style="color: #0000FF;">ref</span><span style="color: #000000;"> visible);</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>Isnâ€™t this so cool? But wait it gets even better. Another new feature of C# 4.0 is that we donâ€™t need to explicitly pass parameters by reference anymore. So we can clean our code up some more. Finally we will end up with this piece of code, which is by the way much more readable.</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:d0e0e576-4305-49df-97cc-72262b72553f" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;overflow: auto;"><span style="color: #0000FF;">class</span><span style="color: #000000;"> Program
{
   </span><span style="color: #0000FF;">static</span><span style="color: #000000;"> </span><span style="color: #0000FF;">void</span><span style="color: #000000;"> Main(</span><span style="color: #0000FF;">string</span><span style="color: #000000;">[] args)
   {
       </span><span style="color: #008000;">//</span><span style="color: #008000;">Create instance of Word application</span><span style="color: #008000;">
</span><span style="color: #000000;">       Word.Application wordApp </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> Microsoft.Office.Interop.Word.Application();
       </span><span style="color: #008000;">//</span><span style="color: #008000;">Make the Word application visible</span><span style="color: #008000;">
</span><span style="color: #000000;">       wordApp.Visible </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">true</span><span style="color: #000000;">;
       </span><span style="color: #008000;">//</span><span style="color: #008000;">Create instance of Word document</span><span style="color: #008000;">
</span><span style="color: #000000;">       Word.Document wordDocument;

       </span><span style="color: #008000;">//</span><span style="color: #008000;">Add document to document collection of word instance</span><span style="color: #008000;">
</span><span style="color: #000000;">       wordDocument </span><span style="color: #000000;">=</span><span style="color: #000000;"> wordApp.Documents.Add(Visible:</span><span style="color: #0000FF;">true</span><span style="color: #000000;">);
       </span><span style="color: #008000;">//</span><span style="color: #008000;">Type text in our Word document</span><span style="color: #008000;">
</span><span style="color: #000000;">       wordApp.Selection.TypeText(</span><span style="color: #800000;">"</span><span style="color: #800000;">This is my dynamically created word document</span><span style="color: #800000;">"</span><span style="color: #000000;">);

       Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Done</span><span style="color: #800000;">"</span><span style="color: #000000;">);
       Console.In.ReadLine();

   }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
]]></content:encoded>
			<wfw:commentRss>http://mshelp.be/better-office-com-interop-thanks-to-named-and-optional-parameters-272.htm/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

