Office and COM interop with .NET: *Now* I'll give you a second glance

by Jon Davis 10. July 2009 14:27

Well we're not quite a decade yet--certainly a lot longer than five years, though--since .NET came out and promised to be the next big thing for programming. C# was supposed to put VB6 to shame. It didn't, though, because with VB6 you had such things as optional parameters and variants--the latter being an ugly beast but under the covers the flexibility that VB6 was made programming so much quicker and easier sometimes.

Now that C# 4.0 introduces optional parameters and dynamic objects, the whole playing field is changing. And now I can feel a bit freer to tinker with old-school technologies built on COM in my language of choice because the language has finally caught up with last decade's featureset in this respect.

I like this somewhat recent blog post by SamNG:

http://blogs.msdn.com/samng/archive/2009/06/16/com-interop-in-c-4-0.aspx

Lets look at a quick Office example.

	static void Main()
	{
	Word.Application app = new Word.Application();
	object m = Type.Missing;
	app.Documents.Add(ref m, ref m, ref m, ref m);
	}
	

This is such typical code! I have to struggle with the type system to make it happy, just to add a simple Word document!

... [With C# 4.0] in the following code, call (1) gets transformed into call (2).

	static void Main()
	{
	Word.Application app = new Word.Application();
	// (1) Your initial call.
	app.Documents.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
	// (2) Compiler generates locals and inserts them.
	object m = Type.Missing;
	app.Documents.Add(ref m, ref m, ref m, ref m);
	}
	

The great thing about this too, is that with the introduction of named and optional arguments, and using the fact that the feature generates Type.Missing in place of default values for object on COM types, we can simply remove the arguments altogether!

	static void Main()
	{
	Word.Application app = new Word.Application();
	// (1) Your optional-parameter-omitted initial call.
	app.Documents.Add();
	// (2) Compiler generates locals and inserts them.
	object m = Type.Missing;
	app.Documents.Add(ref m, ref m, ref m, ref m);
	}
	

Pretty cool stuff huh? Definitely makes programming against the Office APIs much nicer.

Yup. Indeed.


 

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen

About the author

Jon Davis (aka "stimpy77") has been a programmer, developer, and consultant for web and Windows software solutions professionally since 1997, with experience ranging from OS and hardware support to DHTML programming to IIS/ASP web apps to Java network programming to Visual Basic applications to C# desktop apps.
 
Software in all forms is also his sole hobby, whether playing PC games or tinkering with programming them. "I was playing Defender on the Commodore 64," he reminisces, "when I decided at the age of 12 or so that I want to be a computer programmer when I grow up."

Jon was previously employed as a senior .NET developer at a very well-known Internet services company whom you're more likely than not to have directly done business with. However, this blog and all of jondavis.net have no affiliation with, and are not representative of, his former employer in any way.

Contact Me 


Tag cloud

Calendar

<<  December 2020  >>
MoTuWeThFrSaSu
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

View posts in large calendar