<?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>ManvirSingh.net &#187; visual studio</title>
	<atom:link href="http://www.manvirsingh.net/index.php/tag/visual-studio/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.manvirsingh.net</link>
	<description>Manvir&#039;s Blog!</description>
	<lastBuildDate>Tue, 01 Jun 2010 09:21:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Demystifying Microsoft Office Object Model &#8211; Part I</title>
		<link>http://www.manvirsingh.net/index.php/demystifying-microsoft-office-object-model-part-i/</link>
		<comments>http://www.manvirsingh.net/index.php/demystifying-microsoft-office-object-model-part-i/#comments</comments>
		<pubDate>Sun, 26 Jul 2009 13:00:05 +0000</pubDate>
		<dc:creator>Manvir</dc:creator>
				<category><![CDATA[Office Automation]]></category>
		<category><![CDATA[automation]]></category>
		<category><![CDATA[office]]></category>
		<category><![CDATA[visual studio]]></category>

		<guid isPermaLink="false">http://www.manvirsingh.net/?p=39</guid>
		<description><![CDATA[After programming with the Microsoft Office applications like Word, Excel, and PowerPoint for some time, I feel that using these applications in your own solutions can be quite overwhelming for the first timers. So, I thought of writing this post to introduce the concepts to everyone who wants to program/use Office applications in their own [...]]]></description>
			<content:encoded><![CDATA[<p>After programming with the Microsoft Office applications like Word, Excel, and PowerPoint for some time, I feel that using these applications in your own solutions can be quite overwhelming for the first timers. So, I thought of writing this post to introduce the concepts to everyone who wants to program/use Office applications in their own solutions.</p>
<p>The idea of this post is to get you acquainted with the generic concepts related to programming Microsoft Office applications (from here on, whenever I use this term I am mainly referring to Word/Excel/PowerPoint; however, this doesn’t mean that other Office applications cannot be automated). I assume that you already know how to create WinForms/MFC projects using Microsoft Visual Studio. So, let’s begin…</p>
<p>Most widely used Microsoft Office suite applications like Word, Excel, and PowerPoint provide developers with various interfaces, using which they can automate many tasks of these applications; called Office Object Model. Office Object Model is nothing, but a set of interfaces/classes/objects (different terms – same meaning) which allows programmers to call/ check/operate various features of these applications programmatically, which is also called <strong>*Automation*</strong>.</p>
<p>Apart from being desktop applications, Microsoft Office Applications are also COM components, which expose custom Interfaces for you to utilize in your own solutions. Using these components in a C++ based application involves using the COM APIs (like <em>CoCreateInstance</em> etc.). On the other hand, if you are creating a .Net framework based application then you need something called Microsoft Office Primary Interop Assemblies (PIA) installed on your machine. These are the assemblies provided by Microsoft for use in .Net applications specifically. You can download these from following URLs:</p>
<ol>
<li><a title="Download: Office 2003 Primary Interop Assemblies" href="http://www.microsoft.com/downloads/details.aspx?FamilyID=3C9A983A-AC14-4125-8BA0-D36D67E0F4AD&amp;displaylang=en" target="_blank">Download link for Office 2003 PIAs</a> </li>
<li><a title="Download: Office 2007 Primary Interop Assemblies" href="http://www.microsoft.com/downloads/details.aspx?familyid=59daebaa-bed4-4282-a28c-b864d8bfa513&amp;displaylang=en" target="_blank">Download link for Office 2007 PIAs </a></li>
</ol>
<p>Now, before we get into the thick of things, let’s see what it means to use Microsoft Office applications in custom solutions. We will create two applications, viz., a MFC based Dialog box application and a C# based WinForms application. We will then launch Word from these two, and insert a very magical sentence in a new Word document: “Hello World!”… <img src='http://www.manvirsingh.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>Create a MFC based Dialog box application. Add a button to your main dialog box; double click on it and Visual Studio will generate a click event handler for your button. Leave it as it is for now, we’ll come back to it in a few minutes.</p>
<p>To be able to automate Word from MFC based application, we need to define (or rather generate) few classes which will handle to task of instantiating required COM interface, and making function calls to these interfaces for us. For this, right click on your project node (in solution explorer window) and select Add -&gt; Class… From the Add Class dialog box, select “MFC Class from TypeLib” template and click Add (you may have to select the MFC node in the Categories tree to see this Template); as below:</p>
<p><a href="http://www.manvirsingh.net/wp-content/uploads/2009/07/Add_MFC_Class.png"><img class="aligncenter size-medium wp-image-40" title="Add new MFC Class from TypeLib" alt="Add new MFC Class from TypeLib" src="http://www.manvirsingh.net/wp-content/uploads/2009/07/Add_MFC_Class-300x221.png" width="300" height="221" /></a></p>
<p>In the Add Class from TypeLib wizard, select “Microsoft Word XX.0 Object Library” (the value of XX will be 12 if you have Office 2007, and 11 if you have Office 2003 installed on your machine). From the list of available interfaces, select the ones shown in the following screenshot:</p>
<p><a href="http://www.manvirsingh.net/wp-content/uploads/2009/07/Select_MFC_Typelib_Interfaces.png"><img class="aligncenter size-medium wp-image-41" title="Select the Interfaces for which Classes will be generated" alt="Select the Interfaces for which Classes will be generated" src="http://www.manvirsingh.net/wp-content/uploads/2009/07/Select_MFC_Typelib_Interfaces-300x255.png" width="300" height="255" /></a></p>
<p>On clicking the “Finish” button, the wizard will generate header files corresponding to the classes which we have selected above.</p>
<p>At this moment, if you try to compile your project, you may receive many errors. This is due to a known issue with MFC Class generation wizard (don’t know if Microsoft knows about this, but at least I know it.. <img src='http://www.manvirsingh.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  ), and to fix this open each one of the generated files, and comment the following line:</p>
<p>&#160;</p>
<p style="font-family: &#39;Courier New&#39;">#import <span style="color: red">&quot;C:\\Program Files\\Microsoft Office\\Office12\\MSWORD.OLB&quot;</span> no_namespace</p>
<p>&#160;</p>
<p>This #import is actually not required when using MFC classes generated from TypeLib.</p>
<p>Now, copy and paste the following code snippet in your newly created button’s event handler:</p>
<p>&#160;</p>
<div class="cppcode_1">
<pre>::CoInitialize(NULL); <span class="rem">// Initialize the COM related stuff</span>
VARIANT vMissing; <span class="rem">// VARIANT defining a missing/optional parameter.</span>
vMissing.vt = VT_ERROR;
vMissing.scode = DISP_E_PARAMNOTFOUND; 

CApplication wordApp; <span class="rem">// Variable to hold our Application class object</span>
wordApp.CreateDispatch(_T(<span class="str">&quot;Word.Application&quot;</span>)); <span class="rem">// create the instance of Word</span>
wordApp.put_Visible(TRUE); <span class="rem">// make our instance visible to user</span> 

CDocuments docs(wordApp.get_Documents()); <span class="rem">// Get the Application.Documents collection object</span>
CDocument0 doc ( docs.Add( <span class="rem">// Add a new Document to the Documents collection.</span>
	&amp;vMissing, &amp;vMissing, &amp;vMissing, &amp;vMissing) ); <span class="rem">// All the four input parameters are optional.</span> 

CSelection selection ( wordApp.get_Selection() ); <span class="rem">// Get the current selection/cursor position in the newly created document.</span>
selection.put_Text ( _T(<span class="str">&quot;Hello World!&quot;</span>) ); <span class="rem">// Here goes our magical sentence in the document.</span> 

selection.ReleaseDispatch(); <span class="rem">// free the COM resrouces held by our Selection object.</span>
doc.ReleaseDispatch(); <span class="rem">// free the COM resrouces held by our Document object.</span>
docs.ReleaseDispatch(); <span class="rem">// free the COM resrouces held by our Documents collection object.</span>
wordApp.ReleaseDispatch(); <span class="rem">// free the COM resrouces held by our Application object.</span> 

::CoUninitialize(); <span class="rem">// Clean up previously loaded COM related stuff</span></pre>
</div>
<p>&#160;</p>
<p>Also, remember to #include the four header files in the file where you will paste the above code (XXXDlg.cpp).</p>
<p>That’s it; go ahead compile and run your project. When you click your button, it should launch Word with a new document containing “Hello World!” in it… (isn’t that magical?&#8230; well I told you so <img src='http://www.manvirsingh.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  )</p>
<p>If you are a .Net guy, and looking for a similar example in C#, here are the steps you need to perform…</p>
<p>Create a C# based WinForms application; add a button and its click event handler to your form. In the event handler button copy and paste the following code snippet:</p>
<p>&#160;</p>
<div class="cppcode_1">
<pre><span class="kwrd">object</span> vMissing = Type.Missing;        <span class="rem">//object defining missing/optional parameters</span>

<span class="rem">// Create a new instance of Word</span>
Microsoft.Office.Interop.Word.Application wordApp = <span class="kwrd">new</span> Microsoft.Office.Interop.Word.Application();

<span class="rem">// Make it visible to the user</span>
wordApp.Visible = <span class="kwrd">true</span>;

<span class="rem">// Get the Documents collection</span>
Microsoft.Office.Interop.Word.Documents docs = wordApp.Documents;

<span class="rem">// Add a new document to the Documents collection</span>
Microsoft.Office.Interop.Word.Document doc = docs.Add(<span class="kwrd">ref</span> vMissing, <span class="kwrd">ref</span> vMissing, <span class="kwrd">ref</span> vMissing, <span class="kwrd">ref</span> vMissing);

<span class="rem">// Get the current selection/cursor position in the newly created document.</span>
Microsoft.Office.Interop.Word.Selection selection = wordApp.Selection;

<span class="rem">// Here goes our magical sentence in the document.</span>
selection.Text = <span class="str">&quot;Hello World!&quot;</span>;</pre>
</div>
<p>&#160;</p>
<p>Now, before you can compile your project, you need to add reference to the assembly which contains definitions of the classes for used above. For this, right click on the <strong>*References*</strong> folder in your project’s solution explorer, and select Add Reference. From the list of available .Net assemblies select Microsoft.Office.Interop.Word assembly, and click Ok.</p>
<p>Well, that’s it for today. We will discuss about the classes, terms and other related concepts that I have used in today’s snippets in the coming posts, soon. Till then, take care and happy coding <img src='http://www.manvirsingh.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<style type="text/css">
<p>.cppcode_1, .cppcode_1 pre { 	color: black; 	font-family: "Courier New", courier, monospace; 	background-color: #ffffff; 	width:100%; } .cppcode_1 pre { margin: 0em; overflow:auto;} .cppcode_1 .rem { color: #008000; } .cppcode_1 .kwrd { color: #0000ff; } .cppcode_1 .str { color: #ff0000; } .cppcode_1 .op { color: #0000c0; } .cppcode_1 .preproc { color: #cc6633; } .cppcode_1 .asp { background-color: #ffff00; } .cppcode_1 .html { color: #800000; } .cppcode_1 .attr { color: #ff0000; } .cppcode_1 .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .cppcode_1 .lnum { color: #606060; } </style></p>
]]></content:encoded>
			<wfw:commentRss>http://www.manvirsingh.net/index.php/demystifying-microsoft-office-object-model-part-i/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>An MFC based ActiveX may crash Excel 2003 when inserted while recording a macro</title>
		<link>http://www.manvirsingh.net/index.php/an-mfc-based-activex-may-crash-excel-2003-when-inserted-while-recording-a-macro/</link>
		<comments>http://www.manvirsingh.net/index.php/an-mfc-based-activex-may-crash-excel-2003-when-inserted-while-recording-a-macro/#comments</comments>
		<pubDate>Sun, 31 May 2009 01:19:25 +0000</pubDate>
		<dc:creator>Manvir</dc:creator>
				<category><![CDATA[Office Automation]]></category>
		<category><![CDATA[activex]]></category>
		<category><![CDATA[crash]]></category>
		<category><![CDATA[excel]]></category>
		<category><![CDATA[visual studio]]></category>

		<guid isPermaLink="false">http://www.manvirsingh.net/?p=10</guid>
		<description><![CDATA[Let’s say we have created an ActiveX control using the wizard in Visual Studio 2008, and have checked the “Available in Insert Object Dialog” checkbox on the last page of the wizard as shown below: As the name suggests, this setting enables the ActiveX control to be in “Insert Object” dialog box, which is available [...]]]></description>
			<content:encoded><![CDATA[<p>Let’s say we have created an ActiveX control using the wizard in Visual Studio 2008, and have checked the “Available in Insert Object Dialog” checkbox on the last page of the wizard as shown below:</p>
<p style="text-align: center;"><img class="size-full wp-image-14 aligncenter" title="MFC ActiveX Control Wizard" src="http://www.manvirsingh.net/wp-content/uploads/2009/05/mfcwizard.jpg" alt="MFC ActiveX Control Wizard" width="614" height="523" /></p>
<p>As the name suggests, this setting enables the ActiveX control to be in “Insert Object” dialog box, which is available in most Compound Document container applications, such as, Wordpad, Word, Excel, PowerPoint, etc.</p>
<p>Now, when we insert such an ActiveX to Excel 2003 (using this Insert Object dialog box) while we are recording a macro; Excel may crash.</p>
<p>This happens because the default MFC implementation of the ActiveX enables it to capture the focus when instantiated. So, when we insert it to Excel while recording a macro, our ActiveX gets the focus and this causes Excel to crash.</p>
<p>If our ActiveX is not a compound document object (like Word document, PowerPoint spreadsheet etc.) then we can avoid this crash by modifying our ActiveX code generated by the MFC wizard. Here are the steps to achieve this:</p>
<ol>
<li>Open the <em>ActiveX</em>Ctrl.cpp file (here <em>ActiveX</em> is the name that you have given to your control).</li>
<li>Scroll down to the following comment and code block:</li>
<p style="text-align: left; padding-left: 60px;"><code>// Control type information<br />
</code></p>
<p style="text-align: left; padding-left: 60px;"><code>static const DWORD BASED_CODE _dwAxTest1OleMisc =<br />
OLEMISC_ACTIVATEWHENVISIBLE |<br />
OLEMISC_SETCLIENTSITEFIRST |<br />
OLEMISC_INSIDEOUT |<br />
OLEMISC_CANTLINKINSIDE |<br />
OLEMISC_RECOMPOSEONRESIZE;</code></p>
<p style="text-align: left; padding-left: 60px;"><code><br />
</code></p>
<li>Now OR a new flag <strong>OLEMISC_NOUIACTIVATE</strong> to the above list, as below:</li>
<p style="padding-left: 60px;"><code>// Control type information</code></p>
<p style="padding-left: 60px;"><code>static const DWORD BASED_CODE _dwAxTest1OleMisc =<br />
OLEMISC_ACTIVATEWHENVISIBLE |<br />
OLEMISC_SETCLIENTSITEFIRST |<br />
OLEMISC_INSIDEOUT |<br />
OLEMISC_CANTLINKINSIDE |<br />
OLEMISC_RECOMPOSEONRESIZE |<br />
<span style="color: #ff0000;"><strong>OLEMISC_NOUIACTIVATE</strong></span>;</code></p>
<p style="padding-left: 60px;"><code><br />
</code></ol>
<p>This flag ensures that our ActiveX will not get activated (or gain focus) when it is instantiated, thereby avoiding the crash in Excel 2003.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.manvirsingh.net/index.php/an-mfc-based-activex-may-crash-excel-2003-when-inserted-while-recording-a-macro/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
